-
Book Overview & Buying
-
Table Of Contents
Selenium Testing Tools Cookbook Second Edition
By :
The Selenium WebDriver does not implement Selenium RC's isElementPresent() method to check if an element is present on a page. This method is useful for building a reliable test that checks an element's presence before performing any action on it.
In this recipe, we will write a method similar to the isElementPresent() method.
To implement the isElementPresent() method, follow these steps:
Create a method isElementPresent() and keep it accessible to your tests, shown as follows:
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}Now, implement a test that calls the isElementPresent() method. It will check if the desired element is present on a page. If found, it will click on the element; else it fails the test. This is done as follows:
@Test
public void testIsElementPresent() {
// Check if element with locator criteria exists on Page...
Change the font size
Change margin width
Change background colour