Selenium WebDriver is a popular Open Source tool used for automation of web applications. It is a simple, concise programming interface that supports dynamic web pages where the elements of a page may change before the page reloads. It overcomes the limitations of Selenium RC and its unique value proposition is its ability to drive the web browser using the native support of browser for automation.
- Chrome Driver
Selenium WebDriver is available in several programming languages such as Java, C#, Ruby, Perl, Python and PHP. We can use the programming language of our choice when using Selenium.
In this blog I am focusing on the features provided by Selenium for automation of web applications.
Native support for web browsers
As indicated earlier Selenium WebDriver drives the browser using the native support of browser. Firefox browser has in-built support for it. However, for Internet Explorer and Chrome we need to use third-party drivers.
Declaration and usage are as below
WebDriver driver = new FirefoxDriver();
WebDriver driver = new ChromeDriver(); System.setProperty("webdriver.chrome.driver", "path to chrome browser driver");
WebDriver driver = new InternetExplorerDriver(); System.setProperty("webdriver.ie.driver", "path to IE browser driver");
Multiple locator strategies
Selenium WebDriver provides multiple locator strategies to find the element on a web page. Some of these are listed below
Of all the locator types, Id is the most preferred because of its performance, followed by CSS.
Extensive set of simple commands to drive the web
Selenium WebDriver has an extensive set of commands that can be used to perform interaction with the web page. These commands are simple and can be wrapped around a wrapper to make them reusable methods. Some common commands are as follows:
- findElement – Find the first web element of the page
- By – Class used with locator to find a web element
- sendKeys – To pass text content to an input field
- click – Perform a click action
- submit – To perform submit action on the page
- getText – Get the text content from an web element
- getAttribute – Get attribute values from the web element
- getCurrentUrl – Get the current page url
- switchTo() – To Switch to window, frame or an alert
- navigate() – To navigate to an URL, forward or backward
- get – To navigate to the web page
- isEnabled, isDisplayed, and isSelected – Get status of the web element
Select Class to handle dropdowns
WebDriver provides Select class to handle dropdowns in the web page. The web element can be single-select or multi-select and several methods are available to handle them appropriately. Commonly used commands are as below
selectByIndex selectByValue selectByVisibleText getFirstSelectedOption getOptions deselectAll
Implicit and Explicit Waits
Automated tests need a way to wait for some time on the web page to perform some action and then move to the next step. Selenium WebDriver provides both Implicit and Explicit Waits for this purpose.
- Implicit Wait waits for an element to appear on the page until the time provided by the Implicit Wait is exhausted. This wait is common across the project.
- Explicit Wait is used in situations where we need to wait for some condition to occur, prior to moving further. The simplest example of Explicit Wait is Thread.sleep method and there are other sophisticated waits such as WebDriverWait used along with ExpectedConditions where we can specify the duration in seconds.
Action Classes for advanced interaction
Selenium WebDriver provides Action class to perform advanced user interactions with the keyboard such as Key up, Key Down along with ALT, CONTROL OR SHIFT, mouse actions such as Double Click, Drag and Drop. There are several other actions available to make automation easier and effective.
Support for Ajax
Selenium WebDriver supports Ajax effectively and can be used to perform actions on the dynamically loaded web elements on a page. It provides various `waitFor’ methods to wait for an element to appear and disappear on the page. These waitFor methods help us to overcome the problem of placing a manual delay and thus reduces chances of script failure if the element has not appeared within the delay provided.
Support for distributed test execution
Selenium WebDriver is available in two forms
- Selenium Server
WebDriver provides the ability to run our tests on our local machine and Selenium server helps to run our tests on remote machines where we can distribute tests over the network on multiple machines.
We can take a screenshot of the application whenever required using Selenium WebDriver. Screenshots are usually captured on failure conditions to get the details about a failure scenario.
Taking a screenshot is very simple and it is described below:
File screenshotFileName = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshotFileName, new File("Location where screenshot needs to be saved"));
The main advantage of Selenium WebDriver is that a single script built can be used by various operating systems and browsers. Thus, to summarize, based on an understanding of Selenium WebDriver’s features, you will agree that web application automation can be done effectively and easily using Selenium WebDriver. To make automation consistent and reliable we need to build a framework utilizing all the above features along with some tools such as loggers, unit testing frameworks, listeners and reporting utilities.