"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

January 30, 2011

Test Automation Framework Using Selenium - Part II

[Next Post in Series]

In Continuation with Previous post We will be looking at Adding Data Driven Tests using TestNG. Please refer to TestNG earlier posts for examples

TestRun.Java - In this file we instantiate selenium instance, open the page. Data Providers is set in this file.
FirstPage.Java - This has methods for opening first page and verifying first page items. Assert statements are added to verify. Removed print statements that was present in earlier post.
SecondPage.Java - Seperating methods and verifiers for that particular page to keep test suite maintainable. Three verifiers are created for ordered list, unordered list and verify links.

Improvements (Next Upcoming Posts)
  • Logging
  • Reporting
  • Continous Integration Server
In next posts I plan to learn and Implement improvements.
1. Create a project and add required libraries. Verify earlier post
2. TestRun.java


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.thoughtworks.selenium.Selenium;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.*;

import com.thoughtworks.selenium.*;
public class TestRun
{
      Selenium selenium;
      FirstPage fp;
      SecondPage sp;

@BeforeTest
public void setUp()
{
      selenium = new DefaultSelenium("localhost",4444,"*iehta","http://sqlandsiva.blogspot.com/");
      selenium.start();
}

@AfterTest
public void tearDown()
{
      selenium.stop();
}

@Test(dataProvider = "firstPageList")
public void firstPageTestCase( ArrayList<String> listFirstPageExpectedResult)
{
      fp = new FirstPage(selenium);
      fp.clickFirstPage();
      fp.verifyListItems(listFirstPageExpectedResult);
      sp = fp.openSecondPage();
}

@Test(dataProvider = "unOrderedList")
public void secondPageVerifyUnOrderList(ArrayList<String> unOrderedlistExpectedResult)
{
      sp.verifyUnorderedListItems(unOrderedlistExpectedResult);
}

@Test(dataProvider = "verifyLinks")
public void secondPageVerifyLinks(ArrayList<String> linksExpectedResult)
{
      sp.verifySecondPageLinks(linksExpectedResult);
}

@Test(dataProvider = "orderedList")
public void secondPageVerifyOrderList(ArrayList<String> listExpectedResult)
{
      sp.verifyorderedListItems(listExpectedResult);
}

@DataProvider(name = "unOrderedList")
public Object[][] setDataforUnOrderedList() {

      List<String> listExpectedResult = new ArrayList<String>();
      listExpectedResult.add("Unordered List One");
      listExpectedResult.add("Unordered List Two");
      listExpectedResult.add("Unordered List Three");

      Object[][] retkeyword = {{listExpectedResult}};
      return (retkeyword);
}

@DataProvider(name = "verifyLinks")
public Object[][] setDataforLinks() {

      List<String> listExpectedResult = new ArrayList<String>();
      listExpectedResult.add("Visit sqlandsiva!");
      listExpectedResult.add("Visit brightlife4all!");

      Object[][] retkeyword = {{listExpectedResult}};
      return (retkeyword);
}

@DataProvider(name = "orderedList")
public Object[][] setDataforOrderedList() {

      List<String> listExpectedResult = new ArrayList<String>();
      listExpectedResult.add("Order List One");
      listExpectedResult.add("Order List Two");
      listExpectedResult.add("Order List Three");

      Object[][] retkeyword = {{listExpectedResult}};
      return (retkeyword);
}

@DataProvider(name = "firstPageList")
public Object[][] setDataforFirstPageList()
{
      List<String> listExpectedResult = new ArrayList<String>();
      listExpectedResult.add("a1");
      listExpectedResult.add("a2");
      listExpectedResult.add("a3");
      listExpectedResult.add("b1");
      listExpectedResult.add("b2");
      listExpectedResult.add("b3");
      listExpectedResult.add("c1");
      listExpectedResult.add("c2");
      listExpectedResult.add("c3");

      Object[][] retkeyword = {{listExpectedResult}};
      return (retkeyword);
}
}
3. FirstPage.java
import com.thoughtworks.selenium.Selenium;
import static org.testng.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Vector;
import org.testng.Assert;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
public class FirstPage{
Selenium selenium;
public static String FIRST_PAGE_LINK = "/2011/01/test-xml-page-for-selenium.html";
public static String SECOND_PAGE_LINK = "/2011/01/test-page-for-selenium-example.html";

public FirstPage(Selenium selenium)
{
      this.selenium = selenium;
     
}
public FirstPage clickFirstPage()
{
      System.out.println("Opening First Page");
      clickFirstPage(FIRST_PAGE_LINK);
      selenium.waitForPageToLoad("30000");
      return new FirstPage(selenium);
}

public SecondPage openSecondPage()
{
      System.out.println("Opening Second Page");
      clickSecondPage(SECOND_PAGE_LINK);
      selenium.waitForPageToLoad("30000");
      return new SecondPage(selenium);
}

private void clickSecondPage(String number)
{
      selenium.open(SECOND_PAGE_LINK);
}

private void clickFirstPage(String number)
{
      selenium.open(FIRST_PAGE_LINK);
}

public void verifyListItems(ArrayList<String> OrderList)
{
      System.out.println("In FirstPage Test Method");
      try
      {
            System.out.println("Running FirstPage Test");
            int i=1,j=1;
            while(selenium.isElementPresent("//table[@id='firstTable']//tr["+i+"]/td["+j+"]"))
            {
            while(selenium.isElementPresent("//table[@id='firstTable']//tr["+i+"]/td["+j+"]"))
            {
                  Assert.assertTrue(OrderList.contains(selenium.getText("//table[@id='firstTable']//tr["+i+"]/td["+j+"]")));
                  j++;
            }
            j=1;
            i++;
      }
      }
      catch(Exception ex)
      {
                        ex.toString();
      }
}
}
4. SecondPage.Java
import com.thoughtworks.selenium.Selenium;
import static org.testng.Assert.assertTrue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.testng.Assert;
import org.testng.annotations.*;

import com.thoughtworks.selenium.*;
public class SecondPage{
Selenium selenium;

public SecondPage(Selenium selenium)
{
      this.selenium = selenium;
}

public SecondPage clickFirstPage()
{
      return new SecondPage(selenium);
}

public void verifyUnorderedListItems(ArrayList<String> OrderList)
{
      System.out.println("In SecondPage UnOrderedList Verifier Method");
      try
      {
            int j=1;
            while(selenium.isElementPresent("//div[@id='UnOrderedList']/ul/li["+j+"]"))
            {
                  Assert.assertTrue(OrderList.contains(selenium.getText("//div[@id='UnOrderedList']/ul/li["+j+"]")));
                  j++;
            }
      }
      catch(Exception ex)
      {
                        ex.toString();
      }
}

public void verifyorderedListItems(ArrayList<String> OrderList)
{
      System.out.println("In SecondPage Ordered List Verfier Test Method");
      try
      {
            int j=1;
            while(selenium.isElementPresent("//div[@id='OrderList']/ol/li["+j+"]"))
            {
                  Assert.assertTrue(OrderList.contains(selenium.getText("//div[@id='OrderList']/ol/li["+j+"]")));
                  j++;
            }
      }
      catch(Exception ex)
      {
                        ex.toString();
      }
}

public void verifySecondPageLinks(ArrayList<String> OrderList)
{
      System.out.println("In SecondPage Links Verifier Method");
      try
      {
            int j=1;
            while(selenium.isElementPresent("//div[@id='Links']/a["+j+"]"))
            {
                  Assert.assertTrue(OrderList.contains(selenium.getText("//div[@id='Links']/a["+j+"]")));
                  j++;
            }
           
      }
      catch(Exception ex)
      {
                        ex.toString();
      }
}
}
Start Selenium Server. Run TestRun.java as TestNG test.  This is a working example. Please find output








More Reads
A Selenium CaptureNetworkTraffic Example in Java
Selenium Example – How to Amend the JavaScript of the Web Application Under Test
Automated Web/HTTP Profiler with Selenium-RC and Python
Happy Learning!!