Friday, February 13, 2015

The Undocumented documentation of Page Object Pattern

If you are into automation testing using selenium, you may be knowing about the Page Object Pattern suggested by selenium forum available at url :
https://code.google.com/p/selenium/wiki/PageObjects

The documentation suggests the approach for implementing Page Object Pattern for automation and what are the advantages of it. In here I wont be writing about what the documentation suggests and talks about it. So lets directly jump onto the main subject and learn about what the documentation does not contain. Following are those points:
  • Implementing a Page Object Pattern for a single page application
  • Approach towards defining methods for functionality

Implementing Page Object Pattern for a single page application


With new web application development technologies coming into play on a daily basis web applications has reduced to very few pages and even sometimes to one page url. This becomes a challenge while implementing Page Object Pattern. The question arises is, how to segregate the pages as there are only few pages in the application but there are lot of functionalities to cover in them. If we go ahead with the conventional segregation methodologies we will have very few page classes but with lot of functional methods defined in them. We will fall into the same problem of code management that we were trying to solve using Page Object Pattern.

The solution to this is to enhance the Page Object Pattern guidelines. This includes segregating your page classes based on features, sub-features, functionality etc. and not by page urls. To explain this we have a perfect example of Gmail. Gmail is a single page but there are lot of functionalities available in the said page like navigation, hangouts, inbox, Search, compose and mail folders.
If we go with the conventional method we will be forced to define all the functionalities of compose, hangouts contacts chat, inbox etc in a single class. With the enhanced model we will define separate classes for each of the feature ex. ComposePage, HangoutPage, InboxPage, NavigationPage etc. By using the enhanced model we can fully utilize the advantages of using the Page Object Pattern.

Defining methods for Page functionality


The guideline suggests users to define functionalities as method inside page classes and it ends there. It does not talk much about the segregation,  approach or anything of such sort. Anyways lets cover it here. When planning to define methods for the functionality try to follow following added guidelines:
  • Segregate your functionality into smaller functionally executed methods. Lets take an example of login use case where user enters username, password and click on submit button. While writing a functional method for login you should think of having methods like enter username, enter password and clicking on submit button. This way you will have more control on the code and you can provide more utilities to user who is writing automation tests.
  • Think of functional as well as structural method declarations. Taking the above example. login method that accepts username and password arguments is a functional method whereas enter username, enter password and click on submit button methods are structural methods. As said in the first point structural methods makes code more readable and represents a flow of user actions. It also provide more power into the hands of end user who is using them for writing automation tests.
  • Think of positive and negative scenarios of a functionality while defining methods. I will again take the example of the login functionality. A login can be successful or it can be failure. try to support both kind of scenarios in a single method. This can be achieved in two ways:
    • Allow a Boolean value to be passed as argument to the method that will decide whether the scenario for which the method is being used in positive or negative.
    • Another way is you written a Boolean or some other object as return type representing whether the method execution was success or a failure.
    This way you are covering more ground in the functional methods and giving more power to the user writing automation tests to write different types of tests scenarios with the same given method.

The above suggestion helps in making tests more readable, manageable and in providing more functionality to the user who is automating. The initial time taken to write page class and page methods will be more but there will be very less time spent on writing automation test cases for the said functionality as the methods list will contain methods that supports all kind of related functionality from low level to high level as well as both positive and negative scenarios.

This is a cross post from my company blog here, where I had originally written it.

Monday, February 9, 2015

Selenium Exceptions why it occurs and how to resolve/avoid them

While using selenium for web automation many a time we face numerous kind of exceptions. Many of these exceptions are mainly selenium exceptions. In this blog I will write about few of those selenium exceptions, why they occur and how to resolve/avoid them.

  • StaleElementReferenceException - 
    As the name suggests this exception occurs when the element stale, which means the element reference on which you are trying to take a action upon is no longer available on the page or has changed. 
    To avoid this exception, try to find the element as and when you need to take an action upon it rather than getting the element at some point of code and then reusing it at different places.

  • InvalidCoordinatesException - 
    This exception occurs when invalid co-ordinates are provided for an interaction operation like move, click, drag & drop etc.
    You can avoid this exception by identifying a Webelement on the page for interaction rather than providing a co-ordinates.

  • ElementNotVisibleException - 
    This exception occurs when an element may be available in the dom but not displayed on the page and hence not able to be interacted. This exception may also occurs if the element is unable to scroll onto the browser view.
    You can avoid this exception by verifying that the element is visible and then taking action on the element. There are some methods present in "ExpectedConditions" class that can be used to wait for the element to be visible. These methods are "visibilityOf(Webelement)" and "visibilityOfElementLocated(By)".

  • InvalidSelectorException -
    This exception is thrown when the selector used to find the element is invalid or wrong, this may be a syntax error or a compound class name to be provided to "By.className" method.
    To avoid this exception verify that the selector that you are using are verified on the said page using jQuery or firebug or firepath.

  • NoSuchElementException -
    This is a common exception faced by selenium users and occurs when there is no element found on the current page by selenium based on the user provided selector to method "findElement". There can be multiple reasons for this exception to occur like selector being wrong, element taking time load(in case of ajax), earlier step execution failed etc.
    You can avoid this exception by implementation by waiting for element to be present using "FluentWait" with "ExpectedConditions" class methods "presenceOfElementLocated(By)" and "visibilityOfElementLocated(By)".

  • UnreachableBrowserException -
    This exception mainly occurs due to two reasons first when the browser to which the selenium commands has died mid-test and second when the remote server address to which the RemoteWebDriver trying to contact is invalid.
    There is no proper solution to this error you can only take precautionary measures in this cases and is more or less related to hardware.

Tuesday, January 27, 2015

Developing a Web Automation framework using GEB and Groovy

I had extensively worked on Java and developed different types of frameworks depending upon the project requirements. I understand the pain points of developing the framework using Selenium and Java. I also understand it takes a lot of time to develop the framework to start supporting the automation for a particular application. So when I was given a responsibility of automating an application framework using Groovy I came across "GEB" which is a Web automation framework using groovy. It is a wrapper over selenium and provides a lot of in-built utilities that will allow the end user to jump into the job of automating their test-cases and validating their application within a short period.
Geb provides a enhanced Page Object modelling to its users with the advantage of re-usability and maintainability of tests and framework.  Page Object modelling/pattern as you may be knowing divides the areas on the UI under test as classes in the code. All the functionality related to the said area is put onto a class, hence reducing the probability of code duplicity.

Following are few of the advantages of using Geb:
- Uses webdriver as an internal web automation framework which is well tested and used worldwide
-  Uses css as the locating strategy which makes it faster and easy to use.
- Jquery like methods and usage
- Extensive list of in-built utilities that reduces code and time.
- In-built driver manager
- Support for parallel execution.

Its a complete automation framework package that reduces the automation framework development time by almost 2-3 months.
More details on Geb can be found at http://www.gebish.org/

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.



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


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.