protected String throwAssertionFailureExceptionOrError(String message) { throw new SeleniumException(message); }
private boolean isTimeoutException(SeleniumException e) { return e.getMessage().startsWith("Timed out after"); } }
return; } catch (SeleniumException e) { final String message = StringUtils.defaultString(e.getMessage()); exceptionLogged = true; System.err.println(ctx.toString()); e.printStackTrace();
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!"); }
@Override public boolean executeTest(Selenium selenium) { try { return !(client.isElementPresent(locator) && client.isVisible(locator)); } catch(SeleniumException ex) { //there is a race condition here where the element can be removed before isVisible is called, // and isVisible throws a SeleniumException because the element is not found, so just return false and run the // test again. if(ex.getMessage().endsWith(locator + " not found")) return false; else throw ex; } }
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; }
private String getCommandOutput(ExtendedSeleniumCommand command, String[] values) { try { if (command.returnTypeIsArray()) { return executeArrayCommand(command.getSeleniumCommand(), values); } else { return executeCommand(command.getSeleniumCommand(), values); } } catch (final SeleniumException e) { String output = "Execution of command failed: " + e.getMessage(); LOG.error(output); if (!(command.isAssertCommand() || command.isVerifyCommand() || command.isWaitForCommand())) { throw e; } return output; } }
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 boolean isTrue(ConditionRunner.Context runner) { throw new SeleniumException("Yeehaa!"); } });
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); }
/** * 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; }
@Override public WebElement findElement(WebDriver driver, String locator) { WebElement toReturn = null; String strategy = searchAdditionalStrategies(locator); if (strategy != null) { String actualLocator = locator.substring(locator.indexOf('=') + 1); try { toReturn = (WebElement) ((JavascriptExecutor) driver).executeScript(strategy, actualLocator); if (toReturn == null) { throw new SeleniumException("Element " + locator + " not found"); } return toReturn; } catch (WebDriverException e) { throw new SeleniumException("Element " + locator + " not found"); } } try { return findElementDirectly(driver, locator); } catch (WebDriverException e) { throw new SeleniumException("Element " + locator + " not found", e); } }
/** * 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"); } }
/** * Gets the text from a cell of a table. The cellAddress syntax * tableLocator.row.column, where row and column start at 0. * * @param tableCellAddress a cell address, e.g. "foo.1.4" * @return the text from the specified cell */ public String getTable(String tableCellAddress) { Matcher matcher = TABLE_PARTS.matcher(tableCellAddress); if (!matcher.matches()) { throw new SeleniumException("Invalid target format. Correct format is tableName.rowNum.columnNum"); } String tableName = matcher.group(1); long row = Long.parseLong(matcher.group(2)); long col = Long.parseLong(matcher.group(3)); WebElement table = findElement(tableName); String script = "var table = arguments[0]; var row = arguments[1]; var col = arguments[2];" + "if (row > table.rows.length) { return \"Cannot access row \" + row + \" - table has \" + table.rows.length + \" rows\"; }" + "if (col > table.rows[row].cells.length) { return \"Cannot access column \" + col + \" - table row has \" + table.rows[row].cells.length + \" columns\"; }" + "return table.rows[row].cells[col];"; Object value = executeScript(script, table, row, col); if (value instanceof WebElement) { return ((WebElement) value).getText().trim(); } throw new SeleniumException((String) value); }