protected String throwAssertionFailureExceptionOrError(String message) { throw new SeleniumException(message); }
long targetTime = System.currentTimeMillis() + 10000; // 10 seconds from now boolean found = false; while (System.currentTimeMillis() < targetTime && !found) { if (selenium.isElementPresent("link=Sign In")) { selenium.click("link=Sign In"); found = true; } } if (!found) { throw new SeleniumException("Element not found!"); }
protected LookupStrategy findStrategy(String locator) { String strategyName = "implicit"; Matcher matcher = STRATEGY_AND_VALUE_PATTERN.matcher(locator); if (matcher.matches()) { strategyName = matcher.group(1); } LookupStrategy strategy = lookupStrategies.get(strategyName); if (strategy == null) throw new SeleniumException("No matcher found for " + strategyName); return strategy; }
public List<WebElement> filterElements(List<WebElement> allElements, String filterValue) { try { int index = Integer.parseInt(filterValue); if (allElements.size() > index) return Collections.singletonList(allElements.get(index)); } catch (NumberFormatException e) { // Handled below } throw new SeleniumException("Element with index " + filterValue + " not found"); } }
/** Sends the specified command string to the bridge servlet */ public String executeCommandOnServlet(String command) { InputStream is = null; try { return getCommandResponseAsString(command); } catch (IOException e) { if (e instanceof ConnectException) { throw new SeleniumException(e.getMessage(),e); } e.printStackTrace(); throw new UnsupportedOperationException("Catch body broken: IOException from " + command + " -> " + e, e); } }
public void waitForMacroPresent(final String macroName) { try { client.waitForCondition("$('table.wysiwyg-macro, img.editor-inline-macro', " + DOC + ").filter('[data-macro-name=" + macroName + "]').length > 0"); } catch (SeleniumException e) { throw new SeleniumException("The '"+macroName+"' macro could not be found.", e); } }
public void start() { throw new SeleniumException(message); }
public boolean isTrue(ConditionRunner.Context runner) { throw new SeleniumException("Yeehaa!"); } });
/** * Add a selection to the set of selected options in a multi-select element using an option locator. * * @param locator an <a href="#locators">element locator</a> identifying a multi-select box * @param optionLocator an option locator (a label by default) */ public void addSelection(String locator, String optionLocator) { WebElement select = findElement(locator); if (!"multiple".equals(select.getAttribute("multiple"))) throw new SeleniumException("You may only add a selection to a select that supports multiple selections"); select(locator, optionLocator, true, false); }
/** * Remove a selection from the set of selected options in a multi-select element using an option locator. * * @param locator an <a href="#locators">element locator</a> identifying a multi-select box * @param optionLocator an option locator (a label by default) */ public void removeSelection(String locator, String optionLocator) { WebElement select = findElement(locator); if (!"multiple".equals(select.getAttribute("multiple"))) throw new SeleniumException("You may only remove a selection to a select that supports multiple selections"); select(locator, optionLocator, false, false); }
@Override public boolean isTrue() { Validate.notNull(message); if (!selenium.isAlertPresent()) { return false; } String alertMessage = selenium.getAlert(); if (!message.equals(alertMessage)) { throw new SeleniumException("Alert has been displayed, but the message '" + message + "' doesn't equal to the expected '" + alertMessage + "'"); } return true; }
/** * Gets option index (option number, starting at 0) for selected option in the specified select element. * * @param selectLocator an <a href="#locators">element locator</a> identifying a drop-down menu * @return the selected option index in the specified select drop-down */ public String getSelectedIndex(String selectLocator) { List<WebElement> options = getOptions(selectLocator); for (int i = 0; i < options.size(); i++) { WebElement option = options.get(i); if (option.isSelected()) return String.valueOf(i); } throw new SeleniumException("No option is selected: " + selectLocator); }
private String[] findSelectedOptionProperties(String selectLocator, String property) { List<WebElement> options = getOptions(selectLocator); List<String> selectedOptions = new ArrayList<String>(); for (WebElement option : options) { if (option.isSelected()) { if ("text".equals(property)) { selectedOptions.add(option.getText()); } else if ("value".equals(property)) { selectedOptions.add(option.getValue()); } else { String propVal = option.getAttribute(property); if (propVal != null) selectedOptions.add(propVal); } } } if (selectedOptions.size() == 0) throw new SeleniumException("No option selected"); return selectedOptions.toArray(new String[selectedOptions.size()]); }
public WebElement find(WebDriver driver, String use) { List<WebElement> elements = driver.findElements(By.xpath("//a")); String strategyName = "implicit"; Matcher matcher = TEXT_MATCHING_STRATEGY_AND_VALUE_PATTERN.matcher(use); if (matcher.matches()) { strategyName = matcher.group(1); use = matcher.group(2); } TextMatchingStrategy strategy = textMatchingStrategies.get(strategyName); Iterator<WebElement> allLinks = elements.iterator(); while (allLinks.hasNext()) { WebElement link = allLinks.next(); if (!(strategy.isAMatch(use, link.getText()))) allLinks.remove(); } if (elements.size() > 0) return elements.get(0); throw new SeleniumException(use + " not found"); } }
private List<WebElement> getOptions(String selectLocator) { WebElement element = findElement(selectLocator); List<WebElement> options = element.findElements(By.tagName("option")); if (options.size() == 0) { throw new SeleniumException("Specified element is not a Select (has no options)"); } return options; }
protected WebElement findElement(String locator) { LookupStrategy strategy = findStrategy(locator); String use = determineWebDriverLocator(locator); try { return strategy.find(driver, use); } catch (NoSuchElementException e) { throw new SeleniumException("Element " + locator + " not found"); } }
/** * Runs the provided Javascript function in the context of the test page. The function should * contain a return statement to evaluate. * * @throws SeleniumException a wrapped exception from selenium which indicates the condition that failed to run. */ public void waitForFunction(String javascript, String timeoutMillis) { // within the function, '$' is jQuery and 'this' is the document under test final String condition = "(function() {var w = selenium.browserbot.getCurrentWindow(); return (function($){ " + javascript + " }).call(w, w.jQuery)})()"; try { super.waitForCondition(condition, timeoutMillis); } catch (SeleniumException e) { //run some script to capture the state of various flags so that when a timeout happens on bamboo, //some evidence remains of the state of the flags for debugging later log.error("waitForFunction : this.ajsScriptsFinishedLoading=" + getEval("this.ajsScriptsFinishedLoading")); throw new SeleniumException("The javascript function with result [" + getEval(condition) + "] failed: " + javascript, e); } }
/** * Determines whether some option in a drop-down menu is selected. * * @param selectLocator an <a href="#locators">element locator</a> identifying a drop-down menu * @return true if some option has been selected, false otherwise */ public boolean isSomethingSelected(String selectLocator) { WebElement select = findElement(selectLocator); String tagName = select.getTagName().toLowerCase(); if (!"select".equals(tagName)) { throw new SeleniumException("Specified element is not a Select"); } for (WebElement option : select.findElements(By.tagName("option"))) { if (option.isSelected()) { return true; } } return false; }
/** * Selects a frame within the current window. (You may invoke this command * multiple times to select nested frames.) To select the parent frame, use * "relative=parent" as a locator; to select the top frame, use "relative=top". * You can also select a frame by its 0-based index number; select the first frame with * "index=0", or the third frame with "index=2". * <p/> * <p>You may also use a DOM expression to identify the frame you want directly, * like this: <code>dom=frames["main"].frames["subframe"]</code></p> * * @param locator an <a href="#locators">element locator</a> identifying a frame or iframe */ public void selectFrame(String locator) { if ("relative=top".equals(locator)) { driver.switchTo().defaultContent(); return; } try { driver.switchTo().frame(locator); } catch (NoSuchFrameException e) { throw new SeleniumException(e.getMessage(), e); } }
private void selectWindowWithTitle(String title) { String current = driver.getWindowHandle(); for (String handle : driver.getWindowHandles()) { driver.switchTo().window(handle); if (title.equals(driver.getTitle())) { return; } } driver.switchTo().window(current); throw new SeleniumException("Unable to select window with title: " + title); }