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

No comments:

Post a Comment