Example usage for org.openqa.selenium WebDriver get

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

Introduction

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

Prototype

void get(String url);

Source Link

Document

Load a new web page in the current browser window.

Usage

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 ava  2s  . 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  ww .ja  va2  s .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.PrototypeTestBase.java

License:Apache License

/**
 * Runs the specified test./*from  w w  w.  j  a v a2s.co  m*/
 * @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

/**
 * @throws Exception if an error occurs//from   w  w w  .  j  av  a 2 s .  com
 */
@Test
@Alerts(DEFAULT = { "++++++++++++++++++", "+", "+", "+", "+++", "++", "++++F+++" }, CHROME = {
        "+++++++++++FF+++++", "+", "+", "+", "F++", "++",
        "++++F+++" }, IE = { "+++++++++++F++++++", "+", "+", "+", "FFF", "FF", "FFFFFFFF" })
// TODO [IE]XML sarissa 0.9.9.3 is not compatible with IE's new XML stuff
public void sarissa() throws Exception {
    startWebServer("src/test/resources/libraries/sarissa/0.9.9.3", null, null);
    final String url = URL_FIRST + "test/testsarissa.html";

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

    driver.findElement(By.xpath("//button")).click();

    driver.switchTo().alert().dismiss();

    verify(driver, "SarissaTestCase", getExpectedAlerts()[0]);
    verify(driver, "XmlHttpRequestTestCase", getExpectedAlerts()[1]);
    verify(driver, "XMLSerializerTestCase", getExpectedAlerts()[2]);
    verify(driver, "DOMParserTestCase", getExpectedAlerts()[3]);
    verify(driver, "XMLDocumentTestCase", getExpectedAlerts()[4]);
    verify(driver, "XMLElementTestCase", getExpectedAlerts()[5]);
    verify(driver, "XSLTProcessorTestCase", getExpectedAlerts()[6]);
}

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  .ja va2 s  .  c o  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 a2  s .  com*/

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

License:Apache License

/**
 * Returns the {@code WebDriver} after loading the URL with the specified relative path.
 * @param relativePath the relative path
 * @return the {@code WebDriver}//from w  w  w. j a  v  a2 s. c o m
 */
protected WebDriver getWebDriver(final String relativePath) {
    final WebDriver driver = getWebDriver();
    driver.get("http://localhost:" + PORT + relativePath);
    return driver;
}

From source file:com.gargoylesoftware.htmlunit.WebDriverTestCase.java

License:Apache License

/**
 * Same as {@link #loadPageWithAlerts2(String)}... but doesn't verify the alerts.
 * @param html the HTML to use//  w  w  w .j  a  va 2s.c  o m
 * @param url the url to use to load the page
 * @param contentType the content type to return
 * @param charset the charset
 * @return the web driver
 * @throws Exception if something goes wrong
 */
protected final WebDriver loadPage2(String html, final URL url, final String contentType, final Charset charset)
        throws Exception {
    if (useStandards_ != null) {
        if (html.startsWith(HtmlPageTest.STANDARDS_MODE_PREFIX_)) {
            fail("HTML must not be prefixed with Standards Mode.");
        }
        if (useStandards_) {
            html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + html;
        }
    }
    final MockWebConnection mockWebConnection = getMockWebConnection();
    mockWebConnection.setResponse(url, html, contentType, charset);
    startWebServer(mockWebConnection);

    WebDriver driver = getWebDriver();
    if (!(driver instanceof HtmlUnitDriver)) {
        try {
            driver.manage().window().setSize(new Dimension(1272, 768));
        } catch (final NoSuchSessionException e) {
            // maybe the driver was killed by the test before; setup a new one
            shutDownRealBrowsers();

            driver = getWebDriver();
            driver.manage().window().setSize(new Dimension(1272, 768));
        }
    }
    driver.get(url.toExternalForm());

    return driver;
}

From source file:com.gargoylesoftware.htmlunit.WebDriverTestCase.java

License:Apache License

/**
 * Same as {@link #loadPage2(String, URL)}, but with additional servlet configuration.
 * @param html the HTML to use for the default page
 * @param url the URL to use to load the page
 * @param servlets the additional servlets to configure with their mapping
 * @return the web driver//from w ww .  j a v  a2s.  co m
 * @throws Exception if something goes wrong
 */
protected final WebDriver loadPage2(final String html, final URL url,
        final Map<String, Class<? extends Servlet>> servlets) throws Exception {

    servlets.put("/*", MockWebConnectionServlet.class);
    getMockWebConnection().setResponse(url, html);
    MockWebConnectionServlet.MockConnection_ = getMockWebConnection();

    startWebServer("./", null, servlets);

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

    return driver;
}

From source file:com.gargoylesoftware.htmlunit.WebDriverTestCase.java

License:Apache License

/**
 * Same as {@link #loadPageWithAlerts2(URL)}, but using with timeout.
 * @param url the URL to use to load the page
 * @param maxWaitTime the maximum time to wait to get the alerts (in millis)
 * @return the web driver//from   w  w  w .ja v  a 2  s  .com
 * @throws Exception if something goes wrong
 */
protected final WebDriver loadPageWithAlerts2(final URL url, final long maxWaitTime) throws Exception {
    expandExpectedAlertsVariables(url);
    final String[] expectedAlerts = getExpectedAlerts();

    startWebServer(getMockWebConnection());

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

    verifyAlerts(maxWaitTime, driver, expectedAlerts);
    return driver;
}