Selenium WebDriver Interface
Introduction to WebDriver Interface
The WebDriver interface is the main interface for testing web applications in Selenium. It provides methods for interacting with web pages, such as opening a URL, navigating back and forth, and switching between browser windows.Syntax to use WebDriver interface
To use the WebDriver interface in our Selenium tests, we need to create an instance of the WebDriver interface and use its methods to interact with the web application. Here is the syntax to create an instance of the WebDriver interface:-
Create an instance of the
WebDriver
interface as per the browser you want to automate testing. For example, to automate testing in Google Chrome, create an instance of theChromeDriver
class as below:WebDriver driver = new ChromeDriver();
-
Then use the methods of the
WebDriver
interface to interact with the web application. For example, to open a URL in the browser, use the get() method as below:driver.get("https://www.nicetesters.com");
WebDriver Implementation Classes
The WebDriver interface has several implementation classes, each corresponding to a different browser. Some of the most commonly used implementation classes are:ChromeDriver
: For Google ChromeFirefoxDriver
: For Mozilla FirefoxInternetExplorerDriver
: For Internet ExplorerEdgeDriver
: For Microsoft EdgeSafariDriver
: For Safari
public class WebDriverExample {
public static void main(String[] args) {
// Set the path to the WebDriver executables
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
System.setProperty("webdriver.edge.driver", "path/to/edgedriver");
// Create instances of WebDriver for different browsers
WebDriver chromeDriver = new ChromeDriver();
WebDriver firefoxDriver = new FirefoxDriver();
WebDriver edgeDriver = new EdgeDriver();
}
}
Note: The
System.setProperty()
method is used to set the path to the browser
driver.
We need to download the browser
driver and set the path before creating an instance of the WebDriver.
WebDriver Methods Details
We will look into some of the WebDriver methods with examples:-
get(String url): This method is used to open a new browser window and navigate to the specified URL. We need to pass the URL of
the website as a parameter to this method. For example:
driver.get("https://www.nicetesters.com");
-
getTitle(): This method is used to return the title of the current page. For example:
String title = driver.getTitle();
-
getCurrentUrl(): This method is used to return the URL of the current page. For example:
String url = driver.getCurrentUrl();
-
findElement(By by): This method is used to find the first WebElement
using the given method. It takes a By
Class
object as a parameter to find the element on page and
return the
WebElement
object. For example:WebElement element = driver.findElement(By.id("elementId"));
-
findElements(By by): This method is used to find all WebElements for
the provided
locator. It takes a By
Class object as a
parameter to find the elements on page and return a
List<WebElement>
object. . For example:List<WebElement> elements = driver.findElements(By.tagName("a"));
-
close(): This method is used to close the current browser window. For example:
driver.close();
-
quit(): This method is used to close all browser windows and end the WebDriver session. For example:
driver.quit();
-
navigate(): This method is used to return an instance of the
Navigation
interface, which allows you to navigate back and forward in the browser's history. For example:driver.navigate().back();
-
manage(): This method is used to return an instance of the
Options
interface, which allows you to manage browser options such as cookies, timeouts, and window size. For example:driver.manage().window().maximize();
WebDriver methods code example
Here is an example of using the WebDriver interface to open a website in different browsers:public class WebDriverExample {
public static void main(String[] args) {
// Set the path to the WebDriver executables
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Create instances of WebDriver for chrome browsers
WebDriver chromeDriver = new ChromeDriver();
// Open a website in Chrome
chromeDriver.get("https://www.nicetesters.com");
System.out.println("Chrome - Title: " + chromeDriver.getTitle());
chromeDriver.quit();
}
}
In this example, we created instances of the
ChromeDriver
,
FirefoxDriver
, and
EdgeDriver
classes to open a website in Google Chrome, Mozilla Firefox,
and
Microsoft Edge
browsers, respectively. We then printed the title of the website
and close the browser window.
Method | Description |
---|---|
get(String url) |
Opens a new browser window and navigates to the specified URL. |
getTitle() |
Returns the title of the current page. |
getCurrentUrl() |
Returns the URL of the current page. |
findElement(By by) |
Finds the first WebElement using the given method. |
findElements(By by) |
Finds all WebElements using the given method. |
close() |
Closes the current browser window. |
quit() |
Closes all browser windows and ends the WebDriver session. |
navigate() |
Returns an instance of the
Navigation interface, which allows you to
navigate back and forward in the
browser's
history.
|
manage() |
Returns an instance of the
Options
interface, which allows you to
manage browser options such as
cookies,
timeouts, and window size.
|
Next we will learn By Class and its methods.