How to: Perform Selenium WebDriver Testing

The following tutorial is for Selenium WebDriver testing- one of the key components of Selenium releases and on which the current automation industry totally relies on, specifically if we say the “Open Source Community”.

What is WebDriver, simply speaking? “It is an API that’s easy to explore and understand, which helps us to make our tests easier to read and maintain.” WebDriver is not linked to any kind of test framework or tool and this makes this API more useful as we can use the same as per our needs or knowledge of other integration open sources like JUNIT, TESTNG etc.

  • A well designed Object Oriented API that provides improved support for web-app testing problems.
  • Supports dynamic web pages where element of a Page may change without the Page itself being reloaded.
  • All the limitations of Selenium RC rectified in this Selenium WebDriver.

Selenium RC Limitations

  • Work using Java script injection leads to same origin policy issues. WebDriver overcomes this limitation by having a driver for each browser.
  • RC means Remote Control, while executing tests using Selenium RC,  it is obvious to have Selenium RC server between application under test & Test Automation Suite. This methodology of Client Server architecture of Selenium RC makes it slower than Selenium WebDriver.
  • Redundancy of commands
  • RC cannot support headless browser but WebDriver can.

WebDriver Architecture

  • WebDriver is implemented on Layered Design, the idea behind this implementation is more and more usage of WebDriver for automation and this could be possible by fitting best fit languages.
  • Implementation of WebDriver is that each browser has a language that is most natural to use when attempting to drive it. Drivers are built as per the best fit language and we can just see the wrapper around them.
  • We can say that for any browser driver, if any of the features works there in one binding language then it should be easy to add support to other binding languages also.
  • WebDriver is a compact Object Oriented API which can directly interact with the application undergoing the tests.
  • WebDriver utilizes the browser native compatibility to automation without using any peripheral entity.

Selenium WebDriver Testing: script-driver-test

Features of WebDriver

  • “Interface WebDriver”, which represents an idealized web browser used for testing. Three categories of methods in this class.
    • Control to browser
    • WebElements selection
    • Debugging
  • We discussed above language bindings with browsers which is a thin wrapper that exposed for us to write code as per our needs. This leads WebDriver to support “Multiple Languages” as well as “Multiple Support Browsers” which means that if any API supports multiple languages then this automatically leads to “Multiple Platforms”.
    • Multiple Languages Binding support”:

    Selenium WebDriver Testing: multi-lang

    • Multiple Browsers support

    Selenium WebDriver Testing: multi-browser

    • Multiple Platforms support

    Selenium WebDriver Testing: multi-platform

    • Speed difference between RC and WebDriver

WebDriver one to one communication with “Application under Test” makes it faster than RC as RC creates one more interface between communications which is RC Server makes it like Client Server Communication. Snapshot below:

“WebDriver”

Selenium WebDriver Testing: script-driver-test

“RC”: Dotted arrow communication below represents additional layer

 Selenium WebDriver Testing: rc-server

“WebDriver” existence in package “org.openqa.selenium”

  • “Org.openqa.selenium”: The global package of Selenium which we import in our program every time when it is needed to use WebDriver. For reference, please see “org.openqa.selenium” mind map below :-

Selenium WebDriver Testing: selenium-package

  • “WebDriver”: Is an interface which comes under this package of Selenium and a sub interface of “SearchContext”. “SearchContext” consists of “WebDriver” & “WebElement” as a sub interfaces.
  • “WebDriver”: Let’s see what all WebDrivers contain, for example: – What is “FirefoxDriver”:  a WebDriver which is used to interact with WebApps. So, this means that all the drivers should have all the implementations which are contracted in the WebDriver interfaces and all  drivers can be called as WebDriver. See Figure below:
  • Drivers Support”: Classes implementing in “Interface WebDriver support

Selenium WebDriver Testing: drivers-support

  • At high level if we can say then Driver means as per the selection of the Explorer driver will be selected and Driver will be used to interact with Web Page.
  • Web Page composed of Web Elements and these Drivers’s will communicate with the Web Page.
  • For communicating with a Web Page means communication with Elements present on the Web Page for example: “Textboxes”, Buttons”, “Links” etc. “WebElement” a sub interface of “SearchContext” comes into picture. Let’s see this Interface also (Methods name and description in the table below)
Methods Description
Clear() If this element is a text entry element, this will clear the value. à Example:  clearing text present in Text Boxes before texting something into it.
Click() Used when we need to click on any of the elements present on the Web Page. Example: Clicking on the “Submit” button on Login Page.
findElement(By by) For finding an element present on the Webpage before performing any of the operation on it. àExample: If the element is not visible on the Web page then how any of the operation can performed on it and in this case exception will generate.

 

Using same method, we can find all the elements also with current context and it returns the list of Elements present.

getAttribute(java.lang.String name) Get the value of the given attribute of the element.
getLocation() Where on the page is the top left-hand corner of the rendered element?
getSize() What is the width and height of the rendered element?
getTagName() Get the tag name of this element.
getText() Get the text displayed of this element.
isDisplayed() Is this element displayed or not? This method avoids the problem of having to parse an element’s “style” attribute.
isEnabled() Is the element currently enabled or not? This will generally return true for everything but disabled input elements.
isSelected() Determine whether or not this element is selected or not.
sendKeys(java.lang.CharSequence… keysToSend) Use this method to simulate typing into an element, which may set its value.
Submit() If this current element is a form, or an element within a form, then this will be submitted to the remote server.

 

  • The above table is providing all methods which we can use to interact with WebElements on Web Page but now for performing any of the operations on Web Element, we need to find the element first on Web Page as per the above table and method findElement(). In order to find the element on Web Page, We need an address/locator of the element.
    • WebDriver”:Below is detailed what all WebDrivers consist of.

Selenium WebDriver Testing: webdriver

    • At last, Interfaces short description below (will be really helpful to learn ahead)
Interface Names Description
WebDriver.ImeHandler An interface for managing inputs
WebDriver.Navigation An interface for managing Page navigation
WebDriver.Options An interface for managing browser menu operations
WebDriver.TargetLocator An interface for locating Frames & Windows
WebDriver.Timeouts An interface for managing Timeouts
WebDriver.Window An interface for managing Window visualization

 

Highlights for Selenium Webdriver Testing

  • WebDriver: One of the most core component by Selenium
  • WebDriver: With vast features where driver covers all the features and properties of explorers
  • WebDriver: Gives us the opportunity to write once and execute on multiple platforms
  • WebDriver: Provides speed to Selenium architecture where communication with Application under Test becomes faster
  • WebDriver: Despite of working on any language or environment you need not to learn anything new it is just grab the knowledge and start producing results
  • WebDriver: Gives opportunity to explore something from core and can contribute from scratch till end in Automation process
  • WebDriver: Introduce AndroidDriver and IphoneDriver to explore Mobile world

External Reference: Thanks to “Selenium.org”, for giving us the opportunity to explore about Selenium WebDriver testing thoroughly.


Happy testing and further research on Selenium WebDriver testing!