"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 22, 2011

Test Automation Framework Using Selenium - Part I

[Prior Post in Series - Selenium Automation Best Practices]
[Next Post in Series Part II]


In our continious effort to develop automation framework please find high level approach to building automation framework. This is based on chapter. I have also noted coding guidelines in writing this code.
  • Coding Standards
  • High Level Approach 
Approach is independant test suites for individual pages in a site. Already in earlier posts I have created test pages. I am reusing the pages and verifier code. This is a draft outline of the framework.

TestRun.Java - In this file we instantiate selenium instance, open the page.
FirstPage.Java - This has methods for opening first page and verifying first page items
SecondPage.Java - Seperating methods and verifiers for that particular page to keep test suite maintainable. Verifier for second page is provided in this file

Improvements (Next Upcoming Posts)
  • Logging
  • Reporting
  • Continous Integration Server
  • Implementing Data Driven Testing by reading data from XML file
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 com.thoughtworks.selenium.Selenium;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
public class TestRun
{
Selenium selenium;

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

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

@Test
public void firstPageTestCase()
{
      FirstPage fp = new FirstPage(selenium);
      fp.clickFirstPage();
      fp.verifyListItems();
      SecondPage sp = fp.openSecondPage();
      sp.verifyUnorderedListItems();
}
}
3. FirstPage.Java
import com.thoughtworks.selenium.Selenium;
import static org.testng.Assert.assertTrue;
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()
{
      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+"]"))
            {
                  System.out.println("Value is " + 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.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()
{
      System.out.println("In SecondPage Test Method");
      try
      {
            int j=1;
            while(selenium.isElementPresent("//div[@id='OrderList']/ol/li["+j+"]"))
            {
                  System.out.println("Value is " + selenium.getText("//div[@id='OrderList']/ol/li["+j+"]"));
                  j++;
            }
            j=1;
           
            while(selenium.isElementPresent("//div[@id='UnOrderedList']/ul/li["+j+"]"))
            {
                  System.out.println("Value is " + selenium.getText("//div[@id='UnOrderedList']/ul/li["+j+"]"));
                  j++;
            }
            j=1;
            while(selenium.isElementPresent("//div[@id='Links']/a["+j+"]"))
            {
                  System.out.println("Value is " + 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
Output
Setting connection parameters:127.0.0.1:50050
[TestNG] Running:
  C:\Users\sivaram\AppData\Local\Temp\testng-eclipse\testng-customsuite.xml
Opening First Page
In FirstPage Test Method
Running FirstPage Test
Value is a1
Value is a2
Value is a3
Value is b1
Value is b2
Value is b3
Value is c1
Value is c2
Value is c3
Opening Second Page
In SecondPage Test Method
Value is Order List One
Value is Order List Two
Value is Order List Three
Value is Unordered List One
Value is Unordered List Two
Value is Unordered List Three
Value is Visit sqlandsiva!
Value is Visit brightlife4all!
PASSED: firstPageTestCase

===============================================
    TestRun
    Tests run: 1, Failures: 0, Skips: 0
===============================================
==============================================
javaTestng
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Happy Learning!!

1 comment:

Krishna said...

Siva sir,

Good article and sampletest programs. Very useful to understand the test program development using Selenium-RC (Java API) and TestNG.

Thank you, Krishna