Example usage for org.openqa.selenium WebElement getText

List of usage examples for org.openqa.selenium WebElement getText

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement getText.

Prototype

String getText();

Source Link

Document

Get the visible (i.e.

Usage

From source file:com.gargoylesoftware.htmlunit.libraries.PrototypeTestBase.java

License:Apache License

/**
 * Helper, because the element was different for the
 * different versions./*from   ww  w  . j  av a2s  .c o  m*/
 * @param driver the WebDriver
 * @return the WebElement
 */
protected boolean testFinished(final WebDriver driver) {
    final List<WebElement> status = driver.findElements(By.cssSelector("div.logsummary"));
    for (WebElement webElement : status) {
        if (!webElement.getText().contains("errors")) {
            return false;
        }
    }
    return true;
}

From source file:com.gargoylesoftware.htmlunit.libraries.PrototypeTestBase.java

License:Apache License

/**
 * Runs the specified test.//from w w w  .ja v  a 2  s.  com
 * @param filename the test file to run
 * @throws Exception if the test fails
 */
protected void test(final String filename) throws Exception {
    final WebDriver driver = getWebDriver();
    if (!(driver instanceof HtmlUnitDriver)) {
        try {
            driver.manage().window().setSize(new Dimension(1272, 768));
        } catch (final WebDriverException e) {
            // ChromeDriver version 0.5 (Mar 26, 2013) does not support the setSize command
            LOG.warn(e.getMessage(), e);
        }
    }
    driver.get(getBaseUrl() + filename);

    // wait
    final long runTime = 60 * DEFAULT_WAIT_TIME;
    final long endTime = System.currentTimeMillis() + runTime;

    while (!testFinished(driver)) {
        Thread.sleep(100);

        if (System.currentTimeMillis() > endTime) {
            fail("Test '" + filename + "' runs too long (longer than " + runTime / 1000 + "s)");
        }
    }

    String expected = getExpectations(getBrowserVersion(), filename);
    WebElement testlog = driver.findElement(By.id("testlog"));
    String actual = testlog.getText();

    try {
        testlog = driver.findElement(By.id("testlog_2"));
        actual = actual + "\n" + testlog.getText();
    } catch (final NoSuchElementException e) {
        // ignore
    }

    // ignore Info lines
    expected = expected.replaceAll("Info:.*", "Info: -- skipped for comparison --");
    actual = actual.replaceAll("Info:.*", "Info: -- skipped for comparison --");

    // normalize line break
    expected = expected.replaceAll("\r\n", "\n");
    actual = actual.replaceAll("\r\n", "\n");

    // dump the result page if not ok
    if (System.getProperty(PROPERTY_GENERATE_TESTPAGES) != null && !expected.equals(actual)) {
        final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
        final File f = new File(tmpDir, "prototype" + getVersion() + "_result_" + filename);
        FileUtils.writeStringToFile(f, driver.getPageSource(), UTF_8);
        LOG.info("Test result for " + filename + " written to: " + f.getAbsolutePath());
    }

    assertEquals(expected, actual);
}

From source file:com.gargoylesoftware.htmlunit.libraries.Sarissa0993Test.java

License:Apache License

/**
 * @param expectedResult empty for successful test or in the form of "+++F+++"
 * for failing tests (see the results in a real browser)
 *//*from w  ww  . j a  va2 s .co  m*/
private static void verify(final WebDriver driver, final String testName, final String expectedResult) {
    final WebElement div = driver
            .findElement(By.xpath("//div[@class='placeholder' and a[@name='#" + testName + "']]"));

    String text = div.getText();
    text = text.substring(0, text.indexOf(String.valueOf(expectedResult.length()))).trim();
    assertEquals(testName + " Results\n" + expectedResult, text);
}

From source file:com.gargoylesoftware.htmlunit.libraries.TinyMceTest.java

License:Apache License

private void test(final String fileName, final int expectedTotal, final int expectedFailed) throws Exception {
    final String url = URL_FIRST + "tests/" + fileName + ".html";
    assertNotNull(url);/* w w w . j  av  a2  s  .co m*/

    final WebDriver driver = getWebDriver();
    driver.get(url);

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    final WebElement result = driver.findElement(By.id("testresult"));
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    final WebElement totalSpan = result.findElement(By.xpath("./span[@class='all']"));
    final int total = Integer.parseInt(totalSpan.getText());
    assertEquals(expectedTotal, total);

    final List<WebElement> failures = driver.findElements(By.xpath("//li[@class='fail']"));
    final StringBuilder msg = new StringBuilder();
    for (WebElement failure : failures) {
        msg.append(failure.getText());
        msg.append("\n\n");
    }

    final WebElement failedSpan = result.findElement(By.xpath("./span[@class='bad']"));
    final int failed = Integer.parseInt(failedSpan.getText());
    assertEquals(msg.toString(), expectedFailed, failed);
}

From source file:com.gargoylesoftware.htmlunit.libraries.YuiTest.java

License:Apache License

private void doTest(final String fileName, final List<String> knownFailingTests, final String buttonToPush,
        final long timeToWait) throws Exception {

    // final URL url = getClass().getClassLoader().getResource("tests/" + fileName);
    final String url = URL_FIRST + "tests/" + fileName;
    assertNotNull(url);/* w w  w . j  a v a  2 s .c om*/

    final WebDriver driver = getWebDriver();
    driver.get(url);

    if (buttonToPush != null) {
        driver.findElement(By.id(buttonToPush)).click();
    }

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    final WebElement logDiv = driver.findElement(By.className("yui-log-bd"));
    final WebElement lastMessage = logDiv
            .findElement(By.xpath("pre[last() and contains(string(.), 'Testing completed')]"));

    LOG.info(lastMessage.getText());

    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
    final List<WebElement> tests = driver.findElements(By.xpath("//p[span[@class='pass' or @class='fail']]"));
    if (tests.isEmpty()) {
        fail("No tests were executed!");
    }

    for (final WebElement pre : tests) {
        final String[] parts;
        try {
            parts = pre.getText().split(" ");
        } catch (final StaleElementReferenceException e) {
            continue; // happens for FF17 on imageLoader test
        }
        final String result = parts[0];
        final String testName = parts[1].substring(0, parts[1].length() - 1);
        if ("pass".equalsIgnoreCase(result)) {
            assertTrue("Test case '" + testName + "' is in the known failing list, but passes!",
                    !knownFailingTests.contains(testName));
        } else {
            assertTrue("Test case '" + testName + "' is not in the known failing list, but fails!",
                    knownFailingTests.contains(testName));
        }
    }
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

private static void checkRecordedKeySequence(final WebElement element, final int expectedKeyCode) {
    assertThat(element.getText().trim(),
            anyOf(is(String.format("down: %1$d press: %1$d up: %1$d", expectedKeyCode)),
                    is(String.format("down: %1$d up: %1$d", expectedKeyCode))));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test./*from   w  ww  . j  a  va 2 s.  c o m*/
 */
@Test
public void shouldReportKeyCodeOfArrowKeysUpDownEvents() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement result = driver.findElement(By.id("result"));
    final WebElement element = driver.findElement(By.id("keyReporter"));

    element.sendKeys(Keys.ARROW_DOWN);
    assertThat(result.getText().trim(), containsString("down: 40"));
    assertThat(result.getText().trim(), containsString("up: 40"));

    element.sendKeys(Keys.ARROW_UP);
    assertThat(result.getText().trim(), containsString("down: 38"));
    assertThat(result.getText().trim(), containsString("up: 38"));

    element.sendKeys(Keys.ARROW_LEFT);
    assertThat(result.getText().trim(), containsString("down: 37"));
    assertThat(result.getText().trim(), containsString("up: 37"));

    element.sendKeys(Keys.ARROW_RIGHT);
    assertThat(result.getText().trim(), containsString("down: 39"));
    assertThat(result.getText().trim(), containsString("up: 39"));

    // And leave no rubbish/printable keys in the "keyReporter"
    assertThat(element.getAttribute("value"), is(""));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test.//from  w w w.j  a va 2  s .  c o m
 */
@Test
public void numericShiftKeys() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement result = driver.findElement(By.id("result"));
    final WebElement element = driver.findElement(By.id("keyReporter"));

    final String numericShiftsEtc = "~!@#$%^&*()_+{}:\"<>?|END~";
    element.sendKeys(numericShiftsEtc);

    assertThat(element.getAttribute("value"), is(numericShiftsEtc));
    assertThat(result.getText().trim(), containsString(" up: 16"));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test.//from  w  w w .  j  a v a 2  s  .c  o m
 */
@Test
public void uppercaseAlphaKeys() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement result = driver.findElement(By.id("result"));
    final WebElement element = driver.findElement(By.id("keyReporter"));

    final String upperAlphas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    element.sendKeys(upperAlphas);

    assertThat(element.getAttribute("value"), is(upperAlphas));
    assertThat(result.getText().trim(), containsString(" up: 16"));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test./*from  ww  w.  j a va  2s.c o m*/
 */
@Test
public void allPrintableKeys() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement result = driver.findElement(By.id("result"));
    final WebElement element = driver.findElement(By.id("keyReporter"));

    final String allPrintable = "!\"#$%&'()*+,-./0123456789:;<=>?@ ABCDEFGHIJKLMNO"
            + "PQRSTUVWXYZ [\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
    element.sendKeys(allPrintable);

    assertThat(element.getAttribute("value"), is(allPrintable));
    assertThat(result.getText().trim(), containsString(" up: 16"));
}