Example usage for org.openqa.selenium By toString

List of usage examples for org.openqa.selenium By toString

Introduction

In this page you can find the example usage for org.openqa.selenium By toString.

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:edu.samplu.common.WebDriverITBase.java

License:Educational Community License

/**
 * //  ww w .j  a va2s .  c  o m
 *
 * @param by method used for finding the element
 * @param message user defined message to display
*/
protected void waitAndClick(By by, String message) throws InterruptedException {
    waitFor(by, message);
    try {
        (driver.findElement(by)).click();
    } catch (Exception e) {
        fail(e.getMessage() + " " + by.toString() + " " + message);
        e.printStackTrace();
    }
}

From source file:edu.samplu.common.WebDriverLegacyITBase.java

License:Educational Community License

private void jiraAwareFail(By by, String message, Throwable t) {
    JiraAwareFailureUtil.failOnMatchedJira(by.toString(), message, this);
    // if there isn't a matched jira to fail on, then fail
    checkForIncidentReport(by.toString(), message);
    failableFail(t.getMessage() + " " + by.toString() + " " + message + " " + driver.getCurrentUrl());
}

From source file:edu.samplu.common.WebDriverLegacyITBase.java

License:Educational Community License

/**
 * Uses Selenium's findElements method which does not throw a test exception if not found.
 * @param by/*from   ww w .java2  s.co m*/
 * @param optionValue
 * @throws InterruptedException
 */
protected void selectOption(By by, String optionValue) throws InterruptedException {
    WebElement select1 = driver.findElement(by);
    List<WebElement> options = select1.findElements(By.tagName("option"));

    if (options == null || options.size() == 0) {
        failableFail("No options for select " + select1.toString() + " was looking for value " + optionValue
                + " using " + by.toString());
    }

    for (WebElement option : options) {
        if (option.getAttribute("value").equals(optionValue)) {
            option.click();
            break;
        }
    }
}

From source file:edu.samplu.common.WebDriverLegacyITBase.java

License:Educational Community License

protected void waitAndType(By by, String text, String message) throws InterruptedException {
    try {/*w w w .  j a v  a 2s . c o  m*/
        jiraAwareWaitFor(by, "");
        WebElement element = driver.findElement(by);
        WebDriverUtil.highlightElement(driver, driver.findElement(by));
        (driver.findElement(by)).sendKeys(text);
    } catch (Exception e) {
        JiraAwareFailureUtil.failOnMatchedJira(by.toString(), this);
        failableFail(e.getMessage() + " " + by.toString() + "  unable to type text '" + text + "'  " + message
                + " current url " + driver.getCurrentUrl() + "\n" + ITUtil.deLinespace(driver.getPageSource()));
    }
}

From source file:edu.samplu.common.WebDriverLegacyITBase.java

License:Educational Community License

protected void waitIsVisible(By by, String message) throws InterruptedException {
    for (int second = 0;; second++) {
        if (second >= waitSeconds) {
            failableFail(TIMEOUT_MESSAGE + " " + by.toString() + " " + message);
        }//from   w  w  w  .ja  v  a2 s  . c  o  m
        if (isVisible(by)) {
            break;
        }
        Thread.sleep(1000);
    }
}

From source file:info.magnolia.integrationtests.uitest.AbstractMagnoliaUITest.java

License:Open Source License

/**
 * Tries to retrieve requested element./* w w  w. jav  a2 s .c  o  m*/
 *
 * @param by locator of an element
 * @return the searched specified element or a NonExistingWebElement in case it couldn't be found.
 */
protected WebElement getElement(final By by) {
    WebElement element;
    try {
        // will loop and try to retrieve the specified element until found or it times out.
        element = new WebDriverWait(driver, timeout).until(new ExpectedCondition<WebElement>() {

            @Override
            public WebElement apply(WebDriver d) {
                try {
                    WebElement element = d.findElement(by);
                    if (element.isDisplayed()) {
                        takeScreenshot(by.toString());
                        return element;
                    }
                    takeScreenshot(by.toString() + "_notDisplayed");
                    return null;
                } catch (NoSuchElementException e) {
                    takeScreenshot(by.toString() + "_notFound");
                    return null;
                }
            }
        });
    } catch (TimeoutException e) {
        log.debug("Could not retrieve element by path {}. Got: {}", by, e);
        // not found within the time limit - assume that element is not existing
        element = new NonExistingWebElement(by.toString());
    } catch (StaleElementReferenceException s) {
        // re-trying on StaleElementReferenceExceptions: see http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp
        log.info("{} when accessing element {} - trying again", s.toString(), by);
        element = getElement(by);
    }
    return element;
}

From source file:info.magnolia.integrationtests.uitest.AbstractMagnoliaUITest.java

License:Open Source License

/**
 * Tries to retrieve requested elements.
 *
 * <p>Tries to retrieve the requested amount of elements matching the given path.
 * Will retry until the amount matches or until the whole process times out.</p>
 *
 * @param by locator of an element//from w ww .ja  va 2 s . c om
 * @return a list matching the searched specified element or <code>null</code> in case it couldn't be found.
 */
protected List<WebElement> getElements(final By by, final int expectedElementCount) {
    List<WebElement> elements;
    try {
        // will loop and try to retrieve the specified element until found or it times out.
        elements = new WebDriverWait(driver, timeout).until(new ExpectedCondition<List<WebElement>>() {

            @Override
            public List<WebElement> apply(WebDriver d) {
                try {
                    List<WebElement> elements = d.findElements(by);
                    if ((elements.size() > 0 && expectedElementCount == -1)
                            || (elements.size() == expectedElementCount)) {
                        takeScreenshot(by.toString());
                        return elements;
                    }
                    log.warn("Expecting {} element(s) for {} - trying again - found {} so far: {}",
                            expectedElementCount != -1 ? expectedElementCount : "at least 1", by,
                            elements.size(), elements);
                    takeScreenshot(by.toString() + "_wrongCount");
                    return null;
                } catch (NoSuchElementException e) {
                    takeScreenshot(by.toString() + "_notFound");
                    return null;
                }
            }
        });
    } catch (TimeoutException e) {
        log.error("Could not retrieve " + (expectedElementCount != -1 ? expectedElementCount : "at least 1")
                + " elements by path " + by + " : " + e.getMessage());
        return Collections.emptyList();
    } catch (StaleElementReferenceException s) {
        // re-trying on StaleElementReferenceExceptions: see http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp
        log.info("{} when accessing element {} - trying again", s.toString(), by);
        elements = getElements(by, expectedElementCount);
    }
    return elements;
}

From source file:info.magnolia.integrationtests.uitest.AbstractMagnoliaUITest.java

License:Open Source License

private WebElement getPotentiallyHiddenTab(String tabCaption, String... parentTitles) {
    final String parentXPath = buildPathFromTitles(parentTitles);
    final String tabCaptionPath = String.format(
            "//*[contains(@class, 'v-shell-tabsheet')]//*[@class = 'tab-title' and text() = '%s']", tabCaption);
    final By byFullTab = getElementLocatorByXpath("%s%s", parentXPath, tabCaptionPath);

    // Use multi-element search method in order to work-around a limitation of #getElementByXpath() - it returns only visible element,
    // whereas here we're interested if the element just exists in DOM
    final List<WebElement> allMatchingElements = getElements(byFullTab);

    return allMatchingElements.isEmpty() ? new NonExistingWebElement(byFullTab.toString())
            : allMatchingElements.get(0);
}

From source file:io.ddavison.conductor.Locomotive.java

License:Open Source License

/**
 * Method that acts as an arbiter of implicit timeouts of sorts.. sort of like a Wait For Ajax method.
 *//*from ww w  . j a  v a  2  s.co m*/
public WebElement waitForElement(By by) {
    int attempts = 0;
    int size = driver.findElements(by).size();

    while (size == 0) {
        size = driver.findElements(by).size();
        if (attempts == MAX_ATTEMPTS)
            fail(String.format("Could not find %s after %d seconds", by.toString(), MAX_ATTEMPTS));
        attempts++;
        try {
            Thread.sleep(1000); // sleep for 1 second.
        } catch (Exception x) {
            fail("Failed due to an exception during Thread.sleep!");
            x.printStackTrace();
        }
    }

    if (size > 1)
        System.err.println("WARN: There are more than 1 " + by.toString() + " 's!");

    return driver.findElement(by);
}

From source file:io.ddavison.conductor.Locomotive.java

License:Open Source License

public Locomotive check(By by) {
    if (!isChecked(by)) {
        waitForCondition(ExpectedConditions.not(ExpectedConditions.invisibilityOfElementLocated(by)))
                .waitForCondition(ExpectedConditions.elementToBeClickable(by));
        waitForElement(by).click();//w ww  . j av a2s  .  co  m
        assertTrue(by.toString() + " did not check!", isChecked(by));
    }
    return this;
}