Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20



Selenium RC and TestNG

In this example you will learn how to use Selenium RC with TestNG. The key feature of the sample is that Selenium Server will be launched automatically by the application. Each test has description in the test methods. 

Model your test cases 

Before you start automation, you need to know what are the tests and what you are going to validate. Let's break the test suite into 4 test cases. 

Test 1 (launchSiteAndLogin)
1. Go to
2. Enter "test" in the Username field
3. Enter "XXXX" in the Password filed
4. Click on the Login button
5. Verify that the text "Selenium Test" is present.

Test 2 (openUserSettingPage)
1. Click on the Settings link on the top of the page
2. Enter "test" in the Username field
3. Enter "XXXX" in the Password filed
4. Click on the Login button
5. Verify that Account Prefernces page displayed.


Test 3 (ChangeUserSettings)
1. Click on the radio button near friends need my authorization to add me
2. Click on the save button
3. Verify that the text "preference saved" displayed.

Test4 (Logout)
1. Click on the Logout button
2. Verify that Login button displayed.

Automation Implementaion

Step 1: Create a Java Project named "SeleniumMasterRemoteControllerTestNg", add a package named "com.seleniummaster.testsuite". In the build path, add the selenium-server-standalone-2.39.0.jar file as a reference. Add the TestNG class named UserSettingTest and add @BeforeSuite and @AfterSuite annotations. See the code in Step 2 for reference. 

Step 2: code


package com.seleniummaster.testsuite;
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.assertEquals;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.ITestContext;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.HttpCommandProcessor;
import com.thoughtworks.selenium.Selenium;

public class UserSettingTest {
  private Selenium selenium;
  SeleniumServer   server;
  @Test(description="Launches the Selenium Master Test Application and Login")
  public void launchSiteAndLogin(){"");
    selenium.type("id=login_login_password""XXXXX")//password is omitted here"id=login_submit");
    assertEquals(selenium.getText(" > li > a")"Test Selenium");
  @Test(description="Navigates to the User Settings page")
    public void openUserSettingPage() {"link=Settings");
    selenium.type("id=login_login_password""XXXXXXX")//password is omitted here"id=login_submit");
  @Test(description="Change a User settings to add as a friends after authorization")
    public void ChangeUserSettings() {"xpath=//input[contains(@id,'accountprefs_friendscontrolc')]");"id=accountprefs_submit");
    assertEquals(selenium.getText("css=div.ok")"Preferences saved");
  @Test(description="Log out the system")
  public void Logout() {"link=Logout");

  @BeforeSuite(alwaysRun = true)
  public void setupBeforeSuite(ITestContext context) {
    String seleniumHost = context.getCurrentXmlTest().getParameter("");
    String seleniumPort = context.getCurrentXmlTest().getParameter("selenium.port");
    String seleniumBrowser = context.getCurrentXmlTest().getParameter("selenium.browser");
    String seleniumUrl = context.getCurrentXmlTest().getParameter("selenium.url");
    RemoteControlConfiguration rcc = new RemoteControlConfiguration();
    try {
     server = new SeleniumServer(false, rcc);
    catch (Exception e) {
      throw new IllegalStateException("Can't start selenium server", e);
    HttpCommandProcessor  proc = new HttpCommandProcessor(seleniumHost, Integer.parseInt(seleniumPort),
        seleniumBrowser, seleniumUrl);
    selenium = new DefaultSelenium(proc);
  @AfterSuite(alwaysRun = true)
  public void setupAfterSuite() {

Step 3: write the testng.xml file to run the test as TestNG Suite

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<parameter name="" value="localhost" />
  <parameter name="selenium.port" value="4444" />
  <parameter name="selenium.browser" value="*firefox" />
  <parameter name="selenium.url" value="" />
  <test name="Test" preserve-order="true">
      <class name="com.seleniummaster.testsuite.UserSettingTest">
          <include name="launchSiteAndLogin" />
          <include name="openUserSettingPage" />
          <include name="ChangeUserSettings" />
          <include name="Logout" />
  </test> <!-- Test -->

Step 4: You can launch TestNG from the command line, using a Eclipse plugin or even programatically. We are going to use the Eclipse plugin.

If you installed TestNG correctly, you will see this menu when you right click on the XML file:

Click on the TestNG Suite option to run the test suite. You will see a nice test result tree below.

The result above showed that 2 test cases failed. We need to check the report file under the folder "test-output" within the Java Project folder to find out the failures.