analytics

Sunday, February 16, 2014

Web Browser Automation using Selenium

Here I will discuss what is Selenium, how Selenium works and will also provide one example to demonstrate how easily we can create automated test cases using Selenium.

 What is Selenium?


Selenium automates browsers. That's it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well. Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser. It is also the core technology in countless other browser automation tools, APIs and frameworks.


Selenium WebDriver


The biggest change in Selenium recently has been the inclusion of the WebDriver API. Driving a browser natively as a user would either locally or on a remote machine using the Selenium Server it marks a leap forward in terms of browser automation.


Selenium WebDriver fits in the same role as RC did, and has incorporated the original 1.x bindings. It refers to both the language bindings and the implementations of the individual browser controlling code. This is commonly referred to as just "WebDriver" or sometimes as Selenium 2.


Selenium 1.0 + WebDriver = Selenium 2.0


• WebDriver is designed in a simpler and more concise programming interface along with addressing some limitations in the Selenium-RC API.


• WebDriver is a compact Object Oriented API when compared to Selenium1.0


• It drives the browser much more effectively and over comes the limitations of Selenium 1.x which affected our functional test coverage, like the file upload or download, pop-ups and dialogs barrier


• WebDriver overcomes the limitation of Selenium Rc's Single Host origin policy


WebDriver is the name of the key interface against which tests should be written in Java, the implementing classes one should use are listed as below:


AndroidDriver, ChromeDriver, EventFiringWebDriver, FirefoxDriver, HtmlUnitDriver,InternetExplorerDriver, IPhoneDriver, PhantomJSDriver, RemoteWebDriver, SafariDriver 


Let’s do some programming now…… This is small but complete example how Selenium works


First you need to download the Selenium and WebDriver libraries from www. seleniumhq.org. Here in this example I have used FirefoxDriver but you can use other Drivers for other browsers.


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

 public class GoogleAutomation {


 public static void googleSearch(){ System.setProperty("webdriver.firefox.bin","C:\\Users\\Roy\\AppData\\Local\\Mozilla Firefox\\Firefox.exe");


System.out.println("Opening WebDriver");

WebDriver driver = new FirefoxDriver();

System.out.println("Opening Google.com");

// And now use this to visit Google
driver.get("http://www.google.com");

// Find the text input element by its name

WebElement element = driver.findElement(By.name("q"));

// Enter something to search for

element.sendKeys("Honda Car");
System.out.println("Doing Search for Honda Car");

// Now submit the form. WebDriver will find the form for us from the element

element.submit();
// Now you can see the search results in Google
 }

 public static void main(String[] args) {

 googleSearch();
 }
 }


 I hope now you will be able to write some cool programs to automate Web Browsers using Selenium.

No comments:

Post a Comment