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

License:Apache License

/**
 * @throws Exception if an error occurs//from  ww w.j  a  v a 2  s  .  c o  m
 */
@Test
public void grid_binding() throws Exception {
    final WebDriver driver = getPage("grid", "binding");

    // usually this need 1s but sometimes our build machine is slower
    // this is not an performance test, we only like to ensure that all
    // functionality is running
    Thread.sleep(2 * DEFAULT_WAIT_TIME);

    final WebElement detailPanel = driver.findElement(By.id("detailPanel"));
    final WebElement resultsDiv = detailPanel.findElement(By.xpath("div/div[1]"));
    assertEquals("Please select a book to see additional details.", resultsDiv.getText());

    final WebElement firstRowDiv = driver.findElement(By.xpath("//div[@class='x-grid3-body']/div[1]"));

    firstRowDiv.click();
    assertEquals("Title: Master of the Game\n" + "Author: Sidney Sheldon\n" + "Manufacturer: Warner Books\n"
            + "Product Group: Book", resultsDiv.getText());
}

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

License:Apache License

/**
 * Runs the specified test.//from  ww  w  . ja va 2  s  .  co m
 * @param testName the test name
 * @throws Exception if an error occurs
 */
protected void runTest(final String testName) throws Exception {
    final int testNumber = readTestNumber(testName);
    if (testNumber == -1) {
        assertEquals("Test number not found for: " + testName, 0, getExpectedAlerts().length);
        return;
    }
    final long runTime = 60 * DEFAULT_WAIT_TIME;
    final long endTime = System.currentTimeMillis() + runTime;

    try {
        final WebDriver webdriver = getWebDriver();
        final String url = "http://localhost:" + PORT + "/jquery/test/index.html?dev&testNumber=" + testNumber;
        webdriver.get(url);

        while (!getResultElementText(webdriver).startsWith("Tests completed")) {
            Thread.sleep(100);

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

        final WebElement output = webdriver.findElement(By.id("qunit-test-output0"));
        String result = output.getText();
        result = result.substring(0, result.indexOf("Rerun")).trim();
        final String expected = testName + " (" + getExpectedAlerts()[0] + ")";
        if (!expected.contains(result)) {
            System.out.println("--------------------------------------------");
            System.out.println("URL: " + url);
            System.out.println("--------------------------------------------");
            System.out.println("Test: " + webdriver.findElement(By.id("qunit-tests")).getText());
            System.out.println("--------------------------------------------");
            System.out.println("Failures:");
            final List<WebElement> failures = webdriver
                    .findElements(By.cssSelector(".qunit-assert-list li.fail"));
            for (WebElement webElement : failures) {
                System.out.println("  " + webElement.getText());
            }
            System.out.println("--------------------------------------------");

            fail(new ComparisonFailure("", expected, result).getMessage());
        }
    } catch (final Exception e) {
        e.printStackTrace();
        Throwable t = e;
        while ((t = t.getCause()) != null) {
            t.printStackTrace();
        }
        throw e;
    }
}

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

License:Apache License

private String getResultElementText(final WebDriver webdriver) throws InterruptedException {
    // if the elem is not available or stale we return an empty string
    // this will force a second try
    try {/*from   w  w w  .  ja v  a2  s . c  o m*/
        final WebElement elem = webdriver.findElement(By.id("qunit-testresult"));
        try {
            return elem.getText();
        } catch (final StaleElementReferenceException e) {
            return "";
        }
    } catch (final NoSuchElementException e) {
        return "";
    }
}

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

License:Apache License

/**
 * Runs the specified test./*from w  w w .j a va 2  s.c om*/
 * @param testName the test name
 * @throws Exception if an error occurs
 */
protected void runTest(final String testName) throws Exception {
    final int testNumber = readTestNumber(testName);
    if (testNumber == -1) {
        assertEquals("Test number not found for: " + testName, 0, getExpectedAlerts().length);
        return;
    }
    final long runTime = 60 * DEFAULT_WAIT_TIME;
    final long endTime = System.currentTimeMillis() + runTime;

    try {
        final WebDriver webdriver = getWebDriver();
        webdriver.get("http://localhost:" + PORT + "/jquery/test/index.html?testNumber=" + testNumber);

        final WebElement status = webdriver.findElement(By.id("qunit-testresult"));
        while (!status.getText().startsWith("Tests completed")) {
            Thread.sleep(100);

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

        final WebElement output = webdriver.findElement(By.id("qunit-test-output0"));
        String result = output.getText();
        result = result.substring(0, result.indexOf("Rerun")).trim();
        final String expected = testName + " (" + getExpectedAlerts()[0] + ")";
        if (!expected.contains(result)) {
            System.out.println("-> " + webdriver.findElement(By.id("qunit-tests")).getText());
            fail(new ComparisonFailure("", expected, result).getMessage());
        }
    } catch (final Exception e) {
        e.printStackTrace();
        Throwable t = e;
        while ((t = t.getCause()) != null) {
            t.printStackTrace();
        }
        throw e;
    }
}

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

License:Apache License

/**
 * Runs the specified test./* w  w w  .  ja v  a 2 s .  c o  m*/
 * @param testName the test name
 * @throws Exception if an error occurs
 */
@Override
protected void runTest(final String testName) throws Exception {
    final int testNumber = readTestNumber(testName);
    if (testNumber == -1) {
        assertEquals("Test number not found for: " + testName, 0, getExpectedAlerts().length);
        return;
    }
    final long runTime = 60 * DEFAULT_WAIT_TIME;
    final long endTime = System.currentTimeMillis() + runTime;

    try {
        final WebDriver webdriver = getWebDriver();
        final String url = URL_FIRST + "jquery/test/index.html?testNumber=" + testNumber;
        webdriver.get(url);

        final WebElement status = webdriver.findElement(By.id("qunit-testresult"));
        while (!status.getText().startsWith("Tests completed")) {
            Thread.sleep(100);

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

        final WebElement output = webdriver.findElement(By.id("qunit-test-output0"));
        String result = output.getText();
        result = result.substring(0, result.indexOf("Rerun")).trim();
        final String expected = testName + " (" + getExpectedAlerts()[0] + ")";
        if (!expected.contains(result)) {
            System.out.println("-> " + webdriver.findElement(By.id("qunit-tests")).getText());
            fail(new ComparisonFailure("", expected, result).getMessage());
        }
    } catch (final Exception e) {
        e.printStackTrace();
        Throwable t = e;
        while ((t = t.getCause()) != null) {
            t.printStackTrace();
        }
        throw e;
    }
}

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

License:Apache License

/**
 * Runs the specified test.//from   w  w  w .  ja  v  a  2 s .c om
 * @param testName the test name
 * @throws Exception if an error occurs
 */
protected void runTest(final String testName) throws Exception {
    final int testNumber = readTestNumber(testName);
    if (testNumber == -1) {
        assertEquals("Test number not found for: " + testName, 0, getExpectedAlerts().length);
        return;
    }
    final long runTime = 60 * DEFAULT_WAIT_TIME;
    final long endTime = System.currentTimeMillis() + runTime;

    try {
        final WebDriver webdriver = getWebDriver();
        final String url = URL_FIRST + "jquery/test/index.html?dev&testNumber=" + testNumber;
        webdriver.get(url);

        while (!getResultElementText(webdriver).startsWith("Tests completed")) {
            Thread.sleep(100);

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

        final WebElement output = webdriver.findElement(By.id("qunit-test-output0"));
        String result = output.getText();
        result = result.substring(0, result.indexOf("Rerun")).trim();
        final String expected = testName + " (" + getExpectedAlerts()[0] + ")";
        if (!expected.contains(result)) {
            System.out.println("--------------------------------------------");
            System.out.println("URL: " + url);
            System.out.println("--------------------------------------------");
            System.out.println("Test: " + webdriver.findElement(By.id("qunit-tests")).getText());
            System.out.println("--------------------------------------------");
            System.out.println("Failures:");
            final List<WebElement> failures = webdriver
                    .findElements(By.cssSelector(".qunit-assert-list li.fail"));
            for (WebElement webElement : failures) {
                System.out.println("  " + webElement.getText());
            }
            System.out.println("--------------------------------------------");

            fail(new ComparisonFailure("", expected, result).getMessage());
        }
    } catch (final Exception e) {
        e.printStackTrace();
        Throwable t = e;
        while ((t = t.getCause()) != null) {
            t.printStackTrace();
        }
        throw e;
    }
}

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

License:Apache License

private static String getResultElementText(final WebDriver webdriver) {
    // if the elem is not available or stale we return an empty string
    // this will force a second try
    try {/*ww w  .jav a 2s  .c  o  m*/
        final WebElement elem = webdriver.findElement(By.id("qunit-testresult"));
        try {
            return elem.getText();
        } catch (final StaleElementReferenceException e) {
            return "";
        }
    } catch (final NoSuchElementException e) {
        return "";
    }
}

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

License:Apache License

private void doTest(final String testName) throws Exception {
    final String url = URL_FIRST + "tests/test_MochiKit-" + testName + ".html";
    assertNotNull(url);/*  w  ww  .j  a  v  a  2 s. c  o m*/

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

    // make single test results visible
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.findElement(By.linkText("Toggle passed tests")).click();
    driver.findElement(By.linkText("Toggle failed tests")).click();
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    String expected = loadExpectation(testName);
    expected = expected.trim();
    expected = StringUtils.replace(expected, "\r\n", "\n");
    final WebElement div = driver.findElement(By.xpath("//div[@class = 'tests_report']"));

    assertNotNull(div);
    String actual = div.getText().trim();
    actual = StringUtils.replace(actual, "\n\n", "\n");
    assertEquals(expected.trim(), actual);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from  w  w  w  .j a va2  s  .  c  om*/
 */
@Alerts(CHROME = { "364", "1", "0",
        "should return the function bound to an object with multiple arguments" }, IE = { "364", "2", "0",
                "should return the function bound to an object with multiple arguments",
                "should return a CSS string representing the Element's styles" }, FF = { "364", "2", "0",
                        "should return true if the string constains the string and separator otherwise false",
                        "should return the function bound to an object with multiple arguments" })
@Test
@NotYetImplemented(IE)
public void mooTools() throws Exception {
    final WebDriver driver = getWebDriver();
    driver.get(URL_FIRST + "Specs/index.html");

    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    driver.findElement(By.xpath("id('progress')[text() = '100']"));
    // usually this need 40s but sometimes our build machine is slower
    // this is not an performance test, we only like to ensure that all
    // functionality is running

    final List<WebElement> failed = driver.findElements(By.xpath("//li[@class = 'exception']/h4"));
    final List<String> failures = new ArrayList<>();
    for (final WebElement elt : failed) {
        failures.add(elt.getText());
    }

    // final File tmpFile = File.createTempFile("htmlunit", "mootools.html");
    // System.out.println(tmpFile.getAbsolutePath());
    // FileUtils.writeStringToFile(tmpFile, driver.getPageSource());

    assertEquals(Arrays.copyOfRange(getExpectedAlerts(), 3, getExpectedAlerts().length), failures);

    assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("total_examples")).getText());
    assertEquals(getExpectedAlerts()[1], driver.findElement(By.id("total_failures")).getText());
    assertEquals(getExpectedAlerts()[2], driver.findElement(By.id("total_errors")).getText());
}

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

License:Apache License

@Override
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;
        }/*from w w w  .j av a 2 s . c om*/
    }
    return true;
}