Having trouble getting selenium WebDriver to do what you want? Want to put together a Swiss Army library for WebDriver? Thinking about creating your own framework? This presentation will show you some of the most common functions you can do in WebDriver. These functions come from my current automation framework, my automation experience, and from common trends on the WebDriver Google group.
2. Problem: I have some javascript or AJAX that
runs after the DOM is loaded.
Solution: User WebDriverWait
◦ Repeats a function until one of the following
conditions are met
the function returns neither null nor false
the function throws an unignored exception
the timeout expires
the current thread is interrupted
3. WebDriverWait wait = new WebDriverWait(driver, timeoutsec);
WebElement element = Wait.until(myCondition);
ExpectedCondition<WebElement> myCondition = new ExpectedCondition<WebElement>()
{
public WebElement apply(WebDriver d)
{
return d.findElement(By.id(“someid"));
}
}
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
titleIs frameToBeAvailableAndSwitchToIt PresenseOfElement visibilityOfElement
visibilityOf presenceOfAllElementsLocatedBy textToBePresentInElement textToBePresentInElementByValue
titleContains invisibilityOfElementLocated elementToBeClickable elementToBeSelected
stalenessOf elementSelectionStateToBe elementToBeSelected elementSelectionStateToBe
4. Problem: My test environment or test case
requires me to edit some cookies or create
new cookies.
Solution:
driver.manage.getCookieNamed(cookieName)
driver.manage.addCookie(cookie)
myDriver.manage().getCookieNamed(“sesssionID”);
Cookie cookie = new Cookie(name, value, domain, path, expiry, isSecure);
myDriver.manage().addCookie(cookie);
5. Problem: I just clicked on a link and it opened
a new window, now what?
Solution: Switch to the window using
driver.switchTo.window(“windowHandle”)
◦ Iterate through all window handles to find the new
window, wither by title or url
◦ Switch to the window using the window handle
9. Problem: When I click on a link it opens a
popup window, how do I close it when I am
done validating it?
Solution: Use window handles and
driver.switchTo().window().close()
◦ Iterate through the window handles
◦ Close everything else but the main window
11. Problem: The developer for my application
likes to use a lot of iframes and Selenium
doesn’t seem to work.
Solution: Use
driver.switchTo().frame(iFrameName)
driver.switchTo().frame(iFrameIndex)
◦ Switches context to the frame
◦ Return to default context with
myDriver.switchTo().defaultContent();
12. List<WebElement> elements = myDriver.findElements(By.tagName("iframe"));
if (elements != null)
iframes = elements.size();
for (int f = 0; f < iframes ; f++)
{
myDriver.switchTo().frame(f);
try
{
element = myDriver.findElement(locator);
return element;
}catch (Exception e) {}
}
return null;
13. Problem: I need to hover over and menu and
then click a submenu
Solution: Use the Action class
hoverOverElement = waitForAndGetElement (locatorForHover);
Actions action = new Actions(myDriver);
action =
action.moveToElement(hoverOverElement).
moveToElement(waitForAndGetElement(locatorForClick)).
click();
action.perform();