"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!!

Selenium Automation Best Practices

[Next Post in Series - Automation Assignment - Automation of Cleartrip.com Homepage]
[You may also like - Selenium Automation Tricks From Selenium Wiki]

I am reading Selenium beginner's Guide. In Best practices section author refers to Page Object Pattern.
  • Moving Selenium methods into private methods to make it maintainable
  • Seperate classes for each page, This example is clearly illustrated - Homepage.java, Chapter1.java, Chapter2.java. This is very good.
  • I am going to try it out with different set of example with coding standards suggested for Java
Summarizing Coding Standards based on - Java Coding Standards Document
  • Comment your methods and add enough information for other team members to understand
  • Naming methods with full description and first letter not captitalized. Ex- openHomePage( ), openFirstPage( )
  • Assigning member function visibility private, public, protected based on usage
  • Declaring variables - searchKeyword, brandName, firstName (Hungarian Notation - hWindow, hMenuItems), Usage of _
  • Last Important thing is naming constants, static variables in CAPS - MAX_LIMIT, CHECK_VALUE
  • Always initialize static fields
  • Declare local variables immediately before their use
  • Prefix letter I for Interface  names
I would prefer online code formatter :). There might be some free tools available need to check.

Continuous Integration Server is something I need to learn :).....

Good Reads



Selenium Maven Plugin

January 15, 2011

Selenium - Parsing Lists and Links in a Page

Next Example is to try looping through Lists and Links in a page
Followed the below steps
1. Created Sample Test Page link
2. Create a project and add required libraries. Verify earlier post

Below is the Example Code - Working Code

import static org.testng.Assert.assertTrue;
import java.util.Vector;
import org.testng.Assert;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
public class TestLists
{
     
public static Selenium selenium;

@BeforeTest
public void openBrowser()
{
      System.out.println("Before Test");
      selenium = new DefaultSelenium("localhost", 4444, "*iehta","http://sqlandsiva.blogspot.com/2011/01/test-page-for-selenium-example.html");
}

@Test
public void LoopthruElement()
{
try
{
      System.out.println("Running Test1");
      selenium.start();
      selenium.open("http://sqlandsiva.blogspot.com/2011/01/test-page-for-selenium-example.html");
      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();
}
}
}

This is tested and working code. Run it as TestNG Test. Below is the output for the program













I used firebug XPATH copier to get exact Xpath location. Below is snapshot of the same















Happy Reading!!

Test Page for Selenium Example

An Ordered List:

  1. Order List One
  2. Order List Two
  3. Order List Three

An Unordered List:

  • Unordered List One
  • Unordered List Two
  • Unordered List Three

Java Coding Standards

I have to revise coding guidelines in Java. Below are useful reads for it.

Java Online Coding Formatter - Link
JAVA CODING STANDARDS

Two interesting questions listed below, I could find discussions related to that while I searched in google

More Interesting Reads

Happy Reading!! 

January 02, 2011

Good Read - How to Incorporate Testers into Agile Teams


Happy Reading!!

SQL Tip of the Day - BI Verdict Site

Today came across a new site bi-verdict. This website provides information on bi vendors, product reviews.

I was looking for review of QlikView's BI Product.
Product Review - QlikView, Busting 5 myths about QlikView

Happy Reading!!

January 01, 2011

Selenium Xpath Looping Example

Thanks to author for article Selenium IDE with an XPath Locator. I wanted to try the same example using TestNG code.

Followed the below steps
1. Created Sample Test Page link
2. Create a project and add required libraries. Verify earlier post

Below is the Example Code - Working Code

import static org.testng.Assert.assertTrue;
import java.util.Vector;
import org.testng.Assert;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
public class SeleniumXpath
{
     
public static Selenium selenium;

@BeforeTest
public void openBrowser()
{
      System.out.println("Before Test");
selenium = new DefaultSelenium("localhost", 4444, "*iehta","http://sqlandsiva.blogspot.com/2011/01/test-xml-page-for-selenium.html);
}

@Test
public void LoopthruElement()
{
try
{
      System.out.println("Running Test1");
      selenium.start();
      selenium.open("http://sqlandsiva.blogspot.com/2011/01/test-xml-page-for-selenium.html");
      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();
}
}
}

This is tested and working code. Run it as TestNG Test. I intend to add more examples in the page and improve on this example.

Happy Reading!!

Test XML page for Selenium

a1a2a3
b1b2b3
c1c2c3