9/19/2012

Hudson job running Selenium RC tests with TestNG Parameters

I really can recommend to execute tests under the testng framework. It provides optional parameters and execution of test suites in an easily understanding way and allows to modify the style of the reports through a css file. Optional parameters also allows you to run tests within Eclipse without setting them (then the default value is used, see below)For my Tests I used a superclass containing the “setUp” method, which is executed just before each testclass:
@Parameters({ "seleniumhost", "seleniumport", "browser"})
@BeforeClass
public void setUp(@Optional("localhost") String seleniumhost, @Optional("4444") String seleniumport, @Optional(Selenium.FIREFOX) String browser){
...
}
view raw Login.java hosted with ❤ by GitHub
As you can see the parameters “seleniumhost” etc. are optional. If one of them isn’t set, then the default value is used, e.g. “localhost. You want to insert all your testclasses into your testsuite often named “testng.xml”. There you also can set values for the parameters:
<suite name="testsuite">
<parameter name="seleniumhost" value="192.168.1.22"/>
<parameter name="seleniumport" value="4444"/>
<parameter name="browser" value="*iexplore"/>
<test name="Login">
<classes>
<class name="usecases.Login" />
</classes>
</test>
<test name="Logout">
<classes>
<class name="usecases.Logout" />
</classes>
</test>
</suite>
view raw testng.xml hosted with ❤ by GitHub
For running the testsuit testng.xml you just need to type "java org.testng.TestNG testng1.xml" into your command prompt. For the execution of your tests by hudson, you’ll need to create a new hudson job, build it once, then copy your tests into the established directory of the job (you’ll find it at usr/.hudson/jobs ) and write a ant file, which can be executed by hudson.
An Ant file has different targets, which can or can’t be executed separatly, sequential. You can define them in the hudson job configuration. Your ant file could look like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="testsuite" name="Selenium">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<path id="TestNG.libraryclasspath">
<pathelement location="../../libs/testng-6.0.1.jar"/>
</path>
<path id="Selenium.classpath">
<pathelement location="bin"/>
<path refid="TestNG.libraryclasspath"/>
<pathelement location="../../libs/selenium-java-client-driver.jar"/>
<pathelement path="${env.JAVA_HOME}\jre\lib\rt.jar"/>
</path>
...
<target depends="build" name="runTestsuite" description="Running testsuite">
<echo>Running testsuite ... </echo>
<taskdef resource="testngtasks" classpathref="Selenium.classpath"/>
<testng outputDir="output/testsuite" haltonfailure="false" classpathref="Selenium.classpath">
<xmlfileset dir="src" includes="testsuite.xml"/>
</testng>
</target>
</project>
view raw build.xml hosted with ❤ by GitHub
For publishing results I can recommend to use testng-plugin and/or HTML Publisher Plugin for Hudson.

No comments:

Post a Comment