{"id":30463,"date":"2015-12-03T22:03:10","date_gmt":"2015-12-03T16:33:10","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=30463"},"modified":"2016-12-19T15:02:52","modified_gmt":"2016-12-19T09:32:52","slug":"parallel-execution-with-selenium-grid","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/parallel-execution-with-selenium-grid\/","title":{"rendered":"Parallel Execution with Selenium Grid"},"content":{"rendered":"<h2 style=\"text-align: center\">\u00a0 \u00a0 \u00a0Selenium Grid for Parallel Execution<\/h2>\n<pre><em>When we say parallel test execution in Selenium is achieved by Selenium Grid than statement is partly incorrect.<\/em><\/pre>\n<ol>\n<li>Testing Framework like testng is used for parallel test execution<\/li>\n<li>Selenium Grid is used for <a title=\"automated testing services\" href=\"http:\/\/www.tothenew.com\/testing\/automated-independent-manual-testing\" target=\"_blank\">automated testing execution<\/a> on Distributed systems parallely<\/li>\n<\/ol>\n<h2 style=\"text-align: center\">Selenium Grid Concept<\/h2>\n<ol>\n<li>In Selenium Grid architecture we have 1 Hub which acts as central controlling authority and connecting nodes. Nodes must be registered to Hub<\/li>\n<li>Consider node as port opened on machine (loacal or remote). Each node is capable to opening multiple browsers.<\/li>\n<li>On single machine we can have multiple nodes opened<\/li>\n<li>Grid Hub decides what\u00a0tests needs to routed on which node, we can&#8217;t control them<\/li>\n<\/ol>\n<h2 style=\"text-align: center\">Best Practices of Selenium Grid<\/h2>\n<ol>\n<li>Single machine should open one node only<\/li>\n<li>Each node should run only single type of browser<\/li>\n<li>We need various driver objects for various threads to be run parallely, so create driver as ThreadLocal variable<\/li>\n<\/ol>\n<p>Let consider an example what we would be achieving in our Grid:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-30485 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/12\/SeleniumGrid.png\" alt=\"SeleniumGrid\" width=\"1360\" height=\"558\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: center\">Start\u00a0Hub and Nodes<\/h2>\n<ol>\n<li>Download &#8220;Selenium Standalone Server&#8221; from &#8220;http:\/\/www.seleniumhq.org\/download\/&#8221; on all 3 machines<\/li>\n<li>Goto machine 1 and open command prompt.<\/li>\n<li>Navigate to location where jar is located.<\/li>\n<li>Start Hub by command: \u00a0<strong>java -jar selenium-server-standalone-2.48.2.jar -role hub<\/strong><\/li>\n<li>Open browser and navigate to\u00a0<strong>http:\/\/localhost:4444\/grid\/console\u00a0<\/strong>and verify hub is started by checking below image.<img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-30493\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/12\/HubStarted.png\" alt=\"HubStarted\" width=\"607\" height=\"67\" \/><\/li>\n<li>Goto machine 2 and open command prompt<\/li>\n<li>Navigate to location where jar is located.<\/li>\n<li>Start Node by command:\u00a0<strong>java -Dwebdriver.chrome.driver=<\/strong>{path to chromedriver.exe}<strong>-jar selenium-server-standalone-2.48.2.jar -role webdriver -hub  -port 5560 -browser browserName=chrome,maxInstances=2,maxSession=2<\/strong><\/li>\n<li>Goto machine 1 and in\u00a0browser navigate to\u00a0<strong>http:\/\/localhost:4444\/grid\/console\u00a0<\/strong>and verify node\u00a0is started by checking below image.<img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-30495 \" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/12\/SeleniumGridNode1.png\" alt=\"SeleniumGridNode1\" width=\"580\" height=\"119\" \/><\/li>\n<li>Goto machine 3 and open command prompt.<\/li>\n<li>Navigate to location where jar is located.<\/li>\n<li>Start Node by command:\u00a0<strong>java -jar selenium-server-standalone-2.48.2.jar -role webdriver -hub  -port 5557 -browser browserName=firefox,maxInstances=5,maxSession=2<\/strong><\/li>\n<li>Goto machine 1 and in\u00a0browser navigate to\u00a0<strong>http:\/\/localhost:4444\/grid\/console\u00a0<\/strong>and verify node\u00a0is started by checking below image. <img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-30525\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/12\/SeleniumGridNode2.png\" alt=\"SeleniumGridNode2\" width=\"585\" height=\"116\" \/><\/li>\n<li>Nodes can be opened for various settings like browser, platform, version.<\/li>\n<li>Now Selenium Hub and Nodes are created, lets make some @Test and execute them.<\/li>\n<li>Make Project on any machine, my project structure as below:<img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-30486 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/12\/PackageStructure.png\" alt=\"PackageStructure\" width=\"237\" height=\"181\" \/><\/li>\n<li>Source code for files are given below.<\/li>\n<li>Execute testng.xml and execution will start parallely on both machines.<\/li>\n<\/ol>\n<h3 style=\"text-align: center\">testng.xml<\/h3>\n<p>[sourcecode language=&#8221;javascript&#8221;]<br \/>\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br \/>\n&lt;suite name=&quot;Parallel test suite&quot; parallel=&quot;classes&quot; thread-count=&quot;2&quot;&gt;<br \/>\n\t&lt;test name=&quot;Regression 1&quot;&gt;<br \/>\n\t    &lt;parameter name=&quot;myBrowser&quot; value=&quot;firefox&quot;\/&gt;<br \/>\n\t\t&lt;classes&gt;<br \/>\n\t\t\t&lt;class name=&quot;myPackage.TestParallel&quot; \/&gt;<br \/>\n\t\t\t&lt;class name=&quot;myPackage.TestParallel&quot; \/&gt;<br \/>\n\t\t&lt;\/classes&gt;<br \/>\n\t&lt;\/test&gt;<br \/>\n\t&lt;test name=&quot;Regression 2&quot;&gt;<br \/>\n\t    &lt;parameter name=&quot;myBrowser&quot; value=&quot;chrome&quot;\/&gt;<br \/>\n\t\t&lt;classes&gt;<br \/>\n\t\t\t&lt;class name=&quot;myPackage.TestParallel&quot; \/&gt;<br \/>\n   \t\t\t&lt;class name=&quot;myPackage.TestParallel&quot; \/&gt;<br \/>\n\t\t&lt;\/classes&gt;<br \/>\n\t&lt;\/test&gt;<\/p>\n<p>&lt;\/suite&gt;<br \/>\n[\/sourcecode]<\/p>\n<h3 style=\"text-align: center\"><strong>BaseClass.java<\/strong><\/h3>\n<p>[sourcecode language=&#8221;javascript&#8221;]<br \/>\npackage myPackage;<\/p>\n<p>package myPackage;<\/p>\n<p>import java.net.MalformedURLException;<br \/>\nimport java.net.URL;<br \/>\nimport java.util.concurrent.TimeUnit;<\/p>\n<p>import org.openqa.selenium.Platform;<br \/>\nimport org.openqa.selenium.WebDriver;<br \/>\nimport org.openqa.selenium.firefox.FirefoxDriver;<br \/>\nimport org.openqa.selenium.remote.DesiredCapabilities;<br \/>\nimport org.openqa.selenium.remote.RemoteWebDriver;<br \/>\nimport org.testng.annotations.AfterClass;<br \/>\nimport org.testng.annotations.AfterMethod;<br \/>\nimport org.testng.annotations.BeforeClass;<br \/>\nimport org.testng.annotations.BeforeMethod;<br \/>\nimport org.testng.annotations.BeforeTest;<br \/>\nimport org.testng.annotations.Parameters;<\/p>\n<p>public class BaseClass {<\/p>\n<p>\t\/\/ThreadLocal will keep local copy of driver<br \/>\n\tpublic static ThreadLocal&lt;RemoteWebDriver&gt; dr = new ThreadLocal&lt;RemoteWebDriver&gt;();<\/p>\n<p>\t@BeforeTest<br \/>\n\t\/\/Parameter will get browser from testng.xml on which browser test to run<br \/>\n\t@Parameters(&quot;myBrowser&quot;)<br \/>\n\tpublic void beforeClass(String myBrowser) throws MalformedURLException{<\/p>\n<p>\t\tRemoteWebDriver driver = null;<\/p>\n<p>\t\tif(myBrowser.equals(&quot;chrome&quot;)){<br \/>\n\t\t\tDesiredCapabilities capability = new DesiredCapabilities().chrome();<br \/>\n\t\t\tcapability.setBrowserName(&quot;chrome&quot;);<br \/>\n\t\t\tcapability.setPlatform(Platform.WINDOWS);<br \/>\n\t\t\tdriver = new RemoteWebDriver(new URL(&quot;http:\/\/localhost:4444\/wd\/hub&quot;), capability);<br \/>\n\t\t}<br \/>\n\t\telse if(myBrowser.equals(&quot;firefox&quot;)){<br \/>\n\t\t\tDesiredCapabilities capability = new DesiredCapabilities().firefox();<br \/>\n\t\t\tcapability.setBrowserName(&quot;firefox&quot;);<br \/>\n\t\t\tcapability.setPlatform(Platform.WINDOWS);<br \/>\n\t\t\tdriver = new RemoteWebDriver(new URL(&quot;http:\/\/localhost:4444\/wd\/hub&quot;), capability);<br \/>\n\t\t}<\/p>\n<p>\t\t\/\/setting webdriver<br \/>\n\t\tsetWebDriver(driver);<\/p>\n<p>\t\tgetDriver().manage().window().maximize();<br \/>\n\t\tgetDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);<\/p>\n<p>\t}<\/p>\n<p>\tpublic WebDriver getDriver() {<br \/>\n\t\treturn dr.get();<br \/>\n\t}<\/p>\n<p>\tpublic void setWebDriver(RemoteWebDriver driver) {<br \/>\n\t\tdr.set(driver);<br \/>\n\t}<\/p>\n<p>\t@AfterClass<br \/>\n\tpublic void afterClass(){<br \/>\n\t\tgetDriver().quit();<br \/>\n\t\tdr.set(null);<\/p>\n<p>\t}<\/p>\n<p>}<\/p>\n<p>[\/sourcecode]<\/p>\n<h3 style=\"text-align: center\"><strong>TestParallel.java<\/strong><\/h3>\n<p>[sourcecode language=&#8221;javascript&#8221;]<br \/>\npackage myPackage;<\/p>\n<p>import java.net.MalformedURLException;<br \/>\nimport java.net.URL;<\/p>\n<p>import org.openqa.selenium.By;<br \/>\nimport org.openqa.selenium.Platform;<br \/>\nimport org.openqa.selenium.WebDriver;<br \/>\nimport org.openqa.selenium.chrome.ChromeDriver;<br \/>\nimport org.openqa.selenium.firefox.FirefoxDriver;<br \/>\nimport org.openqa.selenium.remote.DesiredCapabilities;<br \/>\nimport org.openqa.selenium.remote.RemoteWebDriver;<br \/>\nimport org.testng.annotations.DataProvider;<br \/>\nimport org.testng.annotations.Test;<\/p>\n<p>public class TestParallel extends BaseClass {<\/p>\n<p>\t@Test<br \/>\n\tpublic void test_01() throws InterruptedException, MalformedURLException{<br \/>\n\t\ttry{<br \/>\n\t\t\tgetDriver().get(&quot;http:\/\/www.w3schools.com\/&quot;);<\/p>\n<p>\t\t\tgetDriver().findElement(By.xpath(&quot;html\/body\/div[2]\/div\/a[4]&quot;)).click();<\/p>\n<p>\t\t\t\/\/Wait intentially added to show parallelism execution<br \/>\n\t\t\tThread.sleep(10000);<\/p>\n<p>\t\t\tgetDriver().findElement(By.xpath(&quot;\/\/*[@id=&#8217;gsc-i-id1&#8242;]&quot;)).sendKeys(&quot;test&quot;);<br \/>\n\t\t\tThread.sleep(5000);<\/p>\n<p>\t\t}<br \/>\n\t\tcatch(Exception e){<br \/>\n\t\t\tSystem.out.println(e);<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<\/p>\n<p>[\/sourcecode]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 \u00a0Selenium Grid for Parallel Execution When we say parallel test execution in Selenium is achieved by Selenium Grid than statement is partly incorrect. Testing Framework like testng is used for parallel test execution Selenium Grid is used for automated testing execution on Distributed systems parallely Selenium Grid Concept In Selenium Grid architecture we [&hellip;]<\/p>\n","protected":false},"author":860,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":17},"categories":[1818,1],"tags":[1561,14,25,2857],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/30463"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/860"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=30463"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/30463\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=30463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=30463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=30463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}