Example usage for org.openqa.selenium WebDriver findElements

List of usage examples for org.openqa.selenium WebDriver findElements

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver findElements.

Prototype

@Override
List<WebElement> findElements(By by);

Source Link

Document

Find all elements within the current page using the given mechanism.

Usage

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from  www . j  av a2s  .com*/
 */
@Test
public void core_templates() throws Exception {
    final WebDriver driver = getPage("core", "templates");
    final List<WebElement> buttons = driver.findElements(By.xpath("//button"));
    final List<WebElement> divs = driver.findElements(By.xpath("//div[@class='x-panel-body']"));
    assertEquals(2, buttons.size());
    assertEquals(2, divs.size());
    assertEquals("Apply the template to see results here", divs.get(0).getText());
    assertEquals("Apply the template to see results here", divs.get(1).getText());

    buttons.get(0).click();
    assertEquals("Name: Jack Slocum\n" + "Company: Ext JS, LLC\n" + "Location: Cleveland, Ohio",
            divs.get(0).getText());
    assertEquals("Apply the template to see results here", divs.get(1).getText());

    buttons.get(1).click();
    assertEquals("Name: Jack Slocum\n" + "Company: Ext JS, LLC\n" + "Location: Cleveland, Ohio",
            divs.get(0).getText());
    assertEquals(
            "Name: Jack Slocum\n" + "Company: Ext JS, LLC\n" + "Location: Cleveland, Ohio\n" + "Kids:\n"
                    + "1. Jack Slocum's kid - Sara Grace\n" + "2. Jack Slocum's kid - Zachary",
            divs.get(1).getText());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from   w  w w. j a va 2 s  .c o  m
 */
@Test
public void core_spotlight() throws Exception {
    final WebDriver driver = getPage("core", "spotlight");
    final List<WebElement> buttons = driver.findElements(By.xpath("//button"));
    assertEquals(4, buttons.size());
    assertEquals("Start", buttons.get(0).getText());
    assertEquals("Next Panel", buttons.get(1).getText());
    assertEquals("Next Panel", buttons.get(2).getText());
    assertEquals("Done", buttons.get(3).getText());

    assertTrue(core_spotlight_isDisabled(buttons.get(1)));
    assertTrue(core_spotlight_isDisabled(buttons.get(2)));
    assertTrue(core_spotlight_isDisabled(buttons.get(3)));

    buttons.get(0).click();
    Thread.sleep(200);
    assertFalse(core_spotlight_isDisabled(buttons.get(1)));
    assertTrue(core_spotlight_isDisabled(buttons.get(2)));
    assertTrue(core_spotlight_isDisabled(buttons.get(3)));

    buttons.get(1).click();
    Thread.sleep(200);
    assertTrue(core_spotlight_isDisabled(buttons.get(1)));
    assertFalse(core_spotlight_isDisabled(buttons.get(2)));
    assertTrue(core_spotlight_isDisabled(buttons.get(3)));

    buttons.get(2).click();
    Thread.sleep(200);
    assertTrue(core_spotlight_isDisabled(buttons.get(1)));
    assertTrue(core_spotlight_isDisabled(buttons.get(2)));
    assertFalse(core_spotlight_isDisabled(buttons.get(3)));

    buttons.get(3).click();
    Thread.sleep(200);
    assertTrue(core_spotlight_isDisabled(buttons.get(1)));
    assertTrue(core_spotlight_isDisabled(buttons.get(2)));
    assertTrue(core_spotlight_isDisabled(buttons.get(3)));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from   w w w. ja va  2 s. c  o m
 */
@Test
public void debug_console() throws Exception {
    final WebDriver driver = getPage("debug", "debug-console");

    final List<WebElement> anchors = driver.findElements(By.xpath("//a"));
    assertEquals(2, anchors.size());

    anchors.get(1).click();
    assertEquals("Hello from the Ext console.",
            driver.findElement(By.xpath("//div[starts-with(text(), 'Hello')][1]")).getText());
}

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

License:Apache License

/**
 * Runs the specified test.//from w w  w .  ja v  a  2s.  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 = "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.JQueryTestBase.java

License:Apache License

/**
 * Runs the specified test./*  w  w w . j  a  va 2 s.c o  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 = 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.MooTools121Test.java

License:Apache License

/**
 * @throws Exception if an error occurs//from w ww  .  j  ava  2s .  c o  m
 */
@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;
        }//www  .  j a  va  2s.  co m
    }
    return true;
}

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 a  va  2  s. co 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.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 a  v  a  2  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);// ww  w  .j  ava  2 s  .co m

    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));
        }
    }
}