Saturday, December 7, 2013

Parallel execution of WebDriver test using Selenium Grid2 on multiple machines.

Guide to Setup Selenium Grid2

Objective:-

Intent of this document to use Selenium Grid2 to execute Selenium WebDriver test scripts on multiple browsers running on multiple physical/virtual machines.

Some Terminology need to know:-

  • Hub – Implies selenium grid2 hub, which can only be one in our case & it will distribute test among multiple nodes registered to it at runtime.
  • Node – One node represents one machine where test will be running & we can have multiple nodes those will be registered with one centralized hub which will be running on anyone machine.
Selenium-server-standalone jar:-


Note: - Have used selenium-server-standalone-2.25.0.jar here, You can download latest available version jar.

JSON configuration file creation:-

  • Create a text file named as “webconfig.txt”
  • Open that created “webconfig.txt” file & Copy following content in to that.

{
"capabilities":
                [
                {
                                "browserName": "firefox",
                                "acceptSslCerts": true,
                                "javascriptEnabled": true,
                                "takeScreenshots": true,
                                "firefox_profile": "",
                                "maxInstances": 3,
                                "seleniumProtocol": "WebDriver"
                },
                {
                                "browserName": "chrome",
                                "maxInstances": 3,
                                "seleniumProtocol": "WebDriver"
                },
                {
                                "platform": "WINDOWS",
                                "browserName": "internet explorer",
                                "maxInstances": 4,
                                "seleniumProtocol": "WebDriver"
                }
                ],
  "configuration":
                {
                                "cleanUpCycle": 2000,
                                "timeout": 30000,
                                "proxy": "org.openqa.grid.selenium.proxy.WebDriverRemoteProxy",
                                "maxSession": 5,
                                "port": 5555,
                                "host": noiam003ag-w1,
                                "register": true,
                                "hubPort": 4444,
                                "hubHost" : noiam003ag-w1
                }
}

  • Now after copying above content in the file, Save it.
  • Make sure both ‘selenium-server-standalone-2.25.0.jar’ and ‘webconfig.txt’ are in the same directory.
Configurable parameters in the “webconfig.txt”:-

  • In capabilities section we can have sections for browsersName & ‘maxInstances’ parameter value can be changed as per the required number of instances of that particular browser on that machine.
  • In configuration section, ‘port’ defines port with which a node will be registered to the hub & each node will be registered with different port.
  • In configuration section, ‘host’ defines machine_name/machine_IP on which that node is running & one machine can have only one node running on.
  • In configuration section, ‘hubPort’ defines port where hub is running on anyone machine among all test machine/VMs & port “4444” (i.e. default port to run hub) will remain same when we will have ‘webconfig.txt’ file on any node machine only node configurations will be changed.
  • In configuration section, ‘hubHost’ defines machine_name/machine_IP where hub is running on anyone machine among all test machine/VMs & its value will remain same when we will have ‘webconfig.txt’ file on any node machine only node configurations will be changed.
Copying both ‘selenium-server-standalone-2.25.0.jar’ and ‘webconfig.txt’ on number of machines required for testing:-

  • Before copying ‘webconfig.txt’ on multiple machine, Set ‘hubPort’ and ‘hubHost’ parameters value to the port ‘4444’ and the ‘machine_name’ respectively.
  • Copy both files on all machines/VMs under the same directory.
  • After copying update ‘port’ and ‘host’ parameter values accordingly (i.e. port value will be 5555, 5556, 5557 …in sequence for each node on a machine & host value will be the name of each machine_name where node will running).
Running Hub & Nodes:-

  1. On first machine, open command prompt.
  2. Navigate to folder location where both ‘selenium-server-standalone-2.25.0.jar’ and ‘webconfig.txt’ files are available.
  3. Now run below command to run Selenium Grid 2 Hub on that machine:-
java -jar selenium-server-standalone-2.25.0.jar -role hub -hubConfig webconfig.txt
Command prompt looks as below:-

  1. Open another command prompt if you want to use same as a node also.
  2. Navigate to folder location where both ‘selenium-server-standalone-2.25.0.jar’ and ‘webconfig.txt’ files are available.
  3. Run below command to run node on that machine:-
java -jar selenium-server-standalone-2.25.0.jar -role wd –hub http://put_hub_machine_name_here:4444/grid/register -port 5555 -nodeConfig webconfig.txt
(Note: - Don’t forget to enter the “hub_machine_name” in above command)

Command prompt should appear as below:-

  1. Follow the steps 4 to 6 on each machine where you want to run the node, only you need to change the node port from ‘5555’ to ‘5556’, ‘5557’ …. on each machine in the same sequence.
Running Grid2 console in browser:-

  • Open the browser, Enter the following URL:-
http://put_hub_machine_name:4444/grid/console
(Note: - Don’t forget to enter the “hub_machine_name” in above URL)
  • Web page will be showing number of nodes running.
  • Following screen would appear on the browser.

Here we have two nodes running on two different nodes with number of browser instances (i.e. configured in webconfig.txt file) available.

Make sure BaseTestSetup.java class has setup method as below:-

Use following setup method with your WebDriver scripts to manage multiple browser with Grid2:-

public WebDriver setup(String BROWSER) throws MalformedURLException{
            System.out.println("Browser: " + BROWSER);
           
            if (BROWSER.equals("FF")) {
                  System.out.println("FF is selected");
                  capability = DesiredCapabilities.firefox();
                  capability.setBrowserName("firefox");
                  capability.setPlatform(org.openqa.selenium.Platform.ANY);
                  driver = new RemoteWebDriver(new URL("http://noiam003ag-w1:4444/wd/hub"), capability);
                  driver.get(baseURL);
            } else if (BROWSER.equals("IE")) {
                  System.out.println("IE is selected");
                  capability = DesiredCapabilities.internetExplorer();
                  capability.setBrowserName("internet explorer");
                  capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
            } else if (BROWSER.equals("CH")){
                  System.out.println("Google chrome is selected");
                  System.setProperty("webdriver.chrome.driver", "C:/ChromeDriver/chromedriver.exe");
                  capability = DesiredCapabilities.chrome();
                  capability.setBrowserName("chrome");
                  capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
                  driver = new RemoteWebDriver(new URL("http://noiam003ag-w1:4444/wd/hub"), capability);
                  driver.get(baseURL);
            }
            System.out.println("Remote Driver setup");
            return driver;
}

Running testScripts on those nodes:-

Now create TestNG.xml with following content and run as TestNG Suite then after Grid Hub will distribute the test among nodes & their available browser instances.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Selenium WebDriver Training" parallel="tests" thread-count="20">>
     
      <test name="WebDriverDemo with Opera" preserve-order="true">
            <parameter name="BROWSER" value="OPERA"></parameter>
            <classes>
                  <!--class name="com.pb.selenium.training.testScripts.GoogleTest"></class-->
            </classes>
      </test>

      <test name="WebDriverDemo With FF" preserve-order="true">
            <parameter name="BROWSER" value="FF" />
            <classes>
                  <class name="com.pb.selenium.training.testScripts.GoogleTest"></class>
                  <class name="com.pb.selenium.training.testScripts.GridSample"></class>
            </classes>
      </test>

      <test name="WebDriverDemo with IE" preserve-order="true">
            <parameter name="BROWSER" value="IE"></parameter>
            <classes>
                  <!--class name="com.pb.selenium.training.testScripts.GoogleTest"></class-->
            </classes>
      </test>

      <test name="WebDriverDemo with Google Chrome" preserve-order="true">
            <parameter name="BROWSER" value="CH"></parameter>
            <classes>
                  <class name="com.pb.selenium.training.testScripts.GoogleTest"></class>
                  <class name="com.pb.selenium.training.testScripts.GridSample"></class>
            </classes>
      </test>
</suite>

17 comments:

  1. Visit www.toolsqa.com for Selenium Learning with Practice Exercises and detailed explanations.

    ReplyDelete
  2. Hi Amit,
    I'm having problems in setting up the Base class could you please send the full base class code

    Thanks

    ReplyDelete
    Replies
    1. Hello Vamshi,

      Send in you class file, will try to rectify it.

      Delete
    2. Hi Amit,
      please could you send me a test mail to: vamshi9006@gmail.com, In reply I can send you the code.

      Delete
  3. Hi Amit,

    I am getting the error that is Couldn't register this node: Error sending the registration request: The hub responded with 500:Server Error. Could you please tell me how to resolve this.

    ReplyDelete
  4. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn aboutSelenium

    ReplyDelete
  5. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium. PHP Training Institute in Chennai>

    Best PHP Training in Chennai>

    ReplyDelete
  6. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  7. Such as very excellent information, it helps us to improve our mind knowledge. Keep on updating like this information!!artificial intelligence course

    ReplyDelete
  8. You shared the very useful information with us. For more topics and more data go to All Writers Destination. Where the guest posting facility is available.

    ReplyDelete
  9. "ExcelR is a global leader delivering a wide gamut of management and technical training over 40 countries. We are a trusted training delivery partner of 350+ corporate clients and universities across the globe with 140,000+ professionals trained across various courses.
    Best Data Science Courses in Pune "

    ReplyDelete
  10. I am impressed by the information that you have on this blog. It shows how well you understand this subject.
    data science course in varanasi

    ReplyDelete
  11. Infycle Technologies in Chennai offers the leading Big Data Hadoop Training in Chennai for tech professionals and students at the best offers. In addition to the Python course, other in-demand courses such as Data Science, Big Data Selenium, Oracle, Hadoop, Java, Power BI, Tableau, Digital Marketing also will be trained with 100% practical classes. Dial 7504633633 to get more info and a free demo.

    ReplyDelete