Selenium WebDriver
Selenium WebDriver
© Pepgo Pty Ltd, Level 18, 333 Ann Street, Brisbane, QLD 4000, Australia
Contents
Assert and Verify ............................................................................................................................................... 3
Locating Elements.............................................................................................................................................. 3
Relative Locators ............................................................................................................................................... 5
Determine if an element exists without throwing an exception ...................................................................... 6
Get information from elements ........................................................................................................................ 6
Get the content of a whole page ....................................................................................................................... 7
Enter data in Editboxes...................................................................................................................................... 7
Clear data in Editboxes ...................................................................................................................................... 7
The “submit()” method ..................................................................................................................................... 7
Set information on Dropdown and Lists............................................................................................................ 8
Clicking on Buttons and Links (and other web elements) ................................................................................. 9
Set information on Radio Buttons and Radio Groups ....................................................................................... 9
Set information on Checkboxes....................................................................................................................... 10
Working with Tables ........................................................................................................................................ 10
Check the status of an element ....................................................................................................................... 11
Mouse and Keyboard events ........................................................................................................................... 11
Synchronisation ............................................................................................................................................... 12
Set the Implicit Wait timeout to 10 Seconds............................................................................................... 13
Explicit wait.................................................................................................................................................. 13
Set the default browser navigation timeout ................................................................................................... 14
Controlling browsers using “navigate” ............................................................................................................ 14
Cookies ............................................................................................................................................................ 14
Open new windows and tabs .......................................................................................................................... 15
Handling pop-up windows............................................................................................................................... 15
Handling JavaScript alerts (pop ups) ............................................................................................................... 16
Handling OK/Cancel Message Boxes (=Confirm Box Alert) ............................................................................. 16
Handling an Input Box (Prompt Box Alert) ...................................................................................................... 17
Frames (including IFRAME).............................................................................................................................. 17
Event handling ................................................................................................................................................. 17
Taking Screenshots with WebDriver ............................................................................................................... 18
Taking Screenshots with RemoteWebDriver................................................................................................... 18
The Page Object pattern.................................................................................................................................. 19
Dealing with I/O ............................................................................................................................................... 21
Using Java 8 Features with Selenium .............................................................................................................. 21
P a g e 2 | 22
Assert and Verify
There are two mechanisms for validating elements available in the application under test. The first is
“assert”; this allows the test to check if the element is on the page. If it is not available, then the test will
stop at the step that failed. The second is “verify”; this also allows the test to check if the element is on the
page, but if it isn't, then the test will carry on executing.
verifyElementPresent
assertElementPresent
verifyElementNotPresent
assertElementNotPresent
verifyText
assertText
verifyAttribute
assertAttribute
verifyChecked
assertChecked
verifyAlert
assertAlert
verifyTitle
assertTitle
Locating Elements
Locating elements is done by using the “findElement()” and “findElements()” methods.
The “findElement()” method returns the first WebElement object based on a specified search criteria, or
throws an exception, if it does not find any element matching the search criteria.
The “findElements()” method returns a list of WebElements matching the search criteria. If no elements
are found, it returns an empty list.
Strategy Description
By class name Locates an element using the (CSS style) Class attribute
By tag name Locates an element using the HTML tag (for example “Select”, “Input”, “List”,
“Button” etc.). The “Input” tag has multiple different types (such as “RADIO”,
“CHECKBOX”, “TEXTBOX”, “PASSWORD”) that need to be further filtered by
using the “type” attribute.
P a g e 3 | 22
By link text Locates link using its text
The By CSS locator is similar to the By XPath locator in its usage, but the
difference is that it is slightly faster.
The following example uses two attributes for the “<input>” element:
WebElement previousButton =
driver.findElement(By.cssSelector("input[type='submit'][value='Login']"));
There is also a partial match syntax (a kind of “regular expression”), featuring
“^=” (starting with), “$=” (ending with) and “*=” (containing).
More examples:
To identify an element using the “div” element with “id #flrs”, use
the “#flrs” syntax.
To identify the child “anchor” element, use the “#flrs > a” syntax,
which returns the “link” element.
To identify the “anchor” element with its attribute, use the “#flrs >
a[a[href="/intl/en/about.html"]]” syntax.
By XPath Locates element using XPath query. However, this is the least preferable locator
strategy due its slow performance, as XPath provides the ability to search
elements bi-directionally (downwards in the hierarchy, as well as upwards).
In web browsers, the built-in developer tools can be used to identify the XPath
of elements.
XPath examples:
P a g e 4 | 22
Example for finding a child element:
Example for extracting all the links and print their targets by using the “findElements()” method:
@Test
public void testFindElements()
{
//Get all the links displayed on Page
List<WebElement> links = driver.findElements(By.tagName("a"));
//Verify that there are four links displayed on the page
assertEquals(4, links.size());
//Iterate though the list of links and print target for each link
for(WebElement link : links)
System.out.println(link.getAttribute("href"));
}
Relative Locators
Relative Locators find web elements based on their GUI location.
toLeftOf() Web element appears to the left of the specified web element
toRightOf() Web element appears to the right of the specified web element
near() Web element is at most 50 pixels away from the specified web element. There is also
an overloaded method to specify the distance.
WebElement book;
book =
driver.Findelement(RelativeLocators.withTagName("li").toLeftOf(By.id("pid1")).be
low(By.id("pid2")));
P a g e 5 | 22
Example for “toRightOf()” and “above()” locators:
WebElement book;
book =
driver.Findelement(RelativeLocators.withTagName("li").toRightOf(By.id("pid1")).a
bove(By.id("pid2")));
If they are multiple results for the query, then the first matching item will be returned. It is also important to
consider that the results might vary depending on the viewport. The view on a mobile phone might look very
different than the view on a desktop browser.
To get around this, you can use “findElements()”, and check that the size of the list returned is 0. For
example:
You can also perform a partial match using Java String API methods such as
“contains()”, “startsWith()” , and “endsWith()”. You can use these methods in
the following way:
assertTrue(messageText.contains("color"));
assertTrue(messageText.startsWith("Click on"));
assertTrue(messageText.endsWith("will change"));
assertEquals("justify",message.getAttribute("align"));
getLocation() The “getLocation()” method can be used with all WebElements. It is used to get the
relative position of an element where it is rendered on the web page. This position is
P a g e 6 | 22
calculated relative to the top-left corner of the web page of which the (x, y) coordinates
are assumed as (0, 0). This method can be of use if a test script tries to validate the
layout of a web page.
getSize() The “getSize()” method can also be used with all visible HTML elements. It returns
the width and height of the rendered WebElement.
getTagName() The “getTagName()” method can be used on all WebElements. It returns the tag
name of the WebElement. For example, in the following HTML code, “button” is the
tag name of the HTML element:
<button id="gbqfba" class="gbqfba" name="btnK" aria-label="Google Search">
driver.findElement(By.xpath("//input[@name='q']")).sendKeys("hello");
Special keys, such as Backspace, Enter, Tab, or Shift, require using a special “enum” class of WebDriver,
named “Keys”. This example uses the Shift key to type the text in uppercase:
driver.findElement(By.xpath("//input[@name='q']")).sendKeys(Keys.chord(Keys.SHIF
T,"phones"));
driver.findElement(By.xpath("//input[@name='q']")) .clear();
P a g e 7 | 22
driver.findElement(By.xpath("//input[@name='q']")) .submit();
@Test
For checking whether a specific option is available for selection, you can simply perform a check on the
“act_options” array list in the following way:
P a g e 8 | 22
assertTrue(act_options.contains("BMW"));
@Test
You can also get all the radio buttons from a Radio Group in a list using the “findElements()” method
along with the Radio Group identifier.
P a g e 9 | 22
Set information on Checkboxes
WebDriver supports Checkbox control using the “WebElement” class. You can select or deselect a
checkbox using the “click()” method of the WebElement class and check whether a checkbox is selected
or deselected using the “isSelected()” method.
The first example finds all row elements from a given table. Please note that “t” in this example stands for the
table object.
The second example illustrates how to find the column number for the given column name in a table.
The third example illustrates how to check, if the value in a given cell matches the desired value. The
function takes 3 parameters. The first parameter is the row element, the second parameter is the column
number, and third parameter is the expected value.
P a g e 10 | 22
}
return false;
}
Method Purpose
isEnabled() This method checks if an element is enabled. Returns “true” if enabled, else “false” for
disabled.
isSelected() This method checks if element is selected (radio button, checkbox, and so on). It returns
“true” if selected, or “false” for deselected. It can be executed only on a radio button,
options in select, and checkbox WebElements. When executed on other elements, it will
return “false”.
There are three different actions that are available in the “Actions” class that are specific to the keyboard.
They are the “keyUp()”, “keyDown()”, and “sendKeys()” actions, each having two overloaded methods.
One method is to execute the action directly on the WebElement, and the other is to just execute the method
irrespective of its context.
Example: Let's create a test to select multiple rows from different positions in a table using the Ctrl key. You
can select multiple rows by selecting the first row, then holding the Ctrl key, and then selecting another row
and releasing the Ctrl key. This will select the desired rows from the table.
@Test
public void testRowSelectionUsingControlKey() {
List<WebElement> tableRows =
driver.findElements(By.xpath("//table[@class='iceDatTbl']/tbody/tr"));
//Select second and fourth row from table using Control Key.
//Row Index start at 0
Actions builder = new Actions(driver);
builder.click(tableRows.get(1))
.keyDown(Keys.CONTROL)
.click(tableRows.get(3))
.keyUp(Keys.CONTROL)
.build().perform();
//Verify Selected Row table shows two rows selected
List<WebElement> rows =
driver.findElements(By.xpath("//div[@class='icePnlGrp
P a g e 11 | 22
exampleBox']/table[@class='iceDatTbl']/tbody/tr"));
assertEquals(2,rows.size());
}
You can create a composite action that is ready to be performed by calling the “build()” method. Finally
the test will perform this composite action by calling the “perform()” method of the “Actions” class.
The “click()” method is used to simulate the left-click of your mouse at its current point of location. It is
also possible to specify a WebElement to click on as input parameter: “click(WebElementName)”
Double clicks can be implemented in the same way by clicking on the spot with the “doubleClick()”
method, or on a specific WebElement: “doubleClick(WebElementName)”
The “contextClick()” method is also known as right-click. The context is nothing but a menu; a list of
items is associated to a WebElement based on the current state of the web page. It is also possible to
right-click on a specific WebElement: “contextClick(WebElementName)”
The “moveByOffset()” method is used to move the mouse from its current position to another point on the
web page.
Drag and Drop operations can be implemented in the same way with the
“dragAndDrop(SourceWebElementName, TargetWebElementName)” (to move to a target element) and
the “dragAndDropBy(WebElementName, intXoffset, intYoffset)” (move by an offset) methods.
The “clickAndHold()” method is another method of the “Actions” class that left-clicks on an element
and holds it without releasing the left button of the mouse. This method is useful when executing operations
such as drag-and-drop. It is also possible to specify a WebElement to click on as input parameter:
“clickAndHold(WebElementName)”
The “release()” method is the one that can release the left mouse button. By using
“release(WebElementName)”, you can actually release the currently held WebElement in the middle of
another WebElement, so that you don't have to calculate the offset of the target WebElement from the held
WebElement.
The “moveToElement()” method is another method that helps to move the mouse cursor to a WebElement
on the web page.
WebDriver also provides a “JavascriptExecutor” interface that can be used to execute arbitrary
JavaScript code within the context of the browser.
The “JavascriptExecutor” can inject and then use entire JavaScript libraries (such as JQuery). The
“JavascriptExecutor” also has a method called “executeAsyncScript()”, that allows execution of
JavaScript that does not respond instantly.
Synchronisation
“WaitFor” Commands are very useful for AJAX. The “waitFor” commands will timeout after 30 seconds by
default.
Command Condition
waitForPageToLoad Delays execution until the page is fully loaded in the browser
P a g e 12 | 22
waitForElementPresent Delays execution until the specified element is present on the page
waitForElementNotPresent Delays execution until the specified element is removed from the page
waitForTextPresent Delays execution until the specified text is present on the page
waitForFrameToLoad Delays execution until the contents of the frame are fully loaded in the
browser
A number of these commands are run implicitly when other commands are being executed. For example,
with respect to the “clickAndWait” command, when you click on an element, the “waitForPageToLoad”
command is also executed.
Implicit timeouts are common to all WebElements and have a global timeout period associated with them.
However, an implicit wait condition may slow down your tests when an application responds normally, as it
will wait for each element to appear in the DOM and increase the overall execution time. It is recommended
to avoid or minimize the use of an implicit wait condition. Try to handle synchronization issues with an explicit
wait condition instead, which provides more control as compared to an implicit wait condition.
Explicit wait
WebDriver provides the “WebDriverWait” and “ExpectedCondition” classes for implementing an
explicit wait condition. The following example sets the explicit wait time for a new WebElement called
“searchBox” that is identified by its name (“q”) to 20 seconds. The implicit timeout doesn't get applied for this
WebElement. This effectively allows you to override the implicit wait time exclusively for a WebElement, if
you think that it might require more time.
The “ExpectedCondition” class provides a set of predefined conditions to wait before proceeding further.
The following table shows some common conditions that you frequently come across when automating web
browsers supported by the ”ExpectedCondition” class:
P a g e 13 | 22
Predefined condition WebDriver (Java)
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(50));
The method that is used for this purpose is “navigate()”, for example:
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
Cookies
WebDriver provides methods for handling cookies, for example “driver.manage().getCookies()” to
fetch all cookies that are loaded for a web page, or
“driver.manage().addCookie(CookieObjectName)” for adding cookie information to the driver.
P a g e 14 | 22
Open new windows and tabs
Open a new window:
driver.get("https://www.google.com/");
driver.switchTo().newWindow(WindowType.WINDOW);
driver.navigate().to("https://www.microsoft.com/");
driver.get("https://www.google.com/");
driver.switchTo().newWindow(WindowType.TAB);
driver.navigate().to("https://www.microsoft.com/");
Every time you open a web page using WebDriver in a browser window, WebDriver assigns a window
handle to that window. WebDriver uses this window handle to identify the window.
WebDriver allows you to identify a pop-up window by its window handle or its “name” attribute and switching
between the pop-up window and the browser window is done using the
“Webdriver.switchTo().window()”method.
In the following example, a user can open a pop-up window by clicking on the Help button. In this example,
the developer has provided “HelpWindow” as its name:
@Test
//Clicking Help Button will open Help Page in a new Popup Browser Window
WebElement helpButton = driver.findElement(By.id("helpbutton"));
helpButton.click();
try {
P a g e 15 | 22
} catch (NoSuchWindowException e) {
e.printStackTrace();
If there is no dialog currently present, and you invoke this API, it throws a “NoAlertPresentException”
exception.
A test might need to verify what message is displayed in an alert box. You can get the text from an alert box
by calling the “getText()” method of the “Alert” class as follows:
An alert box is closed by clicking on the “OK” button. This can be done by calling the “accept()” method as
follows:
alert.accept();
Alternatively, an alert box can also be accessed without creating an instance of the “Alert” class by directly
calling the desired methods as follows:
driver.switchTo().alert().accept();
alert.dismiss();
P a g e 16 | 22
The handling is essentially the same as with an Alert Box. The “accept()” method clicks the “OK” button,
“dismiss()” clicks on the “Cancel” button.
The handling is essentially the same as with an Alert Box. Input is sent via the “sendKeys()” method.
Example of switching to a frame by the index of the frame. The index is zero-based, meaning that the first
frame has the number 0:
driver.switchTo().frame(1);
Switch back to the default page. This is very important. If you don't do this and try to switch to another frame
(for example the frame with the index 2) while you are still in the first frame, your WebDriver will complain,
saying that it can't find a frame with index 2. This is because the WebDriver searches for the second frame in
the context of the first frame. So, you have to first come back to the top-level container, before you switch to
another frame:
driver.switchTo().defaultContent();
driver.switchTo().frame(frameElement);
Event handling
WebDriver provides a very good framework for tracking the various events that happen while running test
scripts using WebDriver. Many navigation events that get fired before and after an event occurs (such as
before and after navigating to a URL, before and after browser back-navigation, and so on) can be tracked
and captured. To fire an event, WebDriver uses a class named “EventFiringWebDriver”, and to catch
that event, it provides an interface named “WebDriverEventListener”. Test script developers should
provide their own implementations for the overridden methods from the interface.
The “EventFiringWebDriver” class is a wrapper around the WebDriver that gives the driver the
capability to fire events. That means that it effectively replaces the original WebDriver inside the script. For
example, instead of “webDriver.get("https://www.google.com");”, something like
“eventFiringDriver.get("https://www.google.com");” will be used.
P a g e 17 | 22
beforeAlertAccept(WebDriver webDriver)
afterAlertAccept(WebDriver webDriver)
afterAlertDismiss(WebDriver webDriver)
beforeAlertDismiss(WebDriver webDriver)
beforeNavigateTo(String url, WebDriver webDriver)
afterNavigateTo(String s, WebDriver webDriver)
beforeNavigateBack(WebDriver webDriver)
afterNavigateBack(WebDriver webDriver)
beforeNavigateForward(WebDriver webDriver)
afterNavigateForward(WebDriver webDriver)
beforeNavigateRefresh(WebDriver webDriver)
afterNavigateRefresh(WebDriver webDriver)
beforeFindBy(By by, WebElement webElement, WebDriver webDriver)
afterFindBy(By by, WebElement webElement, WebDriver webDriver)
beforeClickOn(WebElement webElement, WebDriver webDriver)
afterClickOn(WebElement webElement, WebDriver webDriver)
beforeChangeValueOf(WebElement webElement, WebDriver webDriver)
afterChangeValueOf(WebElement webElement, WebDriver webDriver)
beforeScript(String s, WebDriver webDriver)
afterScript(String s, WebDriver webDriver)
onException(Throwable throwable, WebDriver webDriver)
Example:
@Test
public void testTakesScreenshot()
{
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\main_page.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
The “TakesScreenshot” interface captures the screenshot of the entire page, current window, visible
portion of the page, or of the complete desktop window in their respective order, as supported by the
browser.
P a g e 18 | 22
One way is to create an own “WebDriver” class that extends the “RemoteWebDriver” class and
implements the “TakesScreenshot” interface by providing the implementation for the
“getScreenshotAs()” method. Another ways is to use the "Augmenter" class.
It helps in building a layer of abstraction separating automation code, which knows about locating
application elements and the one which interacts with these elements for actual testing.
A Factory method is just a fancy name for a method that instantiates objects. Like a factory, the job of the
factory method is to create -- or manufacture – objects.
Page Object Classes You split the test logic out into separate PageObject classes. This allows you to
create a Java class for each page and/or business process that you can use in an
application.
The second way to use “@FindBy” works with all the different locator mechanisms
(CLASS_NAME, CSS, ID, ID_OR_NAME, LINK_TEXT, NAME,
PARTIAL_LINK_TEXT, TAG_NAME, XPATH):
@FindBy(how=How.ID, using="user_login")
WebElement email;
A constructor method is used to call the web page of the page objects, so that they
can be initialised:
public NameOfThePageObjectClass(WebDriver driver){
this.driver = driver;
driver.get("http://url.html");
}
Page Factory Once the PageObject class declares elements using the “@FindBy” annotation,
you can instantiate that PageObject class and its elements using the
“PageFactory” class. The elements of the PageObject class get initialised when
P a g e 19 | 22
you call “PageFactory.initElements();” in your tests.
The following example creates an object called “loginPage” from the PageObject
class “AdminLoginPage”. It then runs a method called “login”:
loginPage.login();
Examples:
@Override
protected void load() {
driver.get("http://url.html");
PageFactory.initElements(driver, this);
}
@Override
protected void isLoaded() throws Error {
Assert.assertTrue(anyPageObjectWebElement != null);
}
The “get()” method ensures that the component is loaded by invoking the
“isLoaded()” method (which calls the “load()” method if the assertion fails), for
example:
P a g e 20 | 22
“load()”method. Please also note that “driver.get("http://url.html");” has
been moved from the constructor of the PageObject class to the “load()” method.
FileHandler Copy files, create directories, delete files or directories, check if a file is a
zipped file, make a file executable, make a file writeable, read a file as a string,
check if a file is executable etc.
TemporaryFilesystem As the name suggests, the files that are created under temporary filesystem
are temporary; that is, the files are deleted as soon as the test script is
executed.
Example for using a filter on “isDisplayed()”, showing first the number of all links, and then just the
number of displayed links:
List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println("Total Links : " + links.size());
long count = links.stream().filter(item -> item.isDisplayed()).count();
System.out.println("Total Link visible " + count);
P a g e 21 | 22
This example collects all HTML links (anchor tags) into a collection of WebElements. It then maps the
WebElements collection to a new collection of Strings and prints them to the console:
List<WebElement> linkElements = driver.findElements(By.tagName("a"));
List<String> linkTexts =
linkElements.stream().map(WebElement::getText).collect(Collectors.toList());
linkTexts.stream().forEach(System.out::println);
P a g e 22 | 22