Tuesday, September 23, 2014

Breaking The Parallel Execution Jinx Using Selenium Grid

We know that automation is cumbersome task and selenium grids come in handy when we talk about parallel test script execution. It supports execution of same code at same time in multiple browsers.

Here we will be using “VisGrid”; it’s a GUI for selenium Grid. You can start hub, create and attach a selenium node very easily and quickly.

Let’s see how to execute our test automation scripts in parallel in different browsers using VisGrid.

Below is the UI for the VisGrid:


Here we can set the Port number for Hub and click on Start Hub button to start the Hub. After that we can create nodes that will comprise of different sets of browser which we want to use.

Step 1: Create a TestNg project using Eclipse. Include the desired jar files in the build path. Create a java class file and follow the code. Here we are invoking the browsers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class testBrowser {
      private WebDriver driver= null;
      private String baseUrl= "http://www.google.co.in/";
      private StringBuffer verificationErrors = new StringBuffer();
      @BeforeMethod
      @Parameters("browserName")
      public void setUp(String browserName) throws MalformedURLException {
            //Invoking IE browser
          if (browserName.equalsIgnoreCase("iexplorer")) {
             URL url=new URL("http://localhost:4444/wd/hub");
             DesiredCapabilities capability=new DesiredCapabilities();
                     capability.setBrowserName("internet explorer");
             driver=new RemoteWebDriver(url,capability);
             System.out.println("Executing in IE");
          }
            //Invoking Firefox browser
          if (browserName.equalsIgnoreCase("Firefox")) {
              URL url=new URL("http://localhost:4444/wd/hub");
              DesiredCapabilities capability=new DesiredCapabilities();
              capability.setBrowserName("firefox");
              driver=new RemoteWebDriver(url,capability);
              System.out.println("Executing in FF");
          }
            //Invoking Chrome browser
          if (browserName.equalsIgnoreCase("Chrome")) {
              URL url=new URL("http://localhost:4444/wd/hub");
              DesiredCapabilities capability=new DesiredCapabilities();
              capability.setBrowserName("Chrome");
              driver=new RemoteWebDriver(url,capability);
              System.out.println("Executing in Chrome");
              }
          }
Here is the @Test method which will be executed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Test
       public void testF() throws Exception {
       driver.get(baseUrl + "/");
       try
        {
          AssertJUnit.assertEquals("Google", driver.getTitle());
        } catch (Error e)
        {
          verificationErrors.append(e.toString());
        }
        WebElement onElement = driver.findElement(By.xpath("//*[@id='hplogo']"));
        if (("Google").equals(onElement.getAttribute("title")))
        System.out.println("Tooltip is : " + onElement.getAttribute("title"));
        else
            System.out.println("tooltip shown is wrong");
      }
Step 2: Do the configuration in the “testng.xml” file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="3" name="Suite" parallel="tests">
  <test name="Test on FF">
  <parameter name="browserName" value="Firefox" />
    <classes>
      <class name="pack.testBrowser"/>
    </classes>
  </test> <!-- Test -->
   <test name="Test on IE">
    <parameter name="browserName" value="iexplorer" />
    <classes>
      <class name="pack.testBrowser"/>
    </classes>
  </test> <!-- Test -->
     <test name="Test on Chrome">
    <parameter name="browserName" value="Chrome" />
    <classes>
      <class name="pack.testBrowser"/>
    </classes>
  </test> <!-- Test -->
 </suite> <!-- Suite →
Step 3: Execute the testng.xml file and enjoy the script execution on multiple browsers.

The above paradigm can save lots of time by executing the scripts on multiple browsers simultaneously.