Browser Driver
Browser Driver
This page describes only Browser Driver specific features and methods which may be different in other drivers.
Implementation which is common for all drivers is described in Drivers page. Please, read it before.
Before delving into browser automation, consider the taskyou are automating. Does the web application you are working with allow API access? APIs do not change as often as the graphical user interfaces. Automating using APIs means your automation scripts don't break as easily.
Sometimes there is no API provided, or the API is missing some functionality to complete the process. In these cases, you need to interact with the application's user interface; filling and submitting forms, pressing buttons, clicking on elements, scraping content, and many other interactions. So BrowserDriver is what you need.
BrowserDriver is used to automate web browser actions.
This driver is based on the Selenium WebDriver which drives a browser natively, as a user would. In addition to Selenium WebDriver, EasyRPA BrowserDriver brings additional useful methods.
Driver Initialization
You can initialize the driver in your automation process task by the following way:
@Driver(param = {@DriverParameter(key = DriverParams.Browser.SELENIUM_NODE_CAPABILITIES, initializerName = DriverParams.BrowserCapabilities.CHROME) }) private BrowserDriver browserDriver;
Please notice that we pass DriverParams.BrowserCapabilities.CHROME
name as a reference to a capability class initializer in the above example, so we expect that selenium grid will provide the chrome node for us. If we want to use the browser other than Chrome, we need to pass the corresponding capability initializer instead.
All the supported browsers are listed in the table below
Target Browser | Capability Initializer |
---|---|
Chrome | DriverParams.BrowserCapabilities.CHROME |
Edge | DriverParams.BrowserCapabilities.EDGE |
Firefox | DriverParams.BrowserCapabilities.FIREFOX |
Internet Explorer | DriverParams.BrowserCapabilities.IE |
Opera | DriverParams.BrowserCapabilities.OPERA |
Safari | DriverParams.BrowserCapabilities.SAFARI |
Which browser should you use in your automation?
You can choose the browser you prefer. Our suggestion is to use Google Chrome unless the application you are trying to automate works only with a specific browser. Different browsers might behave in different ways. A robot could work with Chrome, but not Safari or Internet Explorer.
Driver Params
Param name | Measure | Default Value | Description |
---|---|---|---|
DriverParams.Browser.SELENIUM_HUB_URL | URL | http://localhost:4444/wd/hub | Selenium Hub URL. Defines how browser driver should interact with selenium. Now could be 2 modes:
|
DriverParams.Browser.SELENIUM_NODE_CAPABILITIES | org.openqa.selenium.Capabilities | null | Used to set properties of browsers to perform browser automation of web applications. It stores the capabilities as key-value pairs and these capabilities are used to set browser properties like browser name, browser version, path of browser driver in the system, etc. to determine the behavior of browser at run time. E.g. you can read about Chrome browser capabilities by the following link. |
DriverParams.Browser.PAGE_LOAD_TIMEOUT_SECONDS | seconds | 1800 | Limits the time that the script allots for a web page to be displayed. If the page loads within the time then the script continues. If the page does not load within the timeout the script will be stopped by a TimeoutException. |
DriverParams.Browser.IMPLICITLY_WAIT_TIMEOUT_SECONDS | seconds | 0 | This timeout is used to specify the amount of time the driver should wait while searching for an element if it is not immediately present. |
DriverParams.All.EXPLICITLY_WAIT_TIMEOUT_SECONDS | seconds | 5 | Wait functions default timeout in seconds. |
DriverParams.All.EXPLICITLY_WAIT_POLLING_INTERVAL_MILLISECONDS | milliseconds | 500 | Wait functions default pooling interval in milliseconds. |
Bellow is an example of how to put custom Chrome options capabilities and change default driver params:
@Driver(param = { @DriverParameter(key = DriverParams.Browser.SELENIUM_NODE_CAPABILITIES, initializer = ChromeInitializer.class), @DriverParameter(key = DriverParams.Browser.PAGE_LOAD_TIMEOUT_SECONDS, direct = "20"), @DriverParameter(key = DriverParams.Browser.IMPLICITLY_WAIT_TIMEOUT_SECONDS, direct = "5") }) private BrowserDriver browserDriver; private static final String filePath = System.getProperty("user.home") + "\\Downloads"; public static final class ChromeInitializer implements Supplier<Capabilities> { @Override public Capabilities get() { ChromeOptions chromeOptions = new ChromeOptions(); HashMap<String, Object> chromePrefs = new HashMap<>(); chromePrefs.put("profile.default_content_settings.popups", 0); chromePrefs.put("download.default_directory", filePath); chromeOptions.setExperimentalOption("prefs", chromePrefs); return chromeOptions; } }
The following dependency need to be added into the project because we are using ChromeOptions:
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-chrome-driver</artifactId> <version>${selenium.version}</version> <scope>provided</scope> </dependency>
Opening new applications
The first thing you will want to do after launching a browser is to open your website. This can be achieved in a single line:
browserDriver.get("https://selenium.dev");
Browser navigation
getTittle
You can read the current application title (or browser title).
public String getTitle()
getCurrentUrl
You can read the current URL from the browser’s address bar using this method.
public String getCurrentUrl()
navigate
This method returns an abstraction (Navigation object) which contains the helpful methods to perform navigation in Browser.
public Navigation navigate()
driver.navigate().to("https://selenium.dev"); //longer way of driver.get("https://selenium.dev") driver.navigate().back(); driver.navigate().forward(); driver.navigate().refresh();
Window management
Get window handle
WebDriver does not make the distinction between windows and tabs. If your site opens a new tab or window, Selenium will let you work with it using a window handle. Each window has a unique identifier which remains persistent in a single session. You can get the window handle of the current window by using:
getWindowHandle
Each window has a unique identifier which remains persistent in a single session. You can get the window handle of the current window by using this method.
public String getWindowHandle()
getWindowHandles
Return a set of window handles which can be used to iterate over all open windows available for driver so you can use handle to switch to window by passing them to "switch to window" methods.
public Set<String> getWindowHandles()
Switching windows or tabs
Browser driver supports switching to window by name or window handle.
switchToWindow
Switch the focus of future commands for this driver to the window with the given name/handle.
public void switchToWindow(String windowSearch)
Also it supports methods which help you to wait until window appears and switch to it only after that. It returns "true" if it switched successfully:
public boolean waitAndSwitchToWindow(String windowSearch, int... timeouts) public boolean waitAndSwitchToWindow(String windowSearch, BrowserSearch by, int... timeouts) public boolean waitAndSwitchToWindow(String windowSearch, BrowserSearch by, boolean optional, int... timeouts)
Working with UI Elements
Inspector
UI Elements represents a DOM elements. Browser Driver API provides built-in methods to find the elements which are based on different properties like ID, Name, Class, XPath, CSS Selectors, link Text, etc. To construct a selector you can use built-in browser DOM inspector.
E.g. you can read about Chrome browser inspector by the following link.
Find elements
Please, check the table which locators are supported by BrowserDriver:
Selector type |
---|
BrowserSearch.id |
BrowserSearch.name |
BrowserSearch.xpath |
BrowserSearch.cssSelector |
BrowserSearch.className |
BrowserSearch.tagName |
BrowserSearch.linkText |
BrowserSearch.partialLinkText |
Bellow an example of how to find element by name:
BrowserElement webElement = browserDriver.findElement(BrowserSearch.name("q"));
You can read more information of how to work with selenium selectors: