Web testing is an important requirement considering internet acceptance worldwide for both web and mobile. Considering the wide use of Agile methodology; there is a need for frequent release and hence regression testing.
Manual testing is not only slow and exhaustive but there are high chances of human errors. Testing the same test cases with different sets of data is time consuming. There are so many browsers (Internet Explorer, Firefox, Chrome, Safari, Opera etc.) on which the end user may access the application and hence the test matrix can become very complex and it multiplies the testing effort. This can be overcome by automation.
While there are many tools/API available for web based test automation; Selenium an open source is very widely used to perform automation.
About Selenium WebDriver:-
Selenium 2.0 or Selenium WebDriver is the merging of two projects
Selenium 1.0 and WebDriver API. It supports dynamic web pages where elements of a page may change without the page itself being reloaded. It calls the browser directly using the native support of browser. It works with all browsers and is compatible with major operating systems like Windows, Mac and Linux platforms. Automation Script can be written in any of the Programming Languages like Java, C#, Ruby, Python, Perl and PHP.
Why Selenium WebDriver?
ü Working with frames is a painful process in Web automation. But with Selenium WebDriver, switching between frames is much easier
ü By using WebDriver you will have more control over the code and you can have the ability to reuse the code which saves vast amount of time and also removes complexity
ü By using click handler you can perform click on any element like button, link, radio button, etc.
ü Consistency over the browsers is improved
ü Multiple windows, pop-ups and Alerts can be handled
ü Support for iPhone and Android testing
ü You can easily simulate browsing history by clicking back and front buttons
Configuring Selenium Java Client Driverwith Eclipse:-
Step1:Download Java (JDK and JRE) from the following URL
Step2: Download Eclipse
Download “Eclipse IDE for Java Developers” from the following URL and unzip the folder.
Step3:Download Selenium Jar files
In the following URL, click on “Download” link of Java language under “Selenium Client & WebDriver Language Bindings”.
Step4:Configure Selenium with Eclipse
Follow the steps specified in the following URL to configure:
Now WebDriver acts just as a normal library and is entirely self-contained. After the above steps, youwill be ready to write the first script.
Writing your first script using Selenium WebDriver in Java Language:-
WebDriver driver=new FirefoxDriver();
element.sendKeys(“sample code for testing”);
- Interfaces used in the above code:
- WebElement: – This represents an HTML element which performs all the operations for interacting with a page.
- WebDriver: – This is the main interface to be used for testing, which represents an idealized browser. The methods of this class Controls the browser and selects the Web Elements.
- The above code creates a new Instance of the FirefoxDriver and opens the Firefox browser with Google website.
Basic hurdles to take care while writing for any Website:-
For any Website, Modal Dialogs and Object Identification are the typical parts. You can handle this using Selenium WebDriver in an effective way which is explained below:
1.Handling Browser Modal Dialogs
- Browser modal dialogs can be troublesome for automation testers. Since it is a modal, the browser does not proceed with automation unless these dialogs are closed. Selenium WebDriver has built in support for modal dialogs.
- Alert alert = driver.switchTo().alert();
The above line returns the currently open alert object. By using this object, you can accept, dismiss, read its contents or even type into a prompt.
Object identification is a very crucial part of any automation effort. There are different ways which you can identify or locate the element uniquely:
In order to identify through the above methods, you need to install Firebug and Firepath Add-ons for Firefox.
After installing, open any website (Ex:- www.google.com) and press F12.
Click on the icon which is marked with (1) in the above screenshot and click on the element which you want to inspect.In the above example, Google search text box which is marked with (2) is the element which needs to be inspected.In the above screenshot, you can use the,
- FirePath tab to identify an element by XPathx
- CSS tab to identify an element by CSS
- DOM tab to identify an element by DOM
- HTML or FirePath tab to identify an element by Name, Id, and Link Text
Ex:- In the above screenshot, you can observe the XPath as “//*[@id=’gs_tti0′]” for the Google search text box.
How to know that the selected path is unique:
- Install Selenium IDE Plug-in for Firefox.
- Press Ctrl+Alt+S to open Selenium IDE.
- Enter the path in the Target and click on Find button.
- If the element is identified in the Google page (in the following screenshot marked with yellow color), then you can say that the path is unique.
- If the element is not identified, the following error is shown in the Log tab:
[error] locator not found: //*[@id=’gs_tti0′]
Now you need to go for different Locating option to identify the element uniquely.
Example for Object Identification:
<form id= “loginForm” >
<input name= “username” id=”uid” type= “text” />
<input name= “password” id=”pwd” type= “password” />
<input name= “continue” type= “submit” value= “Login” />
<input name= “continue” type= “button” value= “Clear” />
<a href= “register.html” >NewUser</a>