{"id":3703,"date":"2016-07-11T01:22:02","date_gmt":"2016-07-10T19:52:02","guid":{"rendered":"https:\/\/techbeamers.com\/?p=3703"},"modified":"2024-02-25T10:43:54","modified_gmt":"2024-02-25T05:13:54","slug":"selenium-grid-webdriver-code-example-java","status":"publish","type":"post","link":"https:\/\/techbeamers.com\/selenium-grid-webdriver-code-example-java\/","title":{"rendered":"Selenium Grid Webdriver Code Example"},"content":{"rendered":"\n<p>In our\u00a0earlier post on Selenium Grid, we explained an easier method to <a href=\"\/download-selenium-grid-set-multiple-browsers\/\" target=\"_blank\" rel=\"noopener\">download and install the\u00a0Selenium Grid<\/a>. Also, the post did teach you how to configure multiple browsers like Firefox, Chrome, and IE with Selenium Grid. In today&#8217;s tutorial, you&#8217;ll get to learn how to run parallel tests with Selenium Grid Webdriver. And since we&#8217;ll create a multi-node setup in this post using Grid and Remote Webdriver, which can even help in using Selenium for load testing.<\/p>\n\n\n\n<p>We&#8217;ll give you the best approach to\u00a0speed up test execution using Selenium Grid. We&#8217;ll make sure you should be able to set up and test, so we&#8217;ll try to keep things simple throughout the post. Also, we&#8217;ll provide a fully working\u00a0Selenium Grid Webdriver code in Java which you can easily run in the Eclipse IDE. If you are a newbie in automation and want to learn Selenium Grid Webdriver, then\u00a0just go through the step-by-step<a href=\"\/six-steps-to-setup-selenium-webdriver-project-in-eclipse\/\" target=\"_blank\" rel=\"noopener\"> process of building a Selenium Webdriver project in Eclipse<\/a>. It&#8217;ll help in ramping up quickly.<\/p>\n\n\n\n<p>You would be amazed to know that even companies like Saucelabs and BrowserStack are making use of Selenium for load testing. They are using vast cloud infrastructure and customized Selenium to scale browser automation and load testing. Just imagine that together they are supporting 1000+ browsers and devices for testing using Selenium.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-selenium-grid-webdriver-code-example-in-java\">Selenium Grid Webdriver Code Example in Java.<\/h2>\n\n\n\n<p>Before we jump to the code section, it&#8217;s important for you to understand the use case we&#8217;ll cover using the Selenium Grid Webdriver code. In this tutorial, we&#8217;ll access a website that does the arithmetic calculations. So, the Selenium Grid Webdriver code will start the three browsers in parallel. Then, it&#8217;ll open the web page and process the calculation request at the same time in all three browsers. However, the operation will end up fast. But you would perceive the difference as the Selenium Grid will speed up the tests by running them in parallel.<\/p>\n\n\n\n<p>With Selenium Grid, you not only speed up the test execution but also make it do the cross-browser testing. That&#8217;s the way you can ensure the application is running consistently across\u00a0different browsers. We recommend you adopt this tool in regular practice to\u00a0ease test execution.<\/p>\n\n\n\n<p>We&#8217;ve split up this Selenium Grid tutorial into three parts. Each part has multiple steps so the code should remain easy to grasp.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-writing-selenium-grid-webdriver-code-using-testng-in-java\">1- Writing Selenium Grid Webdriver Code using TestNG in Java.<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step-1\">Step-1:<\/h4>\n\n\n\n<p>We will develop a test using TestNG. In the following example, we will launch the browsers on nodes using a remote web driver. It can pass on its capabilities to the driver so that the driver has all the information to execute on Nodes.<\/p>\n\n\n\n<p>The code below will read the browser parameter from the &lt;<em>TestNG.xml<\/em>&gt; file.<\/p>\n\n\n\n<p>Step-1.1: Here is the sample Selenium Grid Webdriver code to run parallel tests.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package seleniumGrid;\n\nimport java.net.MalformedURLException;\nimport java.net.URL;\nimport java.util.concurrent.TimeUnit;\n\nimport org.openqa.selenium.By;\nimport org.openqa.selenium.WebDriver;\nimport org.openqa.selenium.remote.DesiredCapabilities;\nimport org.openqa.selenium.remote.RemoteWebDriver;\nimport org.testng.annotations.AfterTest;\nimport org.testng.annotations.BeforeTest;\nimport org.testng.annotations.Parameters;\nimport org.testng.annotations.Test;\n\npublic class TestGridClass {\n\tpublic WebDriver driver;\n\tpublic String URL, Node;\n\tprotected ThreadLocal&lt;RemoteWebDriver&gt; threadDriver = null;\n\n\t@Parameters(\"browser\")\n\t@BeforeTest\n\tpublic void launchbrowser(String browser) throws MalformedURLException {\n\t\tString URL = \"http:\/\/www.calculator.net\";\n\n\t\tif (browser.equalsIgnoreCase(\"firefox\")) {\n\t\t\tSystem.out.println(\" Executing on FireFox\");\n\t\t\tString Node = \"http:\/\/192.168.1.3:5566\/wd\/hub\";\n\t\t\tDesiredCapabilities cap = DesiredCapabilities.firefox();\n\t\t\tcap.setBrowserName(\"firefox\");\n\n\t\t\tdriver = new RemoteWebDriver(new URL(Node), cap);\n\t\t\t\/\/ Puts an Implicit wait, Will wait for 10 seconds before throwing\n\t\t\t\/\/ exception\n\t\t\tdriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);\n\n\t\t\t\/\/ Launch website\n\t\t\tdriver.navigate().to(URL);\n\t\t\tdriver.manage().window().maximize();\n\t\t} else if (browser.equalsIgnoreCase(\"chrome\")) {\n\t\t\tSystem.out.println(\" Executing on CHROME\");\n\t\t\tDesiredCapabilities cap = DesiredCapabilities.chrome();\n\t\t\tcap.setBrowserName(\"chrome\");\n\t\t\tString Node = \"http:\/\/192.168.1.3:5567\/wd\/hub\";\n\t\t\tdriver = new RemoteWebDriver(new URL(Node), cap);\n\t\t\tdriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);\n\n\t\t\t\/\/ Launch website\n\t\t\tdriver.navigate().to(URL);\n\t\t\tdriver.manage().window().maximize();\n\t\t} else if (browser.equalsIgnoreCase(\"ie\")) {\n\t\t\tSystem.out.println(\" Executing on IE\");\n\t\t\tDesiredCapabilities cap = DesiredCapabilities.chrome();\n\t\t\tcap.setBrowserName(\"ie\");\n\t\t\tString Node = \"http:\/\/192.168.1.3:5568\/wd\/hub\";\n\t\t\tdriver = new RemoteWebDriver(new URL(Node), cap);\n\t\t\tdriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);\n\n\t\t\t\/\/ Launch website\n\t\t\tdriver.navigate().to(URL);\n\t\t\tdriver.manage().window().maximize();\n\t\t} else {\n\t\t\tthrow new IllegalArgumentException(\"The Browser Type is Undefined\");\n\t\t}\n\t}\n\n\t@Test\n\tpublic void calculatepercent() {\n\t\t\/\/ Click on Math Calculators\n\t\tdriver.findElement(By.xpath(\"\/\/a[contains(text(),'Math')]\")).click();\n\t\t\/\/ Click on Percent Calculators\n\t\tdriver.findElement(\n\t\t\t\tBy.xpath(\"\/\/a[contains(text(),'Percentage Calculator')]\"))\n\t\t\t\t.click();\n\t\t\/\/ Enter value 17 in the first number of the percent Calculator\n\t\tdriver.findElement(By.id(\"cpar1\")).sendKeys(\"17\");\n\t\t\/\/ Enter value 35 in the second number of the percent Calculator\n\t\tdriver.findElement(By.id(\"cpar2\")).sendKeys(\"35\");\n\n\t\t\/\/ Click Calculate Button\n\t\tdriver.findElement(\n\t\t\t\tBy.xpath(\"(\/\/input[contains(@value,'Calculate')])[1]\")).click();\n\t\t\/\/ Get the Result Text based on its xpath\n\t\tString result = driver.findElement(\n\t\t\t\tBy.xpath(\".\/\/*[@id='content']\/p[2]\/font\/b\")).getText();\n\t\t\/\/ Print a Log In message to the screen\n\t\tSystem.out.println(\" The Result is \" + result);\n\t\tif (result.equals(\"5.95\")) {\n\t\t\tSystem.out.println(\" The Result is Pass\");\n\t\t} else {\n\t\t\tSystem.out.println(\" The Result is Fail\");\n\t\t}\n\t}\n\n\t@AfterTest\n\tpublic void closeBrowser() {\n\t\t\/\/ driver.quit();\n\t}\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step-2\">Step-2:<\/h4>\n\n\n\n<p>So, you just need\u00a0to copy-paste the above into a Java file in the Eclipse IDE. In the next few steps, we&#8217;ll tell you how to add the &lt;<em>TestNG.xml<\/em>> to your code and what to keep in this file.<\/p>\n\n\n\n<p>We use this file to pass the browser parameter and to specify the test order. So, first of all, create an XML under the project folder in the Eclipse IDE.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-1.2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"648\" height=\"655\" src=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-1.2.png\" alt=\"create an XML\" class=\"wp-image-3709\"\/><\/a><figcaption class=\"wp-element-caption\">Selenium Grid Webdriver Code &#8211; 1.2<\/figcaption><\/figure>\n<\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step-3\">Step-3:<\/h4>\n\n\n\n<p>Select the &lt;<em>File<\/em>&gt; option from the &lt;<em>General<\/em>&gt; menu and press the &lt;<em>Next<\/em>&gt; button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-1.3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"948\" height=\"651\" src=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-1.3.png\" alt=\"Selenium Grid Webdriver Code - 1.3\" class=\"wp-image-3710\"\/><\/a><figcaption class=\"wp-element-caption\">Selenium Grid Webdriver Code &#8211; 1.3<\/figcaption><\/figure>\n<\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step-4\">Step-4:<\/h4>\n\n\n\n<p>Enter the name of the file as &lt;<em>GridDemo.xml<\/em>&gt; and press the &lt;<em>Finish<\/em>&gt; button. By default, its name is&nbsp;&lt;<em>TestNG.xml<\/em>&gt;.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-1.4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"523\" height=\"595\" src=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-1.4.png\" alt=\"Name the file as GridDemo.xml\" class=\"wp-image-3711\"\/><\/a><figcaption class=\"wp-element-caption\">Selenium Grid Webdriver Code &#8211; 1.4<\/figcaption><\/figure>\n<\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step-5\">Step-5:<\/h4>\n\n\n\n<p>You can now see that file &lt;<em>GridDemo.xml<\/em>&gt; gets created under the project folder as shown below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-1.5.png\"><img loading=\"lazy\" decoding=\"async\" width=\"282\" height=\"308\" src=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-1.5.png\" alt=\"Selenium Grid Webdriver Code - 1.5\" class=\"wp-image-3712\"\/><\/a><figcaption class=\"wp-element-caption\">Selenium Grid Webdriver Code &#8211; 1.5<\/figcaption><\/figure>\n<\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step-6\">Step-6:<\/h4>\n\n\n\n<p>In this section, you can view the contents of the XML file. Please check that we&#8217;ve added three tests and put them in a suite. Also, we mentioned &lt;<em>parallel=&#8221;tests&#8221;<\/em>&gt;. So, all the tests could run in parallel.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;!DOCTYPE suite SYSTEM \"http:\/\/testng.org\/testng-1.0.dtd\"&gt;\n&lt;suite name=\"Suite\" parallel=\"tests\"&gt;\n\n   &lt;test name=\"FirefoxTest\"&gt;\n   &lt;parameter name=\"browser\" value=\"firefox\" \/&gt;\n      &lt;classes&gt;\n         &lt;class name=\"seleniumGrid.TestGridClass\" \/&gt;\n      &lt;\/classes&gt;\n   &lt;\/test&gt;\n\n   &lt;test name=\"ChromeTest\"&gt;\n   &lt;parameter name=\"browser\" value=\"chrome\" \/&gt;\n      &lt;classes&gt;\n         &lt;class name=\"seleniumGrid.TestGridClass\" \/&gt;\n      &lt;\/classes&gt;\n   &lt;\/test&gt;\n\n   &lt;test name=\"IETest\"&gt;\n   &lt;parameter name=\"browser\" value=\"ie\" \/&gt;\n      &lt;classes&gt;\n         &lt;class name=\"seleniumGrid.TestGridClass\" \/&gt;\n      &lt;\/classes&gt;\n   &lt;\/test&gt;\n   \n&lt;\/suite&gt;<\/pre>\n\n\n\n<p>You&#8217;ve now completed the development part of the Selenium Grid Webdriver code. So, what remains is the execution of the tests on multiple browsers. Check out the second part to see the test execution.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-test-execution\">2- Test Execution.<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step-1-0\">Step-1:<\/h4>\n\n\n\n<p>Select the created XML i.e. &lt;<em>GridDemo.xml<\/em>>. Right-click and choose the &lt;<em>Run As >> TestNG Suite<\/em>> option.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-2.1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"774\" height=\"651\" src=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-2.1.png\" alt=\"choose the Run As &gt;&gt; TestNG Suite&gt; option.\" class=\"wp-image-3713\"\/><\/a><figcaption class=\"wp-element-caption\">Selenium Grid Webdriver Code &#8211; 2.1<\/figcaption><\/figure>\n<\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step-2-0\">Step-2:<\/h4>\n\n\n\n<p>Now open the Node where we have launched all the browsers. You will see all of them running simultaneously.<\/p>\n\n\n\n<p><strong><em>Note: For setting up the hub node, please check our earlier post on <a href=\"\/download-selenium-grid-set-multiple-browsers\/\" target=\"_blank\" rel=\"noopener\">download and use Selenium Grid to run multiple browsers<\/a>.<\/em><\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-2.2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"851\" height=\"490\" src=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-2.2.png\" alt=\"Selenium Grid Webdriver Code - 2.2\" class=\"wp-image-3714\"\/><\/a><figcaption class=\"wp-element-caption\">Selenium Grid Webdriver Code &#8211; 2.2<\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-result-analysis\">3- Result Analysis.<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step-1-1\">Step-1:<\/h4>\n\n\n\n<p>After successfully completing the test execution, the result summary gets printed in the console as you can view from the below snapshot.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-3.1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"572\" height=\"289\" src=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-3.1.png\" alt=\"Test execution, the result summary\" class=\"wp-image-3715\"\/><\/a><figcaption class=\"wp-element-caption\">Selenium Grid Webdriver Code &#8211; 3.1<\/figcaption><\/figure>\n<\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step-2-1\">Step-2:<\/h4>\n\n\n\n<p>Next, to see the status of all tests, Navigate to the &lt;<em>Results of Running Suite<\/em>&gt; Tab. Here, TestNG displays the result summary&nbsp;in the following manner.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-3.2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"505\" height=\"382\" src=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-3.2.png\" alt=\"Selenium Grid Webdriver Code - 3.2\" class=\"wp-image-3716\"\/><\/a><figcaption class=\"wp-element-caption\">Navigate to the Test Results<\/figcaption><\/figure>\n<\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step-3-0\">Step-3:<\/h4>\n\n\n\n<p>Finally, let&#8217;s check out the auto-generated &lt;<em>TestNG<\/em>&gt; report. With this, we&#8217;ll be able to see the test results in HTML format.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-3.3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"400\" src=\"\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-3.3.png\" alt=\"Auto-generated TestNG report\" class=\"wp-image-3717\"\/><\/a><figcaption class=\"wp-element-caption\">TestNG Report<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-footnote-nbsp-selenium-grid-webdriver-code-example\">Footnote &#8211;&nbsp;Selenium Grid Webdriver Code Example<\/h2>\n\n\n\n<p>We hope the above tutorial will help you in using Selenium Grid in your environment. And you&#8217;ll be able\u00a0to speed up the test execution by running tests in parallel using Selenium Grid. Also, if you decide to use\u00a0Selenium for load testing, then please share your experience\u00a0after you implement it.<\/p>\n\n\n\n<p>Hope, you succeed in reaping the full benefits from reading our latest post on the Selenium Webdriver and Selenium Grid.<\/p>\n\n\n\n<p>Thanks to you all for visiting. Please drop your feedback in the comment box section.<\/p>\n\n\n\n<p><strong>All the Best,<\/strong><\/p>\n\n\n\n<p><strong>TechBeamers.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our\u00a0earlier post on Selenium Grid, we explained an easier method to download and install the\u00a0Selenium Grid. Also, the post did teach you how to configure multiple browsers like Firefox, Chrome, and IE with Selenium Grid. In today&#8217;s tutorial, you&#8217;ll get to learn how to run parallel tests with Selenium Grid Webdriver. And since we&#8217;ll [&hellip;]<\/p>\n","protected":false},"author":321,"featured_media":3719,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_post_was_ever_published":false},"categories":[76],"tags":[308],"class_list":{"0":"post-3703","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-selenium-tutorial","8":"tag-webdriver-exercises"},"jetpack_featured_media_url":"https:\/\/techbeamers.com\/wp-content\/uploads\/2016\/07\/Selenium-Grid-Webdriver-Code-in-Java.png","_links":{"self":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/3703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/comments?post=3703"}],"version-history":[{"count":0,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/3703\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/media\/3719"}],"wp:attachment":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/media?parent=3703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/categories?post=3703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/tags?post=3703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}