Wednesday, May 5, 2010

Running Java Test Cases using Selenium GRIDs Tutorial

This tutorial explains how to use the Selenium GRID to run multiple Selenium Test Cases (Java Based). Although not recommended this allows you to use the selenium GRID as a stress testing tool.

Prerequisites:
1. Selenium IDE, Selenium RC(Remote Control) and Selenium GRIDS need to be installed
2. Intermediate Knowledge of the above is required. (If you dont have the necessary knowledge then please go through the above tools before continuing further).
3. Basic Knowledge on Java

NOTE:
Test Case - A Collection of Tests.

Test Suite - A Collection of Test Suites

1. Record the Test Suite / Test Case using the selenium IDE.
2. Export the test cases as JAVA (TestNG)

Each class should contain a method for the testing as shown below.

Import_Statements

public class WSC_login extends SeleneseTestNgHelper{

public static final String TIMEOUT = "120000";

@Test
public void testWSC_login() throws Exception {
session().open("/wi5/dev/ava/index.php?PG&&lan=english");
session().type("textfield2", "ava1");
session().type("textfield", "12345678");
session().click("//img[@alt='login']");
Thread.sleep(2000);
session().click("link=home");
assertTrue(session().getConfirmation().matches("^Are you sure you want to logout[\\s\\S]$"));
}
}

3. Modify the class as follows
i. remove the “extends SeleneseTestNgHelper” section
ii. add the following code

public static final String TIMEOUT = "120000";

@BeforeMethod(groups = {"default", "example"}, alwaysRun = true)
@Parameters({"seleniumHost", "seleniumPort", "browser", "webSite"})
protected void startSession(String seleniumHost, int seleniumPort, String browser, String webSite) throws Exception {
startSeleniumSession(seleniumHost, seleniumPort, browser, webSite);
session().setTimeout(TIMEOUT);
}

@AfterMethod(groups = {"default", "example"}, alwaysRun = true)
protected void closeSession() throws Exception {
closeSeleniumSession();
}

iii. Modify the package name and place the java files in the proper folder structure that matches the package
iv. Replace all instances of “selenium.” With “session().”. Do not replace the instance on the import name.
v. Replace all instances of “verify” with “assert”
vi. Replace all variable selenium variable names with java variable names. Eg:- replace “${Login_Name}” with Login_Name.
vii. Add the following imports

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 static org.testng.AssertJUnit.assertTrue;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;

Note : When the test is run using the grid, each “test” method will be run as a new instance. If you wish to run the same test multiple times then copies of the “test” method you with to run must exist. However they cannot have the same method name.

viii. Add the @BeforeMethod to set the actions that need to be done before the test methods are run.

@BeforeMethod(groups = {"default", "example"}, alwaysRun = true)
@Parameters({"seleniumHost", "seleniumPort", "browser", "webSite"})
protected void startSession(String seleniumHost, int seleniumPort, String browser, String webSite) throws Exception {
startSeleniumSession(seleniumHost, seleniumPort, browser, webSite);
session().setTimeout(TIMEOUT);
session().setSpeed("500");
}

ix. Add the @AfterMethod to set the actions that need to be done after the test methods are run.

@AfterMethod(groups = {"default", "example"}, alwaysRun = true)
protected void closeSession() throws Exception {
closeSeleniumSession();
}

4. Place the folder used in step 3.iii within the following folder. We will be using the ant build files provided with the selenium grid release. It is possible to create our own build files.

5. Update the build.xml file within the

\examples\java folder
Place the following code and modify the necessary values

[target name="run_WSC_Test"
description="WSC Test"]
[java classpathref="runtime.classpath"
classname="org.testng.TestNG"
failonerror="true"]

[sysproperty key="java.security.policy" file="${rootdir}/lib/testng.policy"/]
[sysproperty key="webSite" value="http://192.168.11.47/wi5/dev/ava/index.php" /]
[sysproperty key="seleniumHost" value="${seleniumHost}" /]
[sysproperty key="seleniumPort" value="${seleniumPort}" /]
[sysproperty key="browser" value="${browser}" /]
[arg value="-d" /]
[arg value="${basedir}/target/reports" /]
[arg value="-suitename" /]
[arg value="WSC TEST" /]
[arg value="-parallel"/]
[arg value="methods"/]
[arg value="-threadcount"/]
[arg value="5"/]
[arg value="WSC_test.xml"/]
[!--arg value="-testclass"/]
[arg value="com.thoughtworks.selenium.grid.examples.java.WSC_login"/--]
[/java]

[/target]

target name="run_WSC_Test" description="WSC Test"] - name of the ant build

[java classpathref="runtime.classpath"
classname="org.testng.TestNG"
failonerror="true"] - The runtime.classpath is a ant path that points to necessary selenium libraries.

[sysproperty key="java.security.policy" file="${rootdir}/lib/testng.policy"/] - Policy file that depicts the actions permittied by the selenium grid

[sysproperty key="webSite" value="http://192.168.11.47/wi5/dev/ava/index.php" /] - Name of the website that the test will run upon

[sysproperty key="seleniumHost" value="${seleniumHost}" /]
[sysproperty key="seleniumPort" value="${seleniumPort}" /] - points to the selenium grid host and port. The default values are 4444 / localhost

[sysproperty key="browser" value="${browser}" /] - Browser type used for the tests. Default is Firefox

[arg value="-d" /]
[arg value="${basedir}/target/reports" /] - location where the reports are genereated

[arg value="-suitename" /]
[arg value="WSC TEST" /] - The suit name

[arg value="-parallel"/]
[arg value="methods"/]
[arg value="-threadcount"/]
[arg value="5"/] - amount of threads used for the tests.

[arg value="WSC_test.xml"/] - xml file that describes the classes and other parameters used for the test. This part can be replaced with the following.

[!--arg value="-testclass"/]
[arg value="com.thoughtworks.selenium.grid.examples.java.WSC_login"/--]
[/java]

[/target]

6. If the target value points to a xml file then prepare the file as follows.

[!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ]

[suite name="WSC Testing" parallel="methods" thread-count="5" verbose="3" ]
[test name="WSC Testing" ]
[!--packages]
[package name="com.thoughtworks.selenium.grid.examples.java" /]
[/packages--]
[classes]
[class name="WSC_Selenium_Grid_PT.java.WSC_login"/]
[/classes]
[/test]
[/suite]


[!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ]

[suite name="WSC Testing" parallel="methods" thread-count="5" verbose="3" ]
[test name="WSC Testing" ]
[/packages--]

[classes]
[class name="WSC_Selenium_Grid_PT.java.WSC_login"/] - classes used for the test

[!--packages]
[package name="com.thoughtworks.selenium.grid.examples.java" /] - If the classes in the entire package are to be used then it can be specified as above.

[/classes]
[/test]
[/suite]


7. Open a new Command Prompt and navigate to the \examples\java folder. And run “ant build”. This will compile the files and place them within the classpath specified within the build.xml file

8. Start the Selenium Grid as well as multiple instances of the selenium RC

9. run the test by “ant ”. Note the target name should match that which is specified in the build.xml file.