Saturday, 12 April 2014

Object Identification in Selenium




Object Identification:

Object identification is a very crucial part of any automation effort irrespective of the automation tool we may use like QTP, selenium or any other. For every automation engineer, It is very important to identify the objects used in the automation effort effectively.

Object identification, to be more precise, objects being identified accurately demands certain steps to be followed and is certainly not a straight forward experience in Selenium RC or Selenium WebDriver.

Selenium WebDriver offers a range of locators to capture a specific object in AUT, like – ID, Name, Xpath, CSS, etc. Most of the times, user has to make use of Selenium IDE, Firebug or Firepath to identify the objects. There are few limitations with the traditional approach:

§  User wants to verify text without triggering an event on the application (like Click)
§  Selenium-IDE frequently generates index-based Xpath, which is not the right approach as maintainability becomes an issue
§  Selenium -IDE locators work only on single HTML reference at a time. Whereas web technology allows nested HTML structure with frames. If you want your Selenium WebDriver to act on an object which belongs to frame/iframe, you must select the frame first.

There are some tools such as Firebug and Firepath that can provide a much deeper understanding of the objects. The drawback here is that it demands understanding of HTML DOM to help you decide how to deal with objects to derive the best object identification locator.

Identifying Objects is Selenium (WebDriver) is pretty simple if you are working on an application built on Firefox, but it gets very tricky if you are working on IE application. So it would be best to master the object identification techniques.

If you have used the Selenium IDE for any recording you would have noticed that there are multiple ways of identifying an object. By default the Selenium IDE tries to recognize the object by the ID or name attribute. Selenium IDE identifies the objects by the attributes mentioned in the order below:

§  ID
§  Name
§  Link Text
§  CSS Selector
     Based on Combination of HTML tag, id, class, and attributes
     Tag and ID (e.g., css=a#test)
     Tag and Class (e.g., css=a.test)
  • Tag and Attribute (e.g., css=input[name=lastName])
§  Xpath – Attributes
§  Xpath – Relative
§  Xpath – Absolute or Position
§  DOM
     getElementById
     getElementsByName
     dom:name
     dom:index

Example:  When we are identifying a search text box, we can locate the object using different locators as shown below.




When we record a simple step of clicking on the “Google Search” button on the home page. If you can convert the recorded test step to Java – JUnit 4 – WebDriver (Options -> Format -> JUnit 4 – WebDriver). The code for clicking the “Google Search” button using ID attribute would look like this -

driver.findElement(By.id("gbqfb")).click();

for name

driver.findElement(By.name("btnG")).click();

for cssSelector

driver.findElement(By.cssSelector("#gbqfb")).click();

for Xpath Attributes

driver.findElement(By.xpath("//button[@id='gbqfb']")).click();

for Xpath Relative

driver.findElement(By.xpath("//div[@id='gbqfbw']/button")).click();

for Xpath Position / Absolute

driver.findElement(By.xpath("//button")).click();







If you prefer not to use Selenium IDE you can use Firebug or Firepath to identify the objects

Firebug:



Example: Identifying a object locator using Firebug



Firepath:



Example: Identifying a object locator using Firepath


Object Maintenance:

How do we manage objects in the repository efficiently so that we can adapt any change in future in a very efficient manner?

Applications go through constant change with the additional of new screens / objects or changes to the existing. We must know which objects are already present in the repository and which objects in AUT have undergone a change and require update in Object Repository. Many are lost in this phase, ending up with relearning the objects / making changes to object without knowing the impacted test cases. As a result you have to maintain duplicate objects or notice unnecessary failures during execution.

As mentioned earlier, the first step to help with Object Maintenance is Object Identification. Smarter object identification helps ensure that you only have to deal with object maintenance, if an object has truly undergone a change.

An automation tool that can help you understand the impact of any object change, like “the list of test cases impacted by an object change” will save lot of maintenance time. Unfortunately, most of the available automation tools identify the change in application only after a failure has occurred during execution!

Impact of Object Management on Automation ROI

Object management that looks beyond the basics of object identification will have a much larger impact on ROI from a test automation exercise. Incomplete and under-utilized Object Management will have adverse consequences such as:

§     Productivity slows down
§     Maintenance efforts increase
§     Overall cost is increased as against anticipated savings
§     Above all, readiness of automation suite is not guaranteed when required



 By
Automation Mentor

We provide hands-on training on automation tools and frameworks

Saturday, 15 March 2014

Selenium WebDriver - Concepts & Architecture



Selenium - Introduction

Every possible automation scope is looking for the cost effective solution. As the word itself suggests “Cost”, people are very much inclined towards the open source automation tools and finally ending up with “Selenium”.

Why Selenium?
  • Open source
  • Easy to Use
  • Supports multiple languages, platforms & browsers
  • Faster execution
  • Reliable

Evolution of Selenium:

Developed by Jason Huggins at thoughtworks to perform the repeated tests and actions on his development project with different browsers and eventually creating a library of java scripts to interact with web browser.

Core components of Selenium

  • Selenium IDE – Is a Firefox add-on that records user actions performed on the browser to make a test, which can be played back again.
  • Selenium RC – Used to run the tests in multiple browsers and platforms by improving the test based on the language and test requirements.
  • Selenium GRID - Extends Selenium RC and WebDriver to distribute your tests across multiple machines/servers to attain quick test execution.


Selenium 2.0 - WebDriver architecture:

Let’s talk about the Selenium WebDriver and its architecture and some of the capabilities and extensions which can add to WebDriver object to address some of the important automation scenarios.


WebDriver makes direct calls to the browser using browser’s native support for automation.  It uses the approach of firing events at the OS level and is only possible where WebDriver can bind closely with the browser and how best to it can send native events without requiring the browser window to be focused.




WebDriver provides an object oriented API to use page object patterns of automation and is designed to accurately simulate the way that a user will interact with a web application. It emphasizes the exact manual user actions on the web application.

For example : Remote control server will be able to click on any element on the page even though the element or the object is not in the visible state, because RC relies completely on java script interpretations.

Assume there an element which is visible only after the mouse over happens on the main navigation bar. WebDriver will emphasize user to perform the mouse over and then click on the visible link below it.


Interesting capabilities of Selenium 2 - WebDriver

  • It has the ability to control elements on the page like a user would. Like dragging items around a page or even perform the mouse, or keyboard actions at very low-level to do some the good things  like drag and drop

    For example, moving the e-mails from inbox to sub-folders in yahoo or gmail.
Sample script for drag and drop

 Actions builder = new Actions(driver);

 Action dragAndDrop = builder.clickAndHold(<element to click >)
       .moveToElement(<target element to move>)
       .release(<target element to release>)
       .build();
  dragAndDrop.perform();

Even holding a key and performing some other actions likes typing

builder.keyDown(Keys.CONTROL)
   .click(<Element to click>)
   .click(<Other Element to click>)
   .keyUp(Keys.CONTROL);
  • Often we may end up in debugging our automation test scripts during the run time. In order to do so, we need have the addon ready and handy to pause the script execution and use external addons.

    We can bind the add-ons like Selenium
    IDE and firebug to browser while launching the browser using webdriver object with the help of profiling concept.

String IDEpath = "C:\\FF_Profile\\seleniumide.xpi";
FirefoxProfile profile = new FirefoxProfile();      
profile.addExtension(new File(IDEpath));

  • Interesting part in the above method will help us even changing the HTTP headers of the Firefox browser so that normal windows browser will be treated as simple mobile browser and as if the application is being tested on the mobile device/browser.

\
However this kind of situation we may come across when the automation engineer has to automate and test application as if the HTTP request coming from mobile browser.

In the below example, by changing the HTTP header information of the browser, any request sent from browser with modified headers will be considered as mobile browser request by the webserver.

File fl = new File("..\\Inputs\\modify_headers.xpi");
profile.addExtension(fl);
profile.setPreference("modifyheaders.config.active", true);
profile.setPreference("modifyheaders.config.alwaysOn", true);
profile.setPreference("modifyheaders.headers.count", 3);
profile.setPreference("modifyheaders.headers.action0", "Add");
profile.setPreference("modifyheaders.headers.name0", "X-Nokia-msisdn");
profile.setPreference("modifyheaders.headers.value0", "123456878901");
profile.setPreference("modifyheaders.headers.enabled0", true);
profile.setPreference("modifyheaders.headers.action1", "Add");
profile.setPreference("modifyheaders.headers.name1", "x-sec-pass");
profile.setPreference("modifyheaders.headers.value1", "msdp@2010");
profile.setPreference("modifyheaders.headers.enabled1", true);
profile.setPreference("modifyheaders.headers.action2", "Add");
profile.setPreference("modifyheaders.headers.name2", "User-Agent");
profile.setPreference("modifyheaders.headers.value2", "NokiaN95_8GB");
profile.setPreference("modifyheaders.headers.enabled2", true);


 By
Automation Mentor

We provide hands-on training on automation tools and frameworks

Thursday, 6 March 2014

How to choose the right automation framework




Introduction

Automation framework is a set of guidelines / standards / programs / tools defined to improve the productivity of automation resources with better reusability of scripts and ease of use. Framework should also simplify the maintenance effort of the automated tests. If you build / use a strong automation framework, you don't need highly experienced automation resources to build automated tests. Even a junior automation resource with basic understanding on the tool can automate a product if the framework is built to address all the necessary features to automate given product.

Framework should also improve the reporting ability of automated scripts, with better visibility of failures with use of screenshots, logs and, videos in some cases.

The framework should help organizations get better ROI on automation projects.

Can I automate without a framework?

Yes! It is not mandatory to use a framework to implement automation.
However, the automation script will become cumbersome as more and more tests are automated without a strong framework. Maintenance of such scripts becomes huge overhead for the automation teams and may lead to abandonment of total automation initiatives.

Sometimes, automation without frameworks (mostly Record & Playback approach) may work for santiy tests with very few test cases (less than 30). Investing lot of time on frameworks for such smaller initiatives may not be feasible.

Automation frameworks

There are different automation frameworks available / used in the industry for different needs. While most of them are custom built, some of the frameworks for open source tools are are readily available for use with basic features.

Most commonly used frameworks in the industry include Data-driven, Modular, Keyword-driven and Hybrid. Each framework can be built with basic & advanced features.

Data-driven Framework:

  • Used when same tests are performed with different data sets
  • Tests & Test Data are represented by separate data sets for each feature
  • Easy to add/edit tests & test data
  • Test data maintained in external data source like excel, databases, flat files etc.

Modular Framework:

  • Used for small & stabilized applications with limited independent features
  • Each test is represented by separate module / function
  • Test data is passed as arguments to functions


Keyword-driven Framework:

  • Actions & transactions are represented by simple keywords
  • One or more records in Datasheet for each manual test step
  • Easy to understand / implement / maintain code

Hybrid Framework:

  • Combination of multiple frameworks
  • Usually it’s keyword-driven framework with data-driven features
  • Common data source is used for different types of tests


There is a lot of buzz in the market for script-less automation tools / frameworks these days. These frameworks allow non-technical manual testers and business users automate their own test/use cases without much understanding on the automation tools, programming and frameworks.

Why so many automation frameworks?

Each disease needs different medicine. Each of these frameworks solves different issues.
Here are the advantages & dis-advantages of different frameworks.

Advantages
Dis-advantages
Data-driven Framework
§  Easy to develop & maintain
§  Easy to automate using this framework
§  Takes less effort to develop
§  Suitable only for tests with repeated data sets
§  Need to build separate script & data sets for each functionality
Modular Framework
§  Not quite complex to build
§  Reusability of code to some extend
§  Well organized and easy to understand
§  Maintenance effort of scripts is very high
§  Difficult to automate large projects using this framework
Keyword-driven Framework
§  High reusability of code
§  Flexible and easy to use
§  Improves productivity significantly
§  Reduces the script maintenance effort
§  Takes lot of time to build
§  Need advanced automation engineers to develop the framework

Hybrid Framework
§  Comes with several features together
§  Flexible and easy to use
§  Improves productivity significantly
§  Reduces the script maintenance effort
§  Takes lot of time to build
§  Need advanced automation engineers to develop the framework



Which framework do I need?

Success of an automation project starts with the selection of right framework. Here are different framework recommendations based on the application nature, size and complexity.

Application Nature and Recommended Frameworks
Small Application with less than 100 test cases
Simple tests (with 1-5 test steps) & stable application
Record & Playback
Medium size tests (with 5-15 steps)
Modular Framework
Complex tests (beyond 50 steps) with database validations
Modular Framework - with advanced features
Tests with repeated data sets
Data-driven Framework (with combination of Modular based on above conditions)
Mid-size Application with 100 to 500 test cases
Simple tests (with 1-5 test steps)
Modular Framework
Medium size tests (with 5-15 steps)
Modular Framework
Complex tests (beyond 50 steps) with database validations
Keyword-driven Framework - with database capabilities
Tests have repeated data sets
Hybrid Framework with Modular/Keyword-driven & Data-driven combination
Large Application with 500 to 2000 test cases
Simple tests (with 1-5 test steps)
Keyword-driven Framework
Medium size tests (with 5-15 steps)
Keyword-driven Framework
Complex tests (beyond 50 steps) with database validations
Keyword-driven Framework - with database capabilities
Tests have repeated data sets
Hybrid Framework with Keyword-driven & Data-driven combination
Huge Application with above 2000 test cases
Simple / Medium / Complex tests
Advanced Keyword-driven Framework
Tests have repeated data sets
Hybrid Framework with Advanced Keyword-driven & Data-driven combination



By
Automation Mentor


We provide hands-on training on automation tools and frameworks