Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

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 http://www.seleniummaster.com/seleniummastertestapp/index.php
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: UserSettingTest.java 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.open("http://www.seleniummaster.com/seleniummastertestapp/index.php/");
    selenium.waitForPageToLoad("30000");
    selenium.type("id=login_login_username""test");
    selenium.type("id=login_login_password""XXXXX")//password is omitted here
    selenium.click("id=login_submit");
    selenium.waitForPageToLoad("30000");
    assertEquals(selenium.getText("css=ul.cr > li > a")"Test Selenium");
  }
    
  @Test(description="Navigates to the User Settings page")
    public void openUserSettingPage() {
    selenium.click("link=Settings");
    selenium.waitForPageToLoad("30000");
    selenium.type("id=login_login_username""test");
    selenium.type("id=login_login_password""XXXXXXX")//password is omitted here
    selenium.click("id=login_submit");
    selenium.waitForPageToLoad("30000");
    assertTrue(selenium.isElementPresent("xpath=//input[contains(@id,'accountprefs_friendscontrolc')]"));
    
  }
    
  @Test(description="Change a User settings to add as a friends after authorization")
    public void ChangeUserSettings() {
    selenium.click("xpath=//input[contains(@id,'accountprefs_friendscontrolc')]");
    selenium.click("id=accountprefs_submit");
    assertEquals(selenium.getText("css=div.ok")"Preferences saved");
  }
   
  @Test(description="Log out the system")
  public void Logout() {
    selenium.click("link=Logout");
    selenium.waitForPageToLoad("30000");
    assertEquals(selenium.getText("id=login_submit")"");
  }

  @BeforeSuite(alwaysRun = true)
  public void setupBeforeSuite(ITestContext context) {
    String seleniumHost = context.getCurrentXmlTest().getParameter("selenium.host");
    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();
    rcc.setSingleWindow(true);
    rcc.setPort(Integer.parseInt(seleniumPort));
    
    try {
     server = new SeleniumServer(false, rcc);
      server.boot();
    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);
    selenium.start();
    selenium.setSpeed("5000");
  }
  @AfterSuite(alwaysRun = true)
  public void setupAfterSuite() {
    selenium.stop();
    server.stop();
  }
}

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="selenium.host" value="localhost" />
  <parameter name="selenium.port" value="4444" />
  <parameter name="selenium.browser" value="*firefox" />
  <parameter name="selenium.url" value="http://www.seleniummaster.com/seleniummastertestapp/index.php" />
  <test name="Test" preserve-order="true">
    <classes>
      <class name="com.seleniummaster.testsuite.UserSettingTest">
      <methods>
          <include name="launchSiteAndLogin" />
          <include name="openUserSettingPage" />
          <include name="ChangeUserSettings" />
          <include name="Logout" />
          </methods>
      </class>
     </classes>
  </test> <!-- Test -->
</suite>

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.