• Save
What the FUF?
Upcoming SlideShare
Loading in...5
×
 

What the FUF?

on

  • 842 views

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 ...

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.

Statistics

Views

Total Views
842
Views on SlideShare
839
Embed Views
3

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 3

http://www.linkedin.com 3

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

What the FUF? What the FUF? Presentation Transcript

  • An DoanPortland Selenium Users Group July 19th, 2012
  •  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
  • 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 visibilityOfElementvisibilityOf presenceOfAllElementsLocatedBy textToBePresentInElement textToBePresentInElementByValuetitleContains invisibilityOfElementLocated elementToBeClickable elementToBeSelectedstalenessOf elementSelectionStateToBe elementToBeSelected elementSelectionStateToBe View slide
  •  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); View slide
  •  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
  • public String switchToWindow(String title){try{ if (prevWindow == null || prevWindow != title) { prevWindow = myDriver.getTitle(); prevWindowHandle = myDriver.getWindowHandle(); myLog.logTcStep("Saved Prev Window Title:" + prevWindow); } myLog.logTcStep("Switching To Window Title:" + title); return getHandleByTitle(title);} catch (Exception e) {}return null;}
  • private String getHandleByTitle(String title){String currentHandle = myDriver.getWindowHandle();for (String handle : myDriver.getWindowHandles()){ if (title.equals(myDriver.switchTo().window (handle).getTitle())) { myLog.logStep("Found new window handle by title: " + handle); return handle; }}myLog.logStep("Could not find new window handle by title");return currentHandle;}
  • private String getHandleByUrl(String url){String currentHandle = myDriver.getWindowHandle(); for (String handle : myDriver.getWindowHandles()) { if ((myDriver.switchTo().window (handle).getCurrentUrl()).contains(url)) { return handle; } }return currentHandle;}
  •  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
  • String currentWindow = myDriver.getWindowHandle();Set<String> windows = myDriver.getWindowHandles();for (Iterator<String> iterator = windows.iterator(); iterator.hasNext();){ String string = (String) iterator.next(); if (!currentWindow.equals(string)) { myDriver.switchTo().window(string).close(); myDriver.switchTo().window(currentWindow); Util.sleep(1000); return true; }}return false;
  •  Problem: The developer for my application likes to use a lot of iframes and Selenium doesn’t seem to work. Solution: Usedriver.switchTo().frame(iFrameName)driver.switchTo().frame(iFrameIndex) ◦ Switches context to the frame ◦ Return to default context with myDriver.switchTo().defaultContent();
  • 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;
  •  Problem: I need to hover over and menu and then click a submenu Solution: Use the Action classhoverOverElement = waitForAndGetElement (locatorForHover);Actions action = new Actions(myDriver);action = action.moveToElement(hoverOverElement). moveToElement(waitForAndGetElement(locatorForClick)). click();action.perform();
  • keyDown clickkeyUp doubleClicksendKeys moveToElementclickAndHold moveByOffsetrelease contextClickdragAndDrop
  •  Problem: An alert window appears and blocks the automation. Solution: Use the Alert class (works most of the time)
  • try{ Alert alert = myDriver.switchTo().alert(); // And acknowledge the alert (equivalent to clicking "OK") alert.accept(); //OK alert.dismiss(); //”X” alert.sendKeys(Keys.ENTER); //Hit Enter on keyboard myLog.logTcStep("Closing Alert"); return true;} catch (Exception e) { // no alert myLog.logTcStep("Cannot Close Alert"); e.printStackTrace(); return false;}
  •  Problem: AndroidDriver does not support Select, the native spinner pops up Solution: Use Javascript injection
  • WebElement select = getElement(locator);List<WebElement> options = select.findElements(By.tagName("option"));int optionNum = 0;for(WebElement option : options){ if(option.getText().equals(selection)) { try { ((JavascriptExecutor) myDriver).executeScript ("document.getElementById("newLocationSource").options["+ optionNum + "].selected = true"); Util.sleep(2000); return true; } catch (Exception e) {} break; }optionNum++;}
  •  Events not firing?//send enter key to select boxselect. sendKeys(Keys.ENTER)//dismiss the native spinnerRuntime.getRuntime().exec("cmd.exe /K "+ env.get("ANDROID_HOME") + “/platform-tools/adb shell input keyevent 66"); //key enter
  •  Aug. 16th ◦ Open Sept. 13th (Moved from the 20th) ◦ Lee Copeland – Topic TBDThank You Sponsors!