Wednesday, October 2, 2013

Generate ReportNG report using Maven

Earlier I had covered in my blog about generating TestNg-xslt report for your TestNG test execution using Maven. Following is the link to it:


In this blog I will write about how to generate a ReportNG report for your TestNG test execution using maven. Following is a pom.xml file that can be used to execute your tests in a maven project and also generate a ReportNG report after execution.


 4.0.0
 com.test.maven
 sample-maven-build
 1
 sample-maven-build
 
  
  src
  
   
   
    org.apache.maven.plugins
    maven-surefire-plugin
    2.14.1
    
     
     
      testng.xml
     
     
     
     
         
           listener
           org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter
         
     
    
   
   
    maven-compiler-plugin
    
      1.7 
     1.7
    
   
  
 
 
  
  
   com.google.inject
   guice
   3.0
   test
  
  
   org.uncommons
   reportng
   1.1.4
  
  
   org.testng
   testng
   6.3.1
  
 



Run the following command to run your test cases and generate the ReportNG report.

mvn clean test

Or just

mvn test

Once run by default the execution report can be found under the folder named "html" under the surefire report directory inside "target" folder of your project folder.

Wednesday, September 4, 2013

Page Object Pattern - What and Why?

Today I will be writing about Page Objet pattern which became famous when details about it was mentioned in the selenium wiki.

Page Object pattern solves one of the biggest problem that any automation framework face i.e Managing utils, managing methods, reducing code duplicity etc. It solves this by suggesting certain methodology for managing and modelling the code.

Some of the main points that it suggests are:
1. Model your application UI pages as classes in your framework. (There is no hard-and-fast rule that the whole page on the UI should be represented by one class, you can also create classes for the sections of the page if they are used in other UI pages or sections.)
2. Each class will contain methods that perform the actual functionality provided on the page or section that the said class represents.
3. Each method in the Page class should return the object of the same page class or another page class if the said method/functionality navigate the UI to a new page.
4. Method names inside the page class should represent the actual functionality that it performs.

Let’s take this by an example:
We’ll take the same example as mentioned in the Selenium wiki, you have a login page where user enters username and password. If the login is successful user will get navigated to the home page else an error will be thrown on the UI.

Now converting this use case to a Page Object pattern we’ll have two classes LoginPage and HomePage defined. LoginPage will contains functions related to entering username, entering password, login using a given username and password, clicking on submit button etc. HomePage will contain functionality related to the HomePage, in this case we will have the functionality to verify the HomePage. Below is a class representation of the Pages and their respective functionality defined as methods. There is also a sample test-class with name "SampleTest" which contains two test methods that makes the use of the Page Object Pattern defined using "LoginPage" and "HomePage" class


public class LoginPage {
    /**
     * Type username and password into the respective fields and click on submit button.
     * Returns the HomePage class object for successful login
     */
    public HomePage loginAs(String username, String password) {
    }
    /**
     * Used to check whether the login fails for a said username/password.
     * Type username and password into the respective fields and click on submit button.
     * Returns the LoginPage class object for failure login
     */

    public LoginPage loginAsWithFailure(String username, String password) {
    }
    /**
     * Type passed username value into the username field
     * Returns the LoginPage class object
     */
    public LoginPage typeUsername(String username) {
    }
    /**
     * Type passed password value into the password field
     * Returns the LoginPage class object
     */
    public LoginPage typePassword(String password) {
    }
    /**
     * Click on the submit button on the page
     * Returns the HomePage class object
     */
    public HomePage clickOnSubmit() {
    }
}

public class HomePage {
    /**
     * Verifies the said page is the Home page
     */
    public boolean verifyHomePage() {
    }
}

public class SampleTest {
    @Test
    public void verifyThatLoginIsSuccessFul() {
        LoginPage loginPage = new LoginPage();
        loginPage.typeUsername("username");
        loginPage.typePassword("password");
        HomePage homepage = loginPage.clickOnSubmit();
        Assert.assertTrue(homepage.verifyHomePage(),"User not at homepage");
    }

    @Test
    public void verifyThatLoginIsSuccessFulSecond() {
        LoginPage loginPage = new LoginPage();
        HomePage homepage = loginPage.typeUsername("username")
                                     .typePassword("password")
                                     .clickOnSubmit();
        Assert.assertTrue(homepage.verifyHomePage(),"User not at homepage");
    }
}
As you can see from the above test-method in the above “SampleTest” class, there two different test-methods. Both the test-methods have the same flow defined in them , the only difference is the way they had been written. Also you can notice that the test are more readable when implemented by following the Page Object model. Without a  Page Object Pattern the tests may have been more cluttered and non-readable.

Advantages of using Page Object pattern:
- Better code management
- Reduced code duplicity
- In case of change in functionality very minimal changes required to be done. Change the respective page method and it will automatically take effect in all test-methods.
- Testcases are more readable.

Wednesday, August 15, 2012

CSS Locator / CSS Selector Tutorial

CSS locating strategy is the fastest way to identify an element compared to any other locating strategy currently available. Many of the automation frameworks now uses CSS as a locating strategy for locating elements. In this blog I will explain about how to identify an element using CSS locators.

1. Locate an element using id.
<input id="textid" value="textid" type="text"/>    
<button id="buttonid">Button wiht id</button>

Consider the above html code of a text box and a button, you can locate the above elements in html using there "id" attribute value.
In CSS  an element with id is identified by appending "#" to the start of its id attribute value. For ex the above text element can be located as "#textid". Similarly the button element can be identified as "#buttonid"


2. Locate an element using class attribute.
<input class="textusingcls" value="Text with single class" type="text"/>    
<button class="btnusingcls">Button with single Class</button>

Consider the above html code of a text box and a button, you can locate the above elements in html using there "class" attribute value.
In CSS  an element with class is identified by appending "." to the start of its class attribute value. For ex the above text element can be located as ".textusingcls". Similarly the button element can be identified as ".btnusingcls"

Locating an element having more than one class, for ex. consider the following html code.
<input class="textcls1 textcls2" value="Text with multiple class" type="text"/>    
<button class="buttoncls1 buttoncls2">Button with multiple Class</button>
The above element can be identified by replacing the spaces between the class values with "." . The above elements can be located as ".textcls1.textcls2" and ".buttoncls1.buttoncls2" respectively.

3. Locate an element using html tag.
<input id="textid" value="textid" type="text"/>    
<button id="buttonid">Button wiht id</button>

Consider the above html code of a text box and a button. To locate the above elements using there html tags in css, you just have to use the tag name. For ex. The above two elements can be located as "input" and "button" respectively.

4. Locate an element using html tag, class, id all together.
<input id="textidcls" class="textidcls" value="Text with id and class" type="text"/>    
<button id="buttonidcls" class="buttonidcls">Button with id and Class</button>

Consider the above html code of a text box and a button. You can locate the above element using a combination of html tag, id and class all together. To do that just append all the three independent locator together. For the above element the unique locators will be "input#textidcls.textidcls" and "button#buttonidcls.buttonidcls" respectively.

5. Locate element based on Parent element:
<input class="textcls" value="Text" type="text"/>  
<button class="buttoncls">Button</button>
<div id="parent">
    <div id="project-label">4. Locate Child element:</div>
    <input class="textcls" value="Text" type="text"/>    
    <button class="buttoncls">Button</button>
    <div>
        <input class="textcls" value="Text" type="text"/>    
        <button class="buttoncls">Button</button>
    </div>
</div>

Consider the above html code, if you try to find the text element using css locator ".textcls" you will get 3 elements in return. In case you need to find the text box under the div element with id "parent" you can locate it using the locator "#parent .textcls", but this also gives you 2 text box elements that are under the element with id "parent".
To find the direct child element of the "#parent" element you can use the ">" sign of css locator. So if you use the locator "#parent > .textcls" it will give you the direct child of "parent" element.
In css if you use space between the parent and child element, it will return all the child elements under the said parent irrespective of the hierarchy.

6. Identify an element using attribute value:
<input test="excttxt" value="Text for exact attr. value" type="text"/> </br>
<input test="strttxt1234" value="Text for starts with option" type="text"/> </br>
<input test="1234endtxt" value="Text for ends with option" type="text"/> </br>
<input test="12cntntxt34" value="Text for contains option" type="text"/> </br>

Consider the above html code, there are 4 text boxes each having a different value for attribute "test".Each element can be uniquely identified by using the following strategies:
  • By exact attribute value: 'input[test="excttxt"] '
  • By using starts with: 'input[test^="strttxt"]'
  • By using ends with: 'input[test$="endtxt"]'
  • By using contains: 'input[test*="cntntxt"]'

I had created a dummy html page which can be download from the following link: CSS-Locator-demo
To use the demo for identifying a locator:
1. Unzip the downloaded zip and open the page "css_locator.html" in chrome or firefox.
2. Now go to console in the browser. For Chrome: Right Click -> Inspect Element -> Console. For Firefox: Right Click ->Inspect with Firebug -> Console.
3. Use the jquery way of identifying element in the cosole. To do that type $('<Ur CSS locator>') under console and press enter.
Jquery will find the matching element for the said css locator and display it on the console as a link. If you hover on the said link it will highlight the said element.

More info on CSS selector at W3C link

Wednesday, July 18, 2012

Android automation tools/frameworks


As we all know the market of android based devices is increasing day by day. Due to this lot of companies are developing android based applications. These applications can be categorized as following:
  • Native apps - Apps that are developed using native android classes and runs on the device android OS.
  • Webapps - These apps are actually web-applications  that are customized to work with Android based mobile devices.
  • Hybrid apps - These apps are the mix of Native and Web based development. Such kinds of apps have some Native coding to interact with device hardware resources like camera,recorder, speaker etc. along with some web based components to directly serve the pages from a web-application itself.
With such a variety of apps in the market the QA will have a difficult task in hand, that is to automate such applications. There are a lot of tools and frameworks available in the market that can help you with automating the android apps. In this blog I will list a few of them and basic info about it. Following are some of the opensource apps available in the market:
  • Robotium - Android automation framework built over Android instrumentation testing provided by the Android itself. This is currently one of the major frameworks available that can be used a base for your android app automation. More info at: http://code.google.com/p/robotium/
  • NativeDriver - Extension of Selenium webdriver for Native android automation.This currently discontinued and no further support or development is being done on it. More info at: http://code.google.com/p/nativedriver/
  • Bot-bot - An Opensource android automation project built over Robotium and NativeDriver frameworks. Comes with recording features to record user actions and export them as test case. It currently supports recording user actions of native application elements and new work is going on to support recording on Web-based/Hybrid apps. More info at: http://imaginea.github.com/bot-bot/
  • MonkeyTalk -  Monkey talk is based on Android instrumentation testing provided by android. It have recorder which can be used as a eclipse plugin. The tool seems to be unstable and there is not much changes/ bug fixes that is seen.
  • Calabash - Calabash is another android/ios automation tool available in the market which uses cucumber as its base. More info is available at: https://github.com/calabash/calabash-android
Will keep on updating the list whenever I find something new.

Saturday, May 12, 2012

Generate TestNG-xslt report using Maven

Earlier I had covered in my blog on how to generate a TestNG-xslt report for your TestNG test-case execution using Ant. Following is the link to it:
http://blog.varunin.com/2010/05/generating-selenium-reports-using.html

As many of the recent projects are now using maven as their build tool ,test-cases are also required to integrate itself to the existing build. In this blog I will mention about how to generate a TestNG-xslt report for your TestNG test-case execution using Maven.
Following is the "pom.xml" file for executing TestNG test-cases and to generate a "TestNG-xslt" report for the same:


  4.0.0
  com.testng.app
  my-testng
  jar
  my-testng
  1.0-SNAPSHOT
  http://maven.apache.org
 
  
  src
    
      
        org.apache.maven.plugins
        maven-surefire-plugin
        2.14.1
        
          
            testng.xml
          
        
      
      
        maven-compiler-plugin
        
          iso-8859-1
          1.7
          1.7
        
      
    
  
 
  
    
    
      
        org.reportyng
        reporty-ng
        1.2
        
          
          /target/testng-xslt-report
          true
          FAIL,SKIP,PASS,CONF,BY_CLASS
          true
        
      
    
  
 
  
    
      org.testng
      testng
      6.3.1
    
  
 
  
    
      reporty-ng
      https://github.com/cosminaru/reporty-ng/raw/master/dist/maven
    
  
 

Run the following command in terminal/command prompt to run your test cases and generate the TestNG-xslt report. The report will be generated under folder named "testng-xslt-report" inside the "target" folder under your build directory.

mvn clean test site org.reportyng:reporty-ng:1.2:reportyng

To know more about the TestNG-xslt configuration parameters please refer to my earlier blog post:
http://blog.varunin.com/2010/05/generating-selenium-reports-using.html

Wednesday, February 8, 2012

Native android application automation using bot-bot

Its been long time that I had written a blog here. I was busy with writing an android automation tool for native android application testing, named as "bot-bot". Please take a look at it and let us know your comments. This is work-in-progress and there are lot-of features that still have to implement.

Following are some of the links giving information on bot-bot:
Webpage:
http://imaginea.github.com/bot-bot/

Github page:
https://github.com/Imaginea/bot-bot

Most of the things are covered in the webpage or in Github.
In case of issues you can contact me or send a mail.

Tuesday, October 25, 2011

Selenium Grid with WebDriver

I had earlier covered in my blog on how to execute your RC cases in selenium grid. But with the commencement of Selenium-2/Webdriver, grid setup has been changed. In the following blog I will cover how to set-up your grid and the changes that will be required for easy execution of cases.


Following things are required:
1. Selenium 2.xx version server jar and Java library. The latest one can be downloaded from the link: Selenium-2
2. Java 1.6 and above
3. TestNg jar . You can download it from the link: TestNG
4. Eclipse with TestNG plugin installed(optional)
5. ChromeDriver. Can be downloaded from: ChromeDriver


The Test Code
Following is an example of test-class that have a test case to search google for "testing" and verifying after clicking it on a link.

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.Selenium;

import junit.framework.Assert;

public class Google {
 private Selenium selenium;
 @Parameters({"browser","port"})
 @BeforeClass
 public void beforeClass(String browser,String port){
  DesiredCapabilities capability= new DesiredCapabilities();
  capability.setBrowserName(browser);
  try {
   WebDriver driver= new RemoteWebDriver(new URL("http://localhost:".concat(port).concat("/wd/hub")), capability);
   selenium = new WebDriverBackedSelenium(driver, "http://www.google.com");
  } catch (MalformedURLException e) {
   
   e.printStackTrace();
  }
 }
 
 @Test
 public void search() {
  selenium.open("/");
  selenium.type("id=lst-ib", "testing");
  selenium.click("//input[@value='Google Search']");
  for (int second = 0;; second++) {
   if (second >= 60)
    Assert.fail("timeout");
   try {
    if (selenium
      .isElementPresent("link=Software testing - Wikipedia, the free encyclopedia"))
     break;
   } catch (Exception e) {
   }
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  selenium.click("link=Software testing - Wikipedia, the free encyclopedia");
  for (int second = 0;; second++) {
   if (second >= 60)
    Assert.fail("timeout");
   try {
    if (selenium.isTextPresent("Software testing"))
     break;
   } catch (Exception e) {
   }
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {

    e.printStackTrace();
   }
  }
 }
 @AfterClass
 public void afterClass(){
  selenium.stop();
 }
 
}
In the above class I am using the TestNG "Parameter" property to provide different data set to the "BeforeClass" method "beforeClass". The beforeClass method accepts two properties "browser" and a "port".
These values are used for initialization of driver and in-turn for initialization of selenium object. In the above code I am using the "WebDriverBackedSelenium" class for creation of the selenium object, so that its easy for guys who had worked on Selenium-1 to understand the code. If you want the code to be purely WebDriver, you can directly use the "driver" object for defining your test-cases.
The main part in this test case is how the driver object is being created:
DesiredCapabilities capability= new DesiredCapabilities();
capability.setBrowserName(browser);
WebDriver driver= new RemoteWebDriver(new URL("http://localhost:".concat(port).concat("/wd/hub")), capability);
The above code creates an object of DesiredCapability and then set the browser value to it. Now using this "capability" object I am creating the webdriver object using the "RemoteWebDriver" class. This tells the selenium on which browser the test-case needs to run and where the server is located. In this example I am assuming the server to be running locally. In case it is on different system the "localhost" needs to be re-placed with the ip of the said system.

TestNG configuration
For parallel execution you need to use the TestNG configuration. Following is an "testng.xml" file for the above said test class. The said configuration executes the test-cases across different browser.


<suite name="Selenium TestNG Suite" parallel="tests"
    thread-count="5">

    <test name="Selenium TestNG - 1">
        <parameter name="browser" value="firefox" />
        <parameter name="port" value="4444" />
        <classes>
            <class name="com.test.testng.Google" />
        </classes>
    </test>
    <test name="Selenium TestNG - 2">
        <parameter name="browser" value="chrome" />
        <parameter name="port" value="4444" />
        <classes>
            <class name="com.test.testng.Google" />
        </classes>
    </test>

</suite>


In the above configuration, I am configuring TestNG to run "tests" in parallel. Also there are two different tests inside a suite. For each test a different "browser" parameter value has been configured.

Selenium-Grid server
Now start your grid using the following commands. Run each command in a seperate terminal or command prompt by going to the directory containing your selenium-server-standalone-2.x.x.jar. In the following example I am using the 2.7.0 version of selenium.

For Hub:

java -jar selenium-server-standalone-2.7.0.jar -role hub


For a firefox based node:

java -jar selenium-server-standalone-2.7.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=firefox


For google-chrome based node:

java -Dwebdriver.chrome.driver=/path/to/chromedriver -jar selenium-server-standalone-2.7.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555 -browser browserName=chrome

Before running  the above command  you need to provide the chrome-driver path to the property "-Dwebdriver.chrome.driver".

Now run your testng.xml from eclipse by selecting it -> Right click -> Run as -> TestNG suite(this will work only if you have TestNG plugin installed in your eclipse.)
Or you can choose other ways to execute "testng.xml" like from command prompt, using ant or maven.
Once you run the above testng.xml. TestNG will execute the cases from the Google class on grid across different browsers firefox and google-chrome as configured.


You can do configurations for environment like OS (Linux,Windows), browser, browser-version and all and then you can run you cases on particular type of environment by configuring them accordingly. More details can be found at the following URL:
http://code.google.com/p/selenium/wiki/Grid2