"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" ;
Showing posts with label Selenium - Test Automation Series. Show all posts
Showing posts with label Selenium - Test Automation Series. Show all posts

April 21, 2014

Test Automation Framework Series

[Previous Post - Test Automation Framework Using Selenium - Part IV]

This is in continuation with Test Automation Framework Series. After 3 years I found step-by-step framework examples. As you see this reflects my stack overflow answer (Build first, Iterate and Improve). Rest of cleartrip test series / code improvements are covered in ClearTrip Tests (Very Good Walk through)

Project 1 - Selenium examples
Project 2 - ClearTrip Test. Downloaded and compiled the project. This list of 13 tests are great examples

I had also posted cleartrip automation assignment  earlier. (My incremental learning series :))

Another interesting post in similar lines - Evolution of Software Engineer. In the similar fashion Automation Framework Exercise evolves based on continuous feedback

Very Interesting slide from presentation in link. Similar to my learning path :)



Github Test Automation projects
Test Automation Framework (TAF)
Git Code

Selenium Test Framework
Selenium Test Framework

Happy Learning!!!

May 06, 2011

Test Automation Framework Using Selenium - Part IV

[Next Post in Series - Selenium Automation Tricks From Selenium Wiki]
[You may also like - Selenium Automation Best Practices]


In continuation with previous posts we are now going to look at running cross browser tests and specifying methods to run for each test run.

We need to edit the TestNG xml file as provided below. Below is edited XML file. You can find test's grouped, URL, browser sent as parameter.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
      <listeners>
   <listener class-name="SuiteReporter" />
   <listener class-name="TestReporter" />
   </listeners>
   <test name="TestinInternetExplorer">
   <parameter name="browserpath" value="*iehta"/>
   <parameter name="appurl" value="http://sqlandsiva.blogspot.com/"/>
      <classes>
      <class name="TestRun"/>
      <methods>
        <include name="firstPageTestCase" />
        <include name="secondPageVerifyUnOrderList" /> 
        <include name="secondPageVerifyLinks" /> 
        <include name="secondPageVerifyOrderList" /> 
      </methods> 
    </classes> 
   </test>
   <test name="TestinFirefox">
   <parameter name="browserpath" value="*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"/>
   <parameter name="appurl" value="http://sqlandsiva.blogspot.com/"/>
   <classes>
      <class name="TestRun"/>
      <methods> 
        <include name="firstPageTestCase" />
        <include name="secondPageVerifyUnOrderList" /> 
        <include name="secondPageVerifyLinks" /> 
        <include name="secondPageVerifyOrderList" /> 
      </methods> 
    </classes> 
   </test>
</suite>

Along with this change we also need to replace below method in TestRun.java file
Current Method
@BeforeTest
public void setUp()
{
      selenium = new DefaultSelenium("localhost",4444,"*iehta","http://sqlandsiva.blogspot.com/");
      selenium.start();
}

Changed / Replaced Method
      @Parameters( { "browserpath", "appurl" })
      @BeforeTest(alwaysRun = true)
      public void init(String browserpath, String appurl) {
            selenium = new DefaultSelenium("localhost", 4444, browserpath, appurl);
            selenium.start();
            selenium.deleteAllVisibleCookies();
            selenium.refresh();
      }

You can right click on XML and run it as TestNG test. Below would be result of TestNG test.


Improvements (Next Upcoming Posts)
  • Next focus is on re-running failed test cases
  • Grouping of Test cases (Will write a small example for it)
  • Threads and running a test several times (Planning for another small example script)
Stackoverflow site is awesome and very good source of learning

More Reads
Running TestNG tests from command line

Happy Learning!! 

February 18, 2011

Test Automation Framework Using Selenium - Part III

[Next Post in Series]


In continuation with previous posts we are now going to look at logging. Please refer to TestNG earlier posts for examples. I attribute my learning on logging to post. Thanks to Bindu Laxminarayan.

Added two files SuiteReporter.java and TestReporter.java. You can copy/paste the contents of file from the blog post. You can right click on TestRun.java and select option Convert to TestNG as per below snapshot

We need to edit the xml file as provided below testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
      <listeners>
   <listener class-name="SuiteReporter" />
   <listener class-name="TestReporter" />
   </listeners>
   <test name="Test" preserve-order="false">
    <classes>
      <class name="TestRun"/>
    </classes>
  </test>
</suite>

You can run TestRun.java as TestNG test. In the console window you will see below entries logged


After running TestNG test you would also see emailable-report.html. Please find snapshot



Improvements (Next Upcoming Posts)
  • Continous Integration Server
Pending Learning are below topics
  • Watir, Sahi
  • JavaScript, Python, Ruby
We have successfully covered basics of automation framework design, coding standards, data driven testing, reports and Logging as well. You can install Eclipse TestNG plug in from link

More Reads -
Better reporting with ReportNG
Using Firebug for Load Testing
TestNG XML Driven Parameters
Using Firebug in IE
How to setup a Test Automation Infrastructure using Selenium, JUnit, Hudson & ANT

Happy Reading!!

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