Monday, 23 May 2011

Example - Selenium Grid running simultaneously on two different browsers

Aim:
Demonstrate how Selenium Grid can be used to run simultaneously against two browsers.

Difficulty Level:
High

Prerequisites
  • Created project directory should be available under C:\WorkshopFiles folder on PC
  • Selenium Grid server downloaded and running
  • Ant, JAVA should be installed and should work properly
  • TestNG installed and working correctly
What to users learn?
Users can learn how to execute TestNG scripts on the multiple browsers in parallel.


Tips:
  • Some Basis knowledge of Ant, Selenium Grid is good in understanding the example.
  • Some basis working knowledge of TestNG is good in understanding the Project's directory.
Conclusions:

  • Internet explorer and firefox opens up simultaneously and the same selenium Script is executed
  • Selenium Grid jar files are utilised in the library
  • Selenium Grid Console shows the execution status
  • TestNG provides different methods which make it easier to execute across different browsers simultaneously.
  • Ant is used to launch Selenium Grid

Solution:
Key Steps
  1. Launch the Selenium Grid using Command: >ant launch-hub
  2. Launch the Browser and open the URL: http://localhost:4444/console
  3. Now launch the instances of browser from following command:
  • Ant –Dport=5555 –Denvironment=”*firefox” launch-remote-control
  • Ant –Dport=5556 –Denvironment=”*googlechrome” launch-remote-control
5.Open the another Command Terminal.
6.Run the Ant command in execution of created target in build.xml file
7.Scripts would be run in parallel on Firefox and Googlechrome

Here is JAVA script file:

package com.example.tests;
import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import java.util.regex.Pattern;
import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.closeSeleniumSession;
import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.session;
import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.startSeleniumSession;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;

public class Paralleltests extends SeleneseTestCase {
public static final String TIMEOUT = "24000";


@BeforeMethod
@Parameters({"seleniumHost", "seleniumPort", "browser", "webSite"})
protected void startSession(String seleniumHost, int seleniumPort, String browser, String webSite) {
startSeleniumSession(seleniumHost, seleniumPort, browser, webSite);
session().setTimeout(TIMEOUT);
}

@AfterMethod
protected void closeSession() {


closeSeleniumSession();
}
@Test public void test1() throws Exception {
session().open("/wordpress/wp-login.php");

session().type("user_login", "admin");
session().type("user_pass", "password");
session().click("wp-submit");
session().waitForPageToLoad("30000");
}


@Test public void test2() throws Exception {

session().open("/wordpress/wp-login.php");

session().type("user_login", "admin");
session().type("user_pass", "password");
session().click("wp-submit");

session().waitForPageToLoad("30000");

session().click("//ul[@id='adminmenu']/li[@id='menu-users']/a");

session().waitForPageToLoad("30000");
session().click("//li[@id='menu-users']/div[3]/ul/li[2]/a");
session().waitForPageToLoad("30000");
session().type("user_login", session().getEval(""Demo" + new Date().getMilliseconds();"));
session().type("email", session().getEval(""Demo" + new Date().getMilliseconds()+"@gmail.com";"));
session().type("first_name", "Test");
session().type("last_name", "User");
session().type("url", "http://www.wordpress.com");
session().type("pass1", "password");
session().type("pass2", "password");
session().click("addusersub");
session().waitForPageToLoad("30000");
}
@Test public void test3() throws Exception {

session().open("/wordpress/wp-login.php");

session().type("user_login", "admin");
session().type("user_pass", "password");
session().click("wp-submit");
session().waitForPageToLoad("30000");
session().click("//ul/li[@id='menu-posts']/a");
session().waitForPageToLoad("30000");
session().click("//ul[@id='adminmenu']/li[@id='menu-posts']/div[3]/ul/li[2]/a");
session().waitForPageToLoad("30000");
session().type("title", session().getEval(""Add Post" + new Date().getMilliseconds();"));
session().click("publish");
session().waitForPageToLoad("30000");
session().click("//li[@id='menu-posts']/div[3]/ul/li[1]/a");
session().waitForPageToLoad("30000");
}


public void waitForElementPresent(String locator, int timeout, Selenium selenium) throws Exception {
for (int second = 0;; second++) {
if (second >= timeout)
throw new SeleniumException("Timout finding: " + locator);
try {
if (session().isElementPresent(locator))
break;
}
catch (Exception ex) {}
Thread.sleep(1000);
}

}
}

Here is the used configuration file codes:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ParallelExecuteResults" parallel="tests" thread-count="10" verbose="1" annotations="JDK">

<test name="FirefoxTestReport" >
<parameter name="seleniumHost" value="localhost" />
<parameter name="seleniumPort" value="5555" />
<parameter name="browser" value="*firefox" />
<parameter name="webSite" value="http://localhost/wordpress/wp-login.php" />

<classes>
<class name="com.example.tests.Paralleltests">
<methods>
<include name="test1" />
<include name="test2" />
<include name="test3" />

</methods>

</class>
</classes>
</test>
<test name="GoogleChromeTestReport" >
<parameter name="seleniumHost" value="localhost" />
<parameter name="seleniumPort" value="5556" />
<parameter name="browser" value="*googlechrome" />
<parameter name="webSite" value="http://localhost/wordpress/wp-login.php" />

<classes>
<class name="com.example.tests.Paralleltests">
<methods>
<include name="test1" />
<include name="test2" />
<include name="test3" />

</methods>

</class>
</classes>
</test>
</suite>

Here is the build.xml file codes:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE project [
]>
<project name="S4" default="usage" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<property environment="env"/>

<property file="./build.properties"/>

<property name="webSite" value="http://localhost/wordpress/wp-login.php" />
<property name="seleniumHost" value="localhost" />
<property name="seleniumPort" value="4444" />
<property name="browser" value="*firefox" />
<property name="browser" value="*googlechrome" />
<property name="hubPollerIntervalInSeconds" value="30" />


<target name="start-selenium-server">
<java jar="${ws.home}/lib/selenium-server.jar"/>
</target>

<target name="setClassPath" unless="test.classpath">
<path id="classpath_jars">
<fileset dir="${basedir}/lib" includes="*.jar"/>
</path>
<pathconvert pathsep=":"
property="test.classpath"
refid="classpath_jars"/>
</target>

<target name="init" depends="setClassPath">
<tstamp>
<format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
</tstamp>
<condition property="ANT"
value="${env.ANT_HOME}/bin/ant.bat"
else="${env.ANT_HOME}/bin/ant">
<os family="windows" />
</condition>
<taskdef name="testng" classpath="${test.classpath}"
classname="org.testng.TestNGAntTask" />

</target>

<!-- all -->
<target name="all">
</target>

<!-- clean -->
<target name="clean">
<delete dir="${basedir}/build"/>
</target>

<!-- compile -->
<target name="compile" depends="init, clean" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${basedir}/build" includes="**/*"/>
</delete>
<echo message="making directory..."/>
<mkdir dir="${basedir}/build"/>
<echo message="classpath: ${test.classpath}"/>
<echo message="compiling..."/>
<javac
debug="true"
destdir="${basedir}/build"
includes="*.java"
srcdir="${basedir}/test"
target="1.6"
classpath="${test.classpath}"


></javac>
</target>

<!-- build -->
<target name="build" depends="init">
</target>

<!-- run -->

<target name="run-in-parallel" depends="compile">
<testng classpath="${test.classpath}:${basedir}/build" suitename="ParallelExecuteResults">
<xmlfileset dir="${basedir}/config" includes="testngp.xml"/>
</testng>
</target>
<target name="usage">
<echo>
ant run will execute the test
</echo>
</target>

<!-- ****************** targets not used ****************** -->
</project>


Here is the build.properties file codes:

ws.home=C:/WorkshopFile/GridProjectWithParallel
test.dest=${ws.home}/build
test.src=${ws.home}/test



Helpful Video


3 comments:

  1. For Vipin: Need to search the referenced grid project in VM.

    ReplyDelete
  2. comment from viresh:
    example runs on multiple browsers sequentially and not in parallel - more work required

    ReplyDelete
  3. Hi Viresh, it's done from my side, please do below things to make the solution completely synced with VM:
    1. I have uploaded the project directory on Dropbox having name "GridProjectWithParallel.zip" please copy it from here and paste on VM on path
    C:\WorkshopFiles, then unzip it.
    2. It has complete project's directory.

    ReplyDelete

Thank you for your comment
- Time2test Team