Friday, September 2, 2011

Selenium Grid with Webdriver

Recently I gone through the webdriver and then I thought to configure it with Selenium grid. On selenium grid site they have mention how to configure it but it's not in details so i though to write that on this blog.
To configure webdriver with Selenium grid you require grid2.

Prerequisites.
  • TestNG
  • Grid2
    Download the selenium-server-standalone-version.jar from the below location.
    Download Grid2

We will see two things in Selenium Grid.
  1. Run Testcase in parallel
  2. Run same testcase on different browser in parallel for browser compatibility testing.

Run Testcases in parallel.

In this example we will run testcases of GridWithWebdriver and GridWithWebdriver1 class on Internet Explorer.

GridWithWebdriver.java
/**
 * @author Gaurang Shah
 * To Demonstrate how to configure webdirver with Selenium Grid
 */
import java.net.MalformedURLException;
import java.net.URL;

import org.junit.AfterClass;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;

public class GridWithWebdriver {
 
 public WebDriver driver;
 
 @Parameters({"browser"})
 @BeforeClass
 public void setup(String browser) throws MalformedURLException {
  DesiredCapabilities capability=null;
   
  if(browser.equalsIgnoreCase("firefox")){
   System.out.println("firefox");
   capability= DesiredCapabilities.firefox();
   capability.setBrowserName("firefox"); 
   capability.setPlatform(org.openqa.selenium.Platform.ANY);
   //capability.setVersion("");
  }
 
  if(browser.equalsIgnoreCase("iexplore")){
   System.out.println("iexplore");
   capability= DesiredCapabilities.internetExplorer();
   capability.setBrowserName("iexplore"); 
   capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
   //capability.setVersion("");
  }
  
  driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
  driver.navigate().to("http://google.com");
  
 }
 
 @Test
 public void test_first() throws InterruptedException{
  Thread.sleep(3000);
  WebElement search_editbox = driver.findElement(By.name("q"));
  WebElement search_button = driver.findElement(By.name("btnG"));
  search_editbox.clear();
  search_editbox.sendKeys("first");
  search_button.click();
 }
 
 @Test
 public void test_second(){
  WebElement search_editbox = driver.findElement(By.name("q"));
  WebElement search_button = driver.findElement(By.name("btnG"));
  search_editbox.clear();
  search_editbox.sendKeys("second");
  search_button.click();
 }
 
 @AfterClass
 public void tearDown(){
        driver.quit();
 }
}
GridWithWebdriver1.java
/**
 * @author Gaurang Shah
 * To Demonstrate how to configure webdirver with Selenium Grid
 */
import java.net.MalformedURLException;
import java.net.URL;

import org.junit.AfterClass;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;

public class GridWithWebdriver1 {
 
 public WebDriver driver;
 
 @Parameters({"browser"})
 @BeforeClass
 public void setup(String browser) throws MalformedURLException, InterruptedException {
  DesiredCapabilities capability=null;
   
  if(browser.equalsIgnoreCase("firefox")){
   System.out.println("firefox");
   capability= DesiredCapabilities.firefox();
   capability.setBrowserName("firefox"); 
   capability.setPlatform(org.openqa.selenium.Platform.ANY);
   //capability.setVersion("");
  }
 
  if(browser.equalsIgnoreCase("iexplore")){
   System.out.println("iexplore");
   capability= DesiredCapabilities.internetExplorer();
   capability.setBrowserName("iexplore"); 
   capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
   //capability.setVersion("");
  }
  
  driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
  driver.navigate().to("http://google.com");
  
 }
 
 @Test
 public void test_third() throws InterruptedException{
  Thread.sleep(3000);
  WebElement search_editbox = driver.findElement(By.name("q"));
  WebElement search_button = driver.findElement(By.name("btnG"));
  search_editbox.clear();
  search_editbox.sendKeys("third");
  search_button.click();
  
 }
 
 @Test
 public void test_fourth(){
  WebElement search_editbox = driver.findElement(By.name("q"));
  WebElement search_button = driver.findElement(By.name("btnG"));
  search_editbox.clear();
  search_editbox.sendKeys("foruth");
  search_button.click();
  
 }
 
 @AfterClass
 public void tearDown(){
        //Close the browser
     //   driver.quit();

 }
}

Testng.xml
<suite name="Selenium Grid with webdriver" verbose="3"  parallel="classes" thread-count="2">   
  <test name="Selenium Grid Demo">
  <parameter name="browser" value="iexplore"/>
    <classes>
      <class name="tests.GridWithWebdriver"/>
   <class name="tests.GridWithWebdriver1"/>
    </classes>
 </test>
 </suite>

Now in order to run using testng.xml we need to start the Grid hub and two remote control with Iexplore as browser.

Now to start the Grid and remote control open CMD and navigate to folder where Grid2 jar (selenium-server-standalone-x.x.x.jar) file is located and use the following commands

To start the Grid Hub
java -jar selenium-server-standalone-2.5.0.jar -role hub
To start the remote control supporting Internet explore
java -jar selenium-server-standalone-2.5.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=iexplore,platform=WINDOWS
To start another the remote control supporting Internet explore
java -jar selenium-server-standalone-2.5.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=iexplore,platform=WINDOWS -port 5556

After this open http://localhost:4444/grid/console in you browser. You should be able to see something like below image.



Now if you will run using testng.xml you will see two Internet explorer will open and one will execute test_first() and test_second() while another will execute test_third() and test_fourth() in parallel.

Run same testcases on different browser in parallel.

In this example we will execute GridWithWebdriver tests on Firefox and Internet Explorer in parallel.

To do so you need to modify you testng.xml as below.
Testng.xml
<suite name="Same TestCases on Different Browser" verbose="3"  parallel="tests" thread-count="2">   
  <test name="Run on Internet Explorer">
 <parameter name="browser"  value="iexplore"/>
    <classes>
      <class name="Tests.GridWithWebdriver"/>
    </classes>
 </test>  

  <test name="Run on Firefox">
 <parameter name="browser"  value="firefox"/>
    <classes>
      <class name="Tests.GridWithWebdriver"/>
    </classes>
 </test>  
 </suite>
Now to run testcases we need to start grid hub and one remote control supporting Firefox and another supporting Internet Explorer.

Now to start the Grid and remote control open CMD and navigate to folder where Grid2 jar (selenium-server-standalone-x.x.x.jar) file is located and use the following commands
To start the Grid Hub
java -jar selenium-server-standalone-2.5.0.jar -role hub
To start the remote control supporting Firefox
java -jar selenium-server-standalone-2.5.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=firefox,platform=WINDOWS
To start another the remote control supporting Internet explore
java -jar selenium-server-standalone-2.5.0.jar -role webdriver -hub http://localhost:4444/grid/register -browser browserName=iexplore,platform=WINDOWS -port 5556
After this open http://localhost:4444/grid/console in you browser. You should be able to see something like below image.

Now if you will run above test case one Firefox and one IE will launch and both test_first() and test_second() will execute in both browser simultaneously.

Wednesday, August 31, 2011

Selenium Grid for Browser Compatibility Testing

There are two way things we can use Selenium Gird for.


so let's see how to use selenium grid for browser compatibility testing.

There are two way we can make this happen
  1. Using different browsers on same machine.
  2. Using different browsers on different machines.

Using different browsers on same machine.
In this example we will use SeleniumGridDemo.java file, It has two methods test_first() and test_second() which we will execute on Safari and Internet Explorer in parallel.

SeleniumGridDemo.java
/**
 * @author Gaurang Shah
 * To demonstrate the Selenium Grid 
 */
import org.testng.annotations.*;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class SeleniumGridDemo {

 public Selenium selenium;

 @Parameters( { "browser" })
 @BeforeClass
 public void setup(String browser) {
  selenium = new DefaultSelenium("localhost", 4444, browser,"http://google.com");
  selenium.start();
 }

 @AfterClass
 public void tearDown() {
  selenium.stop();
 }

 @Test
 public void test_first() {
  selenium.open("/");
  selenium.type("q", "First");
  selenium.click("btnG");
 }

 @Test
 public void test_second() {
  selenium.open("/");
  selenium.type("q", "second");
  selenium.click("btnG");
 }

}
testng.xml
<suite name="Same TestCases on on same machine on different Browser" verbose="3"  parallel="tests" thread-count="2"> 
  <test name="Run on Firefox">
 <parameter name="browser"  value="*safari"/>
    <classes>
      <class name="SeleniumGridDemo1"/>
    </classes>
 </test>
  <test name="Run on IE">
 <parameter name="browser"  value="*iexplore"/>
    <classes>
      <class name="SeleniumGridDemo1"/>
    </classes>
 </test>
</suite>

Now you can run this using eclipse IDE or you can write down the ANT build file as follows.

build.xml
<project name="demo" default="run" basedir=".">

 <property name="classes.dir" value="bin" />
 <property name="src.dir" value="src" />
 <property name="report.dir" value="reports" />
 
 <path id="libs">
  <fileset dir="src\Libs\">
   <include name="*.jar"/>
  </fileset>
  <pathelement path="${basedir}\${classes.dir}"/>
 </path>

 <target name="run">
  <antcall target="init"/>
  <antcall target="compile"/>
  <antcall target="runTestNG"/>
  </target>
 
 <!-- Delete old data and create new directories -->
 <target name="init" >
  <echo>Initlizing...</echo>
  <delete dir="${classes.dir}" />
  <mkdir dir="${classes.dir}"/>
  <delete dir="${report.dir}" />
  <mkdir dir="${report.dir}"/>
  <mkdir dir="${logs.dir}"/>
 </target>

 <!-- Complies the java files -->
 <target name="compile">
  <echo>Compiling...</echo>
  <javac debug="true" srcdir="${src.dir}" destdir="${classes.dir}"   classpathref="libs" />
 </target>

 <target name="runTestNG">
  <taskdef resource="testngtasks" classpathref="libs"/>
 <testng outputDir="${report.dir}" 
   haltonfailure="false"
   useDefaultListeners="true"
   classpathref="libs">
 <xmlfileset dir="${basedir}" includes="testng.xml"/>
 </testng>
 </target>
 </project>


Now in order to run testcases in parallel we need to start grid hub and selenium remote controls. As we have specified two threads in testng.xml we will start two remote controls.
To start grid hub and remote control, Open command prompt, go to selenium grid directory and give following commands.

to stat the grid hub
ant launch-hub
To Start the selenium remote control for Internet explorer
ant launch-remote-control -Denvironment=*safari
To Start the selenium remote control for Internet explorer on different port
ant launch-remote-control -Denvironment=*iexplore -Dport=5556
After this open http://localhost:4444/console. In this you should be able to see both the remote controls under Available Remote Controls section as appears in below image.

Using different browsers on different machines
To run the testcases on different machine we don't require to change in testng.xml or java file we just need to start two remote control on two different machines.

We will start Remote control for safari browser on local machine and for IE browser on remote machine.
To start grid hub and remote control, Open command prompt, go to selenium grid directory and give following commands.

to stat the grid hub
ant launch-hub
To Start the selenium remote control for Internet explorer
ant launch-remote-control -Denvironment="*safari"

Now Login into another machine and from command prompt navigate to selenium grid directory and fire following command
ant launch-remote-control -DhubURL=http://172.29.72.185:4444/ -Denvironment=*iexplore
In above command 172.29.72.185 is the IP address where my Selenium grid Hub is running.

After this open http://localhost:4444/console. In this you should be able to see both the remote controls under Available Remote Controls section as appears in below image.