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.

Monday, August 29, 2011

Selenium Grid

Selenium grid is basically used to run the testcases in parallel. There are two things you can do using selenium grid.
1. You can run the testcases in parellel to save the execution time.
2. You can run the same testcases on different browsers in parellel to test for the browser compatibility.

So let's see how to do it.

Run Testcases of different classes in parallel on same machine.
In this example we have to classes SeleniumGridDemo1 and SeleniumGridDemo2. SeleniumGridDemo1 class has two methods test_first() and test_second(). SeleniumGridDemo2 also has two methods test_third() and test_fourth().

By the following configuration test_first() and test_second() will run in parallel with test_third() and test_fourth().

SeleniumGridDemo1.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 SeleniumGridDemo1 {

	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");
	}

}

SeleniumGridDemo2.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 SeleniumGridDemo2 {
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_third() { 
		selenium.open("/");
		selenium.type("q","third");
		selenium.click("btnG");
	}	
	@Test
	public void test_fourth() {
		selenium.open("/");
		selenium.type("q","fourth");
		selenium.click("btnG");
	}

}

TestNG.xml
<suite name="parelledSuite" verbose="3"  parallel="classes" thread-count="2">   
  <test name="Selenium Gird Demo">
  <parameter name="browser" value="*iexplore"/>
    <classes>
      <class name="SeleniumGridDemo1"/>
	  <class name="SeleniumGridDemo2"/>
    </classes>
 </test>
 </suite>

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="*iexplore"
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 you both the remote controls under Available Remote Controls section as appears in below image.



Now you can run the same using testn.xml from you eclipse any other IDE or you can write down ANT file.

<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>

Run Testcases of different classes in parallel on different machine
In this example we will run the same testcases in SeleniumGridDemo1 and SeleniumGridDemo2 classes. We don't need to change in testng.xml or any other file.

We just need to start one remote control on local machine and another remote control on another 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="*iexplore"

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.


Wednesday, August 3, 2011

WATIR framework

What I am going to discuss here is a standard Page Object Pattern Framework for WATIR.

What each and every framework requires are, Driver Script, Object repository, Script to read data from Data source(EXCEL, CSV or any other), Files to generate and store reports and finally Test Files.

Download Project

The framework designed by me contains following things
Xunit framework test-unit
Data Sources: EXCEL and YAML
Reports: I am using ci_reporter with JUnit ANT target to generate HTML report
Driver Script: BATCH file which will run tests according to parameter passed and will generate the report.
Object Repository: Ruby class per page.

Now let's see everything in details.

Test Script:LoginTestWithYAML.rb inside Test Folder
require 'rubygems'

gem 'test-unit'
require 'test\unit'
require 'watir'
require_relative '../Libs/BaseClassInstanceMethods'
require_relative '../Libs/BaseClassClassMethods'
require_relative '../ObjRepository/LoginPage'
require_relative '../Libs/ReadYAML'

class LoginTestWithYAML < Test::Unit::TestCase
  include ReadYAML
  include LoginPage
  include BaseClassInstanceMethods
  extend BaseClassClassMethods
  def test_gmail
    username_textbox.set(readData("noPassword","username"))
    password_textbox.set(readData("noPassword","password"))
    login_button.click()
    assert(($browser.text.include? "Enter your password."),"Verify Error Messages")
  end
end
ReadYAML.rb inside Libs folder
require 'yaml'

=begin
Author: Gaurang
read the YAML file based on the given scenario name and fieldname
=end

module ReadYAML
  @@config = YAML.load_file("#{File.dirname(__FILE__)}/../data/gmail.yaml")
  def readData(scenarioName, fieldName)
    if (@@config[scenarioName][fieldName].nil?) then
      return ""
    else
      return @@config[scenarioName][fieldName]
    end

  end
end
Object repository: LoginPage.rb inside ObjRepository folder
=begin
Author: Gaurang
Contains all the elements on the login page
=end

module LoginPage
  def username_textbox
    return $browser.text_field(:id,"Email")
  end

  def password_textbox
    return $browser.text_field(:id,"Passwd")
  end

  def login_button
    return $browser.button(:id,"signIn")
  end
end
ANT Target to generate HTML report
http://qtp-help.blogspot.com/2011/07/watir-html-report.html

And finally our driver scripts: run.rb
require 'rubygems'
gem 'test-unit'
require 'test/unit'
gem 'ci_reporter'
require 'ci/reporter/rake/test_unit_loader.rb'
=begin
Author: Guarang
Driver file which would run all the tests inside Test Folder.
=end

class Run

  if (ARGV.length >= 1 ) then
    if (ARGV[0].downcase == "smoke") then
      p "smoke"
      folder= "Tests/Smoke/*.rb"
    elsif  (ARGV[0].downcase == "regression") then
      p "regression"
      folder= "Tests/Regression/*.rb"
    end
  else
    p "default"
    folder= "Tests/*.rb"
  end
 
  Dir[folder].each do |test|
    p test
    require_relative test
  end

end
rub.bat
If you want to run all the tests inside Tests\Smoke folder give enter the following command on command prompt
run smoke
for regression enter the following command
run regression
if you will not provide any argument it will run all the tests under Tests folder.
@echo off 
@echo "initializing..."
rd /s/q Test
@echo "Running Tests..."
ruby run.rb %1
@echo "Generating reports..."
start cmd /C ant
rem wait for 6 seconds
PING 1.1.1.1 -n 1 -w 6000 >NUL
@echo "Opening report..."
start test/reports/junit-noframes.html"