Wednesday, March 21, 2012

Selenium-ANT-TestNG (SAT) Framework

Posted by aditya_dada on May 20, 2008 at 12:39 PM CDT


This framework is created using ANT, TestNG and Selenium.
Through this framework, a user is able to create an automated test case which can be run later by executing a single command. The uses of different frameworks are as follows:
Selenium: This framework is needed for recording a testcase, exporting it into Java, and executing it on a browser
TestNG: This framework is used to organize different tests, and to report results.
ANT: provides the glue to hold everything together, and execute basic commands.


Basic knowledge of Selenium IDE, Selenium RC, TestNG and ANT.


1. Install Selenium IDE on your Firefox browser.
2. Make sure you have ANT installed.
3. Grab the file from here. Unzip it into a directory of your choice.

About the sample
There are 2 bundled samples:
1. This test logs into the glassfish admin console using the username/password: admin/administrator. It then clicks on the 'Enterprise Applications' link, and tests to see if there are no applications deployed. Else, test will fail.
2. This test accesses http://localhost:8080/ page, and tests whether the term "Glassfish Project" appears or not.

The config directory contains testng.xml file, and is a place holder for configuration files.
The lib directory contains required jar files, and is a place holder for other library jars/zips.
The test directory contains test java files and is a place holder for other test files.
Once the tests are run, build and test-output directories will be created.
The build directory contains compiled classes, and is a volatile directory that is deleted by the target clean inbuild.xml file.
The test-output directory contains result files, and is generated by TestNG.
To run the bundled sample, follow the steps for "Execute your test through ANT" (see below).
Currently, I face some issues running this on IE. I have successfully been able to run tests using Firefox.

How To:

Write a test case
The basic structure that one has to follow to create a test case is:
1. Record a testcase using Selenium IDE (Firefox browser only), Export the test case into Java
2. Edit the test case – java file, and add TestNG annotations
3. Execute the test case using ANT

1. Record test using selenium IDE and export it into Java:
i. Open the website you’d like to test in Firefox browser
ii. Start the selenium IDE from your Firefox browser
iii. Ensure that the Base URL in Firefox is the same as the website you want to test
iv. Start clicking on the website. Selenium will record your actions.
v. When finished, do: File -> Export Test As... -> Java – Selenium RC and save the java file.
After exporting, your java test case should look like this:

2. Add TestNG to your test case:
i. Open the java test file you just created in an editor
ii. Add the following:
(a) to the import statements:
import org.testng.annotations.*;
(b) To variable declarations:

private DefaultSelenium selenium;
private static String TIMEOUT_PERIOD ="10000";

(c) To The class:

@BeforeSuite(alwaysRun = true)
public void setUp(String browser) {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://localhost:8080");

@AfterSuite(alwaysRun = true)
private void stopTest() {

(d) After each statement in the test that will take time for a page to load:

selenium.waitForCondition("selenium.browserbot.isNewPageLoaded()", TIMEOUT_PERIOD);

(e) On top of the test method:

@Test(groups = { "" })
e.g. @Test(groups = { "roller" })

(f) Edit page to be opened. Make sure arg to ‘open’ method of selenium points to the right context root e.g. Ifhttp://localhost:8080/ is the correct URL to test, then since we already have tried to open localhost:8080 in setUp, we only need to open the context root page.

iii. Save the test file
(a). (Optional) Update testng.xml file. TestNG uses this file to read which groups to execute and also to pass parameters to the test. This file is only required if build.xml file uses testng.xml file to run the test.
Note: in sample test included, testng.xml is used, and will need to be updated.

iv. Ensure that the test file you created is mentioned in the build.xml compile target.

3. Execute your test through ANT
i. Start domain required by your webpage. e.g. asadmin start-domain
ii. Start database if needed (Not needed for bundled sample test)
iii. Start selenium server e.g. cd testws/lib, java -jar selenium-server.jar
iv. Execute the command: “ant run” from ‘testws’. You should see a new firefox browser window open up, and run through your test.
v. Check reports generated by TestNG under ‘testws/test-output’ directory. Open the index.html to see different output from the test.

Console output:


No comments: