Webdriver is a tool used in web application development to automate testing across multiple types of browsers. Since browsers such as Chrome, Safari, Firefox and Edge all have a tendency to display web apps and websites differently, developers need a efficient way to test accurately at scale. Selenium is a popular webdriver which automates the browser and helps us in automating web application testing across different browsers. Selenium API has provided many classes and interfaces to work with different types of browsers and HTML elements.
Selenium WebDriver is an interface that defines a set of methods. However, implementation is provided by the browser specific classes. Some of the implementation classes are AndroidDriver
, ChromeDriver
, FirefoxDriver
, InternetExplorerDriver
, IPhoneDriver
, SafariDriver
etc. The WebDriver main functionality is to control the browser. Kinda like a remote control. It even helps us to select the HTML page elements and perform operations on them such as click, filling a form fields etc.
If we want to execute your test cases in a Firefox browser we have to use FirefoxDriver
class. Similarly, if we want to execute the test cases in the Chrome browser we have to use ChromeDriver
class.
SearchContext is the topmost interface in Selenium API which has two methods - findElement() and findElements(). Selenium WebDriver interface has many abstract methods like get(String url), quit(), close(), getWindowHandle(), getWindowHandles(), getTitle() etc. WebDriver has nested interfaces like Window
, Navigation
, Timeouts
etc. These nested interfaces are used to perform operations like back(), forward() etc.
Method | Description |
---|---|
get(String url) | This method will launch a new browser and opens the given URL in the browser instance. |
getWindowHandle() | It is used to handle single window i.e. main window. It return type is string. It will returns browser windlw handle from focused browser. |
getWindowHandles() | It is used to handle multiple windows. It return type is Set. It will returns all handles from all opened browsers by Selenium WebDriver. |
close() | This command is used to close the current browser window which is currently in focus. |
quit() | This method will closes all the browsers windows which are currently opened and terminates the WebDriver session. |
getTitle() | This method is used to retrieve the title of the webpage the user currently working on. |
The major implementation classes of WebDriver interface are ChromeDriver, EdgeDriver, FirefoxDriver, InternetExplorerDriver etc. Each driver class corresponds to a browser. We simply create the object of the driver classes and work with them.
Class | Description |
---|---|
ChromeDriver | It helps you to execute Selenium Scripts on Chrome browser. |
FirefoxDriver | It helps you to execute Selenium Scripts on Firefox browser. |
InternetExplorerDriver | It helps you to execute Selenium Scripts on InternetExplorer browser. |
Selenium WebElement represents an HTML element. We can get an instance of WebElement using findElement() method and then perform specific actions such as click, submit etc. Some of the commonly used WebElement methods are:
Command | Description | Syntax |
---|---|---|
findElement() | This method finds the first element within the current web page by using given locator. | WebElement element = driverObject.findElement(By.locator(“value”)); |
sendKeys() | This method enters a value in to an Edit Box or Text box. | driver.findElement(By.elementLocator(“value”)).sendkeys(“value”); |
clear() | It clears the Value from an Edit box or Text Box. | driverObject.findElement(By.locatorname(“value”)).clear(); |
click() | It clicks an Element (Button, Link, Checkbox) etc. | driverObject.findElement(By.ElementLocator(“LocatorValue”)).click(); |
Let’s look at a simple example of using Selenium WebDriver to invoke Firefox browser and print the title of a website.
package com.journaldev.selenium.firefox;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GeckoDriverExample {
public static void main(String[] args) {
//specify the location of GeckoDriver for Firefox browser automation
System.setProperty("webdriver.gecko.driver", "geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://journaldev.com");
String PageTitle = driver.getTitle();
System.out.println("Page Title is:" + PageTitle);
driver.close();
}
}
Output:
1551941763563 mozrunner::runner INFO Running command: "/Applications/Firefox.app/Contents/MacOS/firefox-bin" "-marionette" "-foreground" "-no-remote" "-profile" "/var/folders/1t/sx2jbcl534z88byy78_36ykr0000gn/T/rust_mozprofile.t6ZyMHsrf2bh"
1551941764296 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid host permission: resource://pdf.js/
1551941764297 addons.webextension.screenshots@mozilla.org WARN Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid host permission: about:reader*
Can't find symbol 'GetGraphicsResetStatus'.
1551941765794 Marionette INFO Listening on port 61417
1551941765818 Marionette WARN TLS certificate errors will be ignored for this session
Mar 07, 2019 12:26:05 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Page Title is:JournalDev - Java, Java EE, Android, Python, Web Development Tutorials
1551941814652 Marionette INFO Stopped listening on port 61417
You can checkout more Selenium examples from our GitHub Repository.
Reference: WebDriver GitHub Code
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.