Table of Contents:



Software Quality Assurance (QA) TrainingSELENIUM GRID AUTOMATIONTable of Contents: TOC \o "1-3" \h \z \u Selenium Grid2 IntroductionSelenium Grid ArchitectureWhat is a Hub and NodeCreating Hub and NodesRegister Node with HUBRunning single test on single node – serially/sequentially (one after other) on multiple browsersRunning single test on single node – parallely (all at a time) on multiple browsersBest practices of Selenium GridRevision History:DescriptionVersionDateInitial version created15.04/13/16Changes done in hub/node16.011/15/17Configuring JSON file format to initialize/configure Hub & Nodes. Executing multiple scripts parallelly 20.02/1/18Selenium Grid2 for WebDriver/ TestNG / JavaSelenium Grid2 IntroductionDeciding number of browsers on a NodeDeciding type of browsers on NodeLimiting number of concurrent browsers on nodeDifference between maxSession and maxInstanceConfiguring chromedriver and IEdriver exe files on gridSelenium Grid2 Introduction:Selenium Grid is a part of the Selenium Suite that specializes on running multiple tests across different browsers, operating systems, and machines in parallel. Selenium Grid uses a hub-node concept where you only run the test on a single machine called a hub, but the execution will be done by different machines called nodes. Bottom line is: it will reduce the execution and we execute scripts remotely.Selenium Grid Architecture:What is a Hub and Node?The HubThe hub is the central point where you load your tests into.There should only be one hub in a grid.The hub is launched only on a single machine, say, a computer whose O.S is Windows 7 and whose browser is IE.The machine containing the hub is where the tests will be run, but you will see the browser being automated on the node.The NodesNodes are the Selenium instances that will execute the tests that you loaded on the hub.There can be one or more nodes in a grid.Nodes can be launched on multiple machines with different platforms and browsers.The machines running the nodes need not be the same platform as that of the hub.Creating Hub and NodesPre-requisite: Download Selenium Standalone server from: and put the .jar file at: C:\Selenium\Grid2\selenium-server-standalone-3.4.0Download drivers for IE, FF and Chrome (Please refer Selenium WebDriver doc)Note: You must start HUB first and then Node(s). All nodes must be registered with hub.Start HUB:Open command prompt and go to the location, where your selenium server standalone jar file is (ex: C:\Selenium\Grid2\)C:\Selenium\Grid2> java -jar selenium-server-standalone-3.4.0.jar –port 4444 -role hubBy default, hub will start with port 4444 and you will see a message like: 18:23:42.715 INFO - Selenium Grid hub is up and runningRegister Node with HUB:Open another command prompt and go to the location, where your selenium server standalone jar file is (ex: C:\Selenium\Grid2\)If you want to run the node against only Firefox browser, then run the below command from node’s command line.C:\Selenium\Grid2>java -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub -port 5566 -browser browserName=firefoxNote: localhost = IP address of Hub Now, open in firefox browser, you should see one firefox browserIf you want to run the node (or run multiple scripts parallelly) against Firefox, Chrome and IE browsers on the same machine/PC, then run the below command from node’s command line.Exit currently running node, by pressing cntl+CC:\Selenium\Grid2>java -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub -port 5566 -browser browserName=firefox -browser browserName=chrome -browser browserName=iexploremaxInstances vs maxSession:maxInstances:C:\Selenium\Grid2>java -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub -port 5566 -browser browserName=firefox,maxInstances=2 -browser browserName=chrome -browser browserName=iexploreExit currently running node, by pressing cntl+C and execute above command. Refresh , You should see 2 firefox instances ie you can run your scripts against 2 firefox browsers simultaneously C:\Selenium\Grid2>java -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub -port 5566 -browser browserName=firefox,maxInstances=2 -browser browserName=chrome,maxInstances=2 -browser browserName=iexplore,maxInstances=2maxSession:Default is 5 sessions but you can change by using maxSessionC:\Selenium\Grid2>java -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub -port 5566 -browser browserName=firefox,maxInstances=2 -browser browserName=chrome,maxInstances=2 -browser browserName=iexplore,maxInstances=2 -maxSession 3That means, at a time only 3 browsers can be opened on one node b’cz we have given maxSession 3Refresh you use different browsers, better give each browser driver path like below:C:\Selenium\Grid2>java –Dwebdriver.chrome.driver= C:\Selenium\Grid2\chromedriver.exe -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub -port 5567 -browser browserName=firefox,maxInstances=1 -browser browserName=chrome,maxInstances=1 -browser browserName=iexplore,maxInstances=1 -maxSession 1C:\Selenium\Grid2>java -Dwebdriver.chrome.driver=C:\Selenium\Grid2\chromedriver.exe -Dwebdriver.ie.driver=C:\Selenium\Grid2\IEDriverServer.exe -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub -port 5566 -browser browserName=firefox,maxInstances=3 -browser browserName=chrome,maxInstances=2 -browser browserName=iexplore,maxInstances=3 -maxSession 3C:\Selenium\Grid2>java -Dwebdriver.chrome.driver=C:\Selenium\Grid2\chromedriver.exe -Dwebdriver.ie.driver=C:\Selenium\Grid2\IEDriverServer.exe -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub -port 5567 -browser browserName=firefox,maxInstances=1 -browser browserName=chrome,maxInstances=1 -browser browserName=iexplore,maxInstances=1 -maxSession 1Node for all browsers:C:\Selenium\Grid2>java -Dwebdriver.chrome.driver=C:\Selenium\Grid2\chromedriver.exe -Dwebdriver.ie.driver=C:\Selenium\Grid2\IEDriverServer.exe -Dwebdriver.firefox.driver=C:\Selenium\Grid2\geckodriver.exe -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub -port 5567 -browser browserName=firefox,maxInstances=3 -browser browserName=chrome,maxInstances=3 -browser browserName=iexplore,maxInstances=3 -maxSession 9NOTE: If you see any issue while copy/past, then type the whole command within command promptAccess and check: configurationsDesiredCapabilites is used to set the type of browser and OS that we will automateRemoteWebDriver is used to set which node (or machine) that our test will run against.Running single test on single node – serially on multiple browsersRunning single test on single node – parallelly on multiple browsersOpen Eclipse and create a new workspace “Grid2WS”, project “Grid2ForSelenium” and create a new package “testcases” (src->New->Package) and also add all selenium jar files to the project properties -> java build path (better download latest ver of selenium server and selenium java client from ) and also, make sure TestNG installed/configured with EclipseOn a single machine, setup Hub -> Nodes (1: Serially 2: Parallelly) i.e implement single test case and then run that test case serially on multiple browsers and then parallelly on multiple browsers.Running single test on single node – serially/sequentially (one after other) on multiple browsers (FF, Chrome and IE):Test Case: Verify user can login to and close the browser.Create a new class Amazon_login1 under src -> testcases without public static void…Note: Make sure you integrate testng library within java build path (Add Libraries)package testcases;import .MalformedURLException;import .URL;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.Platform;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.remote.RemoteWebDriver;import org.testng.annotations.AfterClass;import org.testng.annotations.BeforeClass;import org.testng.annotations.DataProvider;import org.testng.annotations.Test; // if you see an error add testng library by clicking on the error//import com.beust.jcommander.Parameters;public class Amazon_login1 {@Test(dataProvider="getData")public void amazonLogin(String u, String p, String b) throws MalformedURLException, InterruptedException {System.out.println(b);//RemoteWebDriverDesiredCapabilities cap = null; // by default cap=blankif (b.equals("firefox")){cap = DesiredCapabilities.firefox();cap.setBrowserName("firefox"); cap.setPlatform(Platform.ANY);}else if (b.equals("chrome")){cap = DesiredCapabilities.chrome();cap.setBrowserName("chrome"); cap.setPlatform(Platform.ANY);} /*else if (b.equals("iexplore")){// for edge on windows10, need to use "edge"cap = DesiredCapabilities.internetExplorer();cap.setBrowserName("iexplore"); cap.setPlatform(org.openqa.selenium.Platform.WINDOWS);}*/// give URL of HubRemoteWebDriver driver = new RemoteWebDriver (new URL(""), cap);driver.manage().window().maximize(); //Launch web pagedriver.get("");//driver.findElement(By.cssSelector("#nav-link-accountList > span.nav-line-2")).click();driver.findElement(By.cssSelector("#nav-link-accountList > span.nav-line-2")).click();Thread.sleep(2000);//Enter emaildriver.findElement(By.cssSelector("#ap_email")).sendKeys("shalmali.qa1@");Thread.sleep(2000);//Click Continue buttondriver.findElement(By.cssSelector("#continue")).click();Thread.sleep(2000);//Enter password. driver.findElement(By.cssSelector("#ap_password")).sendKeys("qatest123");Thread.sleep(2000);//Click on Sign in button. driver.findElement(By.cssSelector("#signInSubmit")).click();//Wait for 3 secondsThread.sleep(3000);driver.quit();} //@DataProvider(parallel=true) // Parallel execution of tests @DataProvider // Sequential execution of testspublic Object[][] getData(){Object data[][] = new Object[2][3];// row 1data[0][0]="strmanualqaapr17@";data[0][1]="Valley17";data[0][2]="firefox";// row 2data[1][0]="strmanualqaapr17@";data[1][1]="Valley17";data[1][2]="chrome";/*// row 3data[2][0]="strmanualqaapr17@";data[2][1]="Valley17";data[2][2]="iexplore";*/return data;}}Download Testng.xml (not testng.xml) (available in G Drive), copy/past that in project (ex:Grid2ForSelenium) in eclipseOpen Testng.xml (right click -> open with -> xml editor – click Source tab) and update like below:<!DOCTYPE suite SYSTEM ""><suite name="Selenium Grid with WebDriver" verbose="3" parallel="tests" thread-count="2"><test name="Selenium Grid Demo"><classes> <class name="testcases.Amazon_login1"/> </classes></test></suite>Run Selenium hub and node on localhostHub: (Please use these files located in Rama’s G-Drive: QA-Tools->Selenium->Selenium-Advanced->Grid2)C:\Selenium\0817\Grid2>java -jar selenium-server-standalone-3.5.2.jar –port 4444 -role hubNode: (Please use these files located in Rama’s G-Drive: QA-Tools->Selenium->Selenium-Advanced->Grid2)C:\Selenium\0817\Grid2>java -Dwebdriver.chrome.driver=C:\Selenium\0817\Grid2\chromedriver.exe -Dwebdriver.ie.driver=C:\Selenium\0817\Grid2\IEDriverServer.exe -Dwebdriver.firefox.driver=C:\Selenium\0817\Grid2\geckodriver.exe -jar selenium-server-standalone-3.5.2.jar -role webdriver -hub -port 5567 -browser browserName=firefox,maxInstances=2 -browser browserName=chrome,maxInstances=2 -browser browserName=iexplore,maxInstances=2 -maxSession 6Open this now and check: Testng.xml now ie right click and run as TestNG Suite, this particular script (Amazon_Login1.java) will open up in the particular node and execute this script against FF, Chrome and IE sequentially (one after other).Running single test on single node – parallelly (all at a time) on multiple browsers:Create a new class Amazon_login2 under src -> test cases without public static void…package testcases;import .MalformedURLException;import .URL;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.Platform;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.remote.RemoteWebDriver;import org.testng.annotations.AfterClass;import org.testng.annotations.BeforeClass;import org.testng.annotations.DataProvider;import org.testng.annotations.Test; // if you see an error add testng library by clicking on the error//import com.beust.jcommander.Parameters;public class Amazon_login2 {@Test(dataProvider="getData")public void amazonLogin(String u, String p, String b) throws MalformedURLException, InterruptedException {System.out.println(b);//RemoteWebDriverDesiredCapabilities cap = null; // by default cap=blankif (b.equals("firefox")){cap = DesiredCapabilities.firefox();cap.setBrowserName("firefox"); cap.setPlatform(Platform.ANY);}else if (b.equals("chrome")){cap = DesiredCapabilities.chrome();cap.setBrowserName("chrome"); cap.setPlatform(Platform.ANY);} /*else if (b.equals("iexplore")){// for edge on windows10, need to use "edge"cap = DesiredCapabilities.internetExplorer();cap.setBrowserName("iexplore"); cap.setPlatform(org.openqa.selenium.Platform.WINDOWS);}*/// give URL of HubRemoteWebDriver driver = new RemoteWebDriver (new URL(""), cap);driver.manage().window().maximize(); //Launch web pagedriver.get("");//driver.findElement(By.cssSelector("#nav-link-accountList > span.nav-line-2")).click();driver.findElement(By.cssSelector("#nav-link-accountList > span.nav-line-2")).click();Thread.sleep(2000);//Enter emaildriver.findElement(By.cssSelector("#ap_email")).sendKeys("shalmali.qa1@");Thread.sleep(2000);//Click Continue buttondriver.findElement(By.cssSelector("#continue")).click();Thread.sleep(2000);//Enter password. driver.findElement(By.cssSelector("#ap_password")).sendKeys("qatest123");Thread.sleep(2000);//Click on Sign in button. driver.findElement(By.cssSelector("#signInSubmit")).click();//Wait for 3 secondsThread.sleep(3000);driver.quit();}@DataProvider(parallel=true) // Parallel execution of tests//@DataProvider // Sequential execution of testspublic Object[][] getData(){Object data[][] = new Object[2][3];// row 1data[0][0]="strmanualqaapr17@";data[0][1]="Valley17";data[0][2]="firefox";// row 2data[1][0]="strmanualqaapr17@";data[1][1]="Valley17";data[1][2]="chrome";/*// row 3data[2][0]="strmanualqaapr17@";data[2][1]="Valley17";data[2][2]="iexplore";*/return data;}}Need Testng.xml to execute: Right click on class (Gmail_Login2.java)-> TestNG->Convert To TestNG and Finish, please make sure, you have the following code in testng.xmlCopy/past Testng.xml and rename as Testng2.xml in project (ex:Grid2ForSelenium) in eclipseOpen testng2.xml (right click -> open with -> xml editor) and update like below:<!DOCTYPE suite SYSTEM ""><suite name="Selenium Grid with WebDriver" verbose="3" parallel="tests" thread-count="3"><test name="Selenium Grid Demo"><classes> <class name="testcases.Amazon_login2"/> </classes></test></suite>Run Selenium hub and node on localhostHub: (Please use these files located in Rama’s G-Drive: QA-Tools->Selenium->Selenium-Advanced->Grid2)C:\Selenium\0817\Grid2>java -jar selenium-server-standalone-3.5.2.jar –port 4444 -role hubNode: (Please use these files located in Rama’s G-Drive: QA-Tools->Selenium->Selenium-Advanced->Grid2)C:\Selenium\0817\Grid2>java -Dwebdriver.chrome.driver=C:\Selenium\0817\Grid2\chromedriver.exe -Dwebdriver.ie.driver=C:\Selenium\0817\Grid2\IEDriverServer.exe -Dwebdriver.firefox.driver=C:\Selenium\0817\Grid2\geckodriver.exe -jar selenium-server-standalone-3.5.2.jar -role webdriver -hub -port 5567 -browser browserName=firefox,maxInstances=1 -browser browserName=chrome,maxInstances=1 -browser browserName=iexplore,maxInstances=1 -maxSession 3Open this now and check: Testng2.xml now ie right click and run as TestNG Suite, this particular script (Gmail_Login2.java) will open up in the particular node and execute this script against FF, IE and Chrome parallelly (all at a time).------ Optional -----Configuring JSON file format to initialize/configure Hub & Nodes. Purpose: Instead of having big command (ex: IE/FF/Ch path etc) to run hub and node(s), we can have .json files to run hub and nodes.Please download hub.json, node1.json and node2.json from SeleinumFiles\Grid-Run-Multiple-Scripts-Parallely folder in G Drive. Copy /past them under relevant folder like: C:\Selenium\1217\SeleniumGrid and also, download latest ver of selenium server .jar and past it under above folder.Hub:C:\Selenium\1217\SeleniumGrid>java -Dwebdriver.gecko.driver=C:\Selenium\1217\SeleniumGrid\geckodriver.exe -Dwebdriver.chrome.driver=C:\Selenium\1217\SeleniumGrid\chromedriver.exe -jar selenium-server-standalone-3.9.1.jar -role hub -hubConfig hub.jsonNode1:C:\Selenium\1217\SeleniumGrid>java -Dwebdriver.gecko.driver=C:\Selenium\1217\SeleniumGrid\geckodriver.exe -Dwebdriver.chrome.driver=C:\Selenium\1217\SeleniumGrid\chromedriver.exe -jar selenium-server-standalone-3.9.1.jar -role node -nodeConfig node1.jsonNode2:C:\Selenium\1217\SeleniumGrid>java -Dwebdriver.gecko.driver=C:\Selenium\1217\SeleniumGrid\geckodriver.exe -Dwebdriver.chrome.driver=C:\Selenium\1217\SeleniumGrid\chromedriver.exe -jar selenium-server-standalone-3.9.1.jar -role node -nodeConfig node2.json------ Optional -----Running multiple scripts together parallelly against multiple browsersLet’s take 2 test scripts (Dropbox.java and Google_Search.java) and execute them parallelly. But each script execute against Chrome and Firefox browsers parallelly. That means at a time, 4 tests will run, 2 on chrome and 2 on firefox. Bottom line is … this will reduce the total execution time.Earlier (above chapter) we have executed single test script against multiple browsers parallelly. Steps:Under Grid Java project, create package testcases and copy/past Dropbox.java and Google_Search.java within testcases package and under Grid Java project, copy/past testng1.xml.You can find above 3 files at: Files->Software Quality Assurance (SQA) Training->QA-TRAINING-ADVANCED->SQA-202 Automation Testing (Selenium 2.0 WebDriver Tool)->SeleniumFiles->Grid-Run-Multiple-Scripts-Parallely.zip (you need to unzip) in G Drive.If you see any error for “import static Util.Utility.driver;” then remove it.Remove the comment from “RemoteWebDriver”Run Selenium hub and node on localhostHub: (Please use these files located in Rama’s G-Drive: QA-Tools->Selenium->Selenium-Advanced->Grid2)C:\Selenium\0817\Grid2>java -jar selenium-server-standalone-3.5.2.jar –port 4444 -role hubNode: (Please use these files located in Rama’s G-Drive: QA-Tools->Selenium->Selenium-Advanced->Grid2)C:\Selenium\0817\Grid2>java -Dwebdriver.chrome.driver=C:\Selenium\0817\Grid2\chromedriver.exe -Dwebdriver.ie.driver=C:\Selenium\0817\Grid2\IEDriverServer.exe -Dwebdriver.firefox.driver=C:\Selenium\0817\Grid2\geckodriver.exe -jar selenium-server-standalone-3.5.2.jar -role webdriver -hub -port 5567 -browser browserName=firefox,maxInstances=2 -browser browserName=chrome,maxInstances=4 -browser browserName=iexplore,maxInstances=2 -maxSession 8Right click testng1.xml -> Run As TestNG Suite.You should see 4 scripts running parallelly like below:Google chromeDropbox chromeDropbox firefoxGoogle firefox===============================================Selenium Grid with webdriverTotal tests run: 4, Failures: 0, Skips: 0===============================================SummarySelenium Grid is used to run multiple tests simultaneously in different browsers and platforms to reduce the execution time.Grid uses the hub-node concept.The hub is the central point wherein you load your tests.Nodes are the Selenium instances that will execute the tests that you loaded on the hub.To install Selenium Grid, you only need to download the Selenium Server jar file.There are 2 ways to verify if the hub is running: one was through the command prompt, and the other was through a browser.To run test scripts on the Grid, you should use the DesiredCapabilities and the RemoteWebDriver objects.DesiredCapabilites is used to set the type of browser and OS that we will automateRemoteWebDriver is used to set which node (or machine) that our test will run against.Verbose: If we specify the verbose attribute in Testng.xml with smaller number (ex:1), then the test results log details in the Eclipse IDE -> 'console' window will be less. But if you want to get more details and want to see more clarity on the test results log details in the Eclipse IDE -> 'console' window then you have to assign larger number (ex: 10 is the max) to the verbose attribute.Thread-count: The default number of threads to use when running tests in parallel. This sets the default maximum number of threads to use for running tests in parallel. It will only take effect if the parallel mode has been selected (for example, with the -parallel option). This can be overridden in the suite definition. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download