Built.io Blog

Getting Started with Selenium WebDriver

,

So, what are Selenium and Selenium WebDriver?

Selenium

Primarily, Selenium automates browsers; the level of automation is completely up to how you use the tool.

Selenium WebDriver

Selenium WebDriver is a successor of Selenium RC. Selenium WebDriver provides a more concise programming interface, better support for dynamic web pages where elements of a page may change without the page itself being reloaded. It’s designed to provide improved support for modern advanced web-app testing problems.

It helps to create robust browser-based automation and scales and distributes scripts across many environments. Selenium WebDriver does not require a special server to execute tests as it directly starts a browser instance and controls the tests. Selenium WebDriver provides a friendly API which is simple to grasp and understand, thus making the tests easier to maintain. It is independent of any test framework which increases its usage from small module testing to large system testing. The WebDrivers are independent and they do not need any additional processes or installers to be run before using them.

Languages support for WebDriver

Selenium WebDriver supports most programming languages, which gives Selenium an upper hand over other testing tools.

language.jpg

Lets walk through a few advantages of Selenium WebDriver through various programming languages:

  • Advantages of Java
    • Java is object-oriented, thus modular programs can be created and so can reusable code. It is platform-independent and faster than any other language.
  • Advantages of JUnit
    • JUnit is a testing framework for unit testing. One can easily create and efficiently manage a rich unit test suite for the entire software. Junit is supported by almost all IDEs.
  • Advantages of .NET
    • It has a consistent programming model.
    • .NET has direct support for security.
    • It allows for simplified development efforts.
    • Is allows easy application deployment and maintenance.
  • Advantages of PHP
    • PHP is open source and capable of designing any website and can handle a lot of traffic.
    • It has easily understandable syntax.
    • It is platform independent and runs on all operating systems.
    • It supports major web servers like Apache, Microsoft IIS, Netscape, personal web server, iPlanet server, and more. *It supports major databases including MySQL, dBase, IBM DB2, InterBase, FrontBase, ODBC, PostgreSQL, SQLite, and more.
    • It uses its own memory space and thus decreases the loading time and workload from the server. The processing speed is fast and web applications like Ecommerce, CRM, CMS and Forums are also developed faster is PHP is used.
  • Advantages of Python
    • Python offers strong support for integrations with other technologies, programmer productivity throughout the development life cycle, and it is particularly well suited for large or complex projects with changing requirements.
    • Python is the most rapidly growing open source programming language.
    • Python is available for most operating systems, including Windows, UNIX, Linux, and Mac OS.
  • Advantages of Perl
    • Perl runs on all platforms and is far more portable than other environments.
    • Perl provides features that are required for large projects: Modularization (i.e. packages)
    • Object-oriented techniques
    • Arbitrary data structures
    • Large number of built-in checks by the compiler and at runtime.
  • Advantages of Ruby on Rails
    • It is perfect for web technologies and the framework is 100% free and runs on Linux, which is also open-source.
    • It’s easy to handle and saves time.
    • It follows coding conventions that make it simple to go from one developer to the next, so it’s clean and easy, and everyone uses it.
    • Ruby on Rails allows apps to be expandable and multi-purpose. Thus web browser testing has become simpler with Selenium WebDriver. WebDriver can test almost any web functionality with ease.

Creating your first script in WebDriver

Download all the Selenium WebDriver JAR files from: http://docs.seleniumhq.org/download/, and assume you're using Eclipse LUNA. First start with Eclipse and create a new Java project:

    1. Create new package or you can keep default package
    2. Inside the package Create new JUNIT test class
    3. Give class name “test”.
    4. Right click on Project and select Properties à java built path à Add External jars Import all jar (Selenium, JUNIT etc.) files here and click OK.
Package automation Framework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test
{
    // class name and file name should be same.
    public static WebDriver driver = null;
    public static void main(String[] args) 
    {  
        driver = new FirefoxDriver(); // Create a new instance of the Firefox driver
        //Put a Implicit wait, this means that any search for elements on the page could take the time driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
        //navigate to Google
        driver.get("http://www.google.com");
        // Find the text input element by its name and enter any text example “RawEng"
        driver.findElement(By.xpath("//*[@id='gbqfq']")).sendKeys("RawEng"); 
        // Hit Enter button on the webpage.
        driver.findElement(By.xpath("//*[@id='gbqfq']")).sendKeys(Keys.ENTER); 
        driver.quit();
    }
}
  1. Browser Structure for WebDriver

browser.jpg

WebDriver allows you to execute tests on multiple browsers like Chrome, Firefox, Internet Explorer, Safari and Opera. WebDriver is faster than Selenium RC as it speaks directly to the browser uses the browser's own engine to control it. To learn more, visit the Selenium WebDriver website.

Subscribe to our blog