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>