Popular Posts

Wednesday, 2 March 2011

Selenium Grid Example - Part2

Working with demo of selenium grid -- 25-march-2011 - fixed the issues with error reported by Mitul. it was regarding a warning message which generates when we don't make 'includeantruntime' variable false in javac tab into the build.xml file

1. Open a new command terminal, if you run following command then it will execute the test in sequence:
Ant run-demo-in-sequence




2. Open a new command terminal, if you run following command then it will execute the test in sequence:
Ant Run-demo-in-parallel



Nice example of Selenium Grid with Testng

1. Create a project workspace, you need to maintain folder structure like below:
gridproject
- build
- config
- test
- build.properties file
- lib
- build.xml
See the below screen to see above workspace:



2. Create a java file with name ‘ParallelTests.java’ with following codes:
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);}@AfterMethodprotected void closeSession() {closeSeleniumSession();}@Test public void test1() throws Exception {session().open("/");}@Test public void test2() throws Exception {session().open("/");}@Test public void test3() throws Exception {session().open("/");}@Test public void test4() throws Exception {session().open("/");}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);}}}


See the below screen just for reference:


3. Now you need to create a configuration file, create a xml with name ‘testngp.xml’ with following codes and put the file in config folder:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><suite name="ParallelExecuteResults" parallel="methods" thread-count="10" verbose="1" annotations="JDK"><parameter name="seleniumHost" value="localhost" /><parameter name="seleniumPort" value="4444" /><parameter name="browser" value="*firefox" /><parameter name="webSite" value="http://www.google.com/" /><test name="Login Test" junit="false"><classes><class name="com.example.tests.Paralleltests"><methods><include name="test1" /><include name="test2" /><include name="test3" /><include name="test4" /></methods></class></classes></test></suite>

See the below screen just for reference:


4. You have already a build.xml file in your project’s workspace, open it and put the following code and again save it:
  <?xml version="1.0" encoding="iso-8859-1" ?> 
  <!DOCTYPE project (View Source for full doctype...)> 
- <project name="S4" default="usage" basedir=".">
- <!--  ========== Initialize Properties =================================== 
  --> 
  <property environment="env" /> 
  <property file="./build.properties" /> 
  <property name="webSite" value="http://www.google.com" /> 
  <property name="seleniumHost" value="localhost" /> 
  <property name="seleniumPort" value="4444" /> 
  <property name="browser" value="*firefox" /> 
  <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="${ws.home}/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" /> 
- <!--  clean 
  --> 
- <target name="clean">
  <delete dir="${test.dest}" /> 
  </target>
- <!--  compile 
  --> 
- <target name="compile" depends="init, clean">
- <delete includeemptydirs="true" quiet="true">
  <fileset dir="${test.dest}" includes="**/*" /> 
  </delete>
  <echo message="making directory..." /> 
  <mkdir dir="${test.dest}" /> 
  <echo message="classpath: ${test.classpath}" /> 
  <echo message="compiling..." /> 
  <javac debug="true" destdir="${test.dest}" includes="*.java" srcdir="${test.src}" target="1.5" classpath="${test.classpath}" includeantruntime="false" /> 
  </target>
- <!--  build 
  --> 
  <target name="build" depends="init" /> 
- <!--  run 
  --> 
- <target name="run-in-parallel" depends="compile">
- <testng classpath="${test.classpath}:${test.dest}" suitename="ParallelExecuteResults">
  <xmlfileset dir="${ws.home}/config" includes="testngp.xml" /> 
  </testng>
  </target>
- <target name="usage">
  <echo>ant run will execute the test</echo> 
  </target>
- <!--  ****************** targets not used ****************** 
  --> 
  </project>

See the below screen just for reference:

5. Now, we need to save build.properties file with following code:
ws.home=C:/gridproject (Note: if you create your workspace in C dir then, otherwise you need to give proper workspace path)
test.dest=${ws.home}/build
test.src=${ws.home}/test

6. See the below screen just for reference:


7. It’s time to put needed jar files in your library, below jar files which you need to put in lib:
a. junit.jar
b. selenium-grid-tools-1.0.6.jar
c. selenium-java-client-driver.jar
d. selenium-server.jar
e. testng-5.5-jdk15.jar
f. testng.jar
g. selenium-java-client-driver-tests.jar
h. selenium-server-0.9.1-20070223.200626-116-standalone.jar
see the below screen just for reference:


8. TESTNG: We have already placed the required jars file what are necessary in using of TESTNG into the project’s library folder as Reference, in the screen you can see testng.jar and testng-5.5-jdk15.jar files which are responsible for compilation/execution of tests written into TESTNG:



Only what you need to do is, set testng-5.5-jdk15.jar file into your system variables path, append C:\gridproject\lib\testng-5.5-jdk15.jar path into the path variable in the System variables section.

9. I am assuming here that selenium grid with all four instances are still running on system if not then please start it again.

10. Open a new command terminal,

And run ant run-in-parallel

As in below screen:



11. After executing the above command, you would see that multiple firefox instances will be launched parallel.

12. After execution of different tests, you can see the execution result; result will be shown into the “C:\gridproject\test-output” folder. You need to go into the different tests folder and by opening the index.html file you would be able to see the results. See an example:


Here is the updated embedded video file:

SeleniumGrid_Example_Part2-updated
Uploaded by vipingupta. - Technology reviews and science news videos.

5 comments:

  1. awesome post:-)

    thanks
    shiva

    ReplyDelete
  2. Hi,

    i copy everything like you have posted and did, but still when i run ant task geet this error:
    "D:\TEST_GRID\build.xml:1: The processing instruction target matching "[xX][mM][l
    L]" is not allowed."

    Can you help me ?

    Thanks.

    ReplyDelete
    Replies
    1. No space before <?xml version="1.0"....
      I hope this will solve the problem.

      Delete
  3. hi
    when we get some time, we will try and answer your question. thanks
    Time2test

    ReplyDelete
  4. Software tester We always follow the latest trends in IT world and update our syllabus based on that information. Our students graduate with the knowledge of the latest technology that is used in Software Development and QA.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...