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

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

No comments: