Example usage for org.openqa.selenium WebDriver navigate

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

Introduction

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

Prototype

Navigation navigate();

Source Link

Document

An abstraction allowing the driver to access the browser's history and to navigate to a given URL.

Usage

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

License:Apache License

/**
 * Tests going in history {@link History#back()} with {@code POST} request.
 *
 * @throws Exception if an error occurs//from w  w  w. j a v a2s  .co  m
 */
@Test
@Alerts("49")
// limit varies for IE
public void historyCache() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    final int testDeep = 70;

    for (int i = 0; i < testDeep; i++) {
        servlets.put("/post" + i, Post1Servlet.class);
    }
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();

    int count = Post1Servlet.Count_;
    for (int i = 0; i < testDeep; i++) {
        driver.get(URL_FIRST + "post" + i);
        assertTrue(driver.getPageSource(), driver.getPageSource().contains("Call: " + (i + count)));
    }

    count = Post1Servlet.Count_;
    for (int i = 0; i < testDeep - 1; i++) {
        driver.navigate().back();
        if (!driver.getPageSource().contains("Call: " + (count - i - 2))) {
            assertEquals(Integer.parseInt(getExpectedAlerts()[0]), i);
            return;
        }

        if (count != Post1Servlet.Count_) {
            Assert.fail("Server called for " + i);
            break;
        }
    }

    assertEquals(getExpectedAlerts()[0], "done");
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.History2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs//from  www . j ava2s.co  m
 */
@Test
@Alerts({ "[object PopStateEvent]", "null" })
public void pushStateSimple() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "<title></title>\n" + "<script>\n" + "  function test() {\n"
            + "    if (!window.history.pushState) { alert('no pushState'); return }\n"
            + "    var stateObj = { hi: 'there' };\n"
            + "    window.history.pushState(stateObj, 'page 2', 'bar.html');\n" + "  }\n"

            + "  function popMe(event) {\n" + "    var e = event ? event : window.event;\n" + "    alert(e);\n"
            + "    alert(e.state);\n" + "  }\n" + "</script>\n" + "</head>\n"
            + "<body onpopstate='popMe(event)'>\n" + "  <button id=myId onclick='test()'>Click me</button>\n"
            + "</body></html>";

    final String[] expectedAlerts = getExpectedAlerts();
    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("myId")).click();

    if (expectedAlerts.length > 1) {
        assertEquals(URL_FIRST + "bar.html", driver.getCurrentUrl());
        driver.navigate().back();
    }
    verifyAlerts(driver, expectedAlerts);
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.History2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*ww w  .  j av a 2s. c  om*/
 */
@Test
@Alerts({ "[object PopStateEvent]", "{\"hi\":\"there\"}", "[object PopStateEvent]", "{\"hi\":\"there\"}",
        "[object PopStateEvent]", "null", "[object PopStateEvent]", "null", "[object PopStateEvent]",
        "{\"hi\":\"there\"}", "[object PopStateEvent]", "{\"hi\":\"there\"}", "[object PopStateEvent]",
        "{\"hi2\":\"there2\"}", "[object PopStateEvent]", "{\"hi2\":\"there2\"}" })
public void pushState() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "<script>\n" + "  function test() {\n"
            + "    if (window.history.pushState) {\n" + "      var stateObj = { hi: 'there' };\n"
            + "      window.history.pushState(stateObj, 'page 2', 'bar.html');\n" + "    }\n" + "  }\n"

            + "  function test2() {\n" + "    if (window.history.pushState) {\n"
            + "      var stateObj = { hi2: 'there2' };\n"
            + "      window.history.pushState(stateObj, 'page 3', 'bar2.html');\n" + "    }\n" + "  }\n"

            + "  function popMe(event) {\n" + "    var e = event ? event : window.event;\n" + "    alert(e);\n"
            + "    alert(JSON.stringify(e.state));\n" + "  }\n"

            + "  function setWindowName() {\n" + "    window.name = window.name + 'a';\n" + "  }\n"

            + "  window.addEventListener('popstate', popMe);\n" + "</script>\n" + "</head>\n"
            + "<body onpopstate='popMe(event)' onload='setWindowName()' onbeforeunload='setWindowName()' "
            + "onunload='setWindowName()'>\n" + "  <button id=myId onclick='test()'>Click me</button>\n"
            + "  <button id=myId2 onclick='test2()'>Click me</button>\n" + "</body></html>";

    final String[] expectedAlerts = getExpectedAlerts();
    int i = 0;

    final WebDriver driver = loadPage2(html);
    assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));

    final long start = (Long) ((JavascriptExecutor) driver).executeScript("return window.history.length");

    driver.findElement(By.id("myId")).click();
    assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
    assertEquals(start + 1, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
    assertEquals(URL_FIRST + "bar.html", driver.getCurrentUrl());

    driver.findElement(By.id("myId2")).click();
    assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
    assertEquals(start + 2, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
    assertEquals(URL_FIRST + "bar2.html", driver.getCurrentUrl());

    driver.navigate().back();
    verifyAlerts(driver, expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++]);
    assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
    assertEquals(start + 2, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
    assertEquals(URL_FIRST + "bar.html", driver.getCurrentUrl());

    driver.navigate().back();
    verifyAlerts(driver, expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++]);
    assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
    assertEquals(start + 2, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
    assertEquals(URL_FIRST.toString(), driver.getCurrentUrl());

    driver.navigate().forward();
    verifyAlerts(driver, expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++]);
    assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
    assertEquals(start + 2, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
    assertEquals(URL_FIRST + "bar.html", driver.getCurrentUrl());

    driver.navigate().forward();
    verifyAlerts(driver, expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++]);
    assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
    assertEquals(start + 2, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
    assertEquals(URL_FIRST + "bar2.html", driver.getCurrentUrl());

    assertEquals(1, getMockWebConnection().getRequestCount());

    // because we have changed the window name
    releaseResources();
    shutDownAll();
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.History2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs//from   w w w.jav a 2  s  .c o m
 */
@Test
@Alerts(CHROME = { "[object PopStateEvent]", "{\"hi\":\"there\"}", "false", "[object PopStateEvent]",
        "{\"hi\":\"there\"}", "false", "[object PopStateEvent]", "null", "true", "[object PopStateEvent]",
        "null", "true", "[object PopStateEvent]", "{\"hi\":\"there\"}", "false", "[object PopStateEvent]",
        "{\"hi\":\"there\"}", "false", "[object PopStateEvent]", "{\"hi2\":\"there2\"}", "false",
        "[object PopStateEvent]", "{\"hi2\":\"there2\"}", "false" }, FF = { "[object PopStateEvent]",
                "{\"hi\":\"there\"}", "true", "[object PopStateEvent]", "{\"hi\":\"there\"}", "true",
                "[object PopStateEvent]", "null", "true", "[object PopStateEvent]", "null", "true",
                "[object PopStateEvent]", "{\"hi\":\"there\"}", "true", "[object PopStateEvent]",
                "{\"hi\":\"there\"}", "true", "[object PopStateEvent]", "{\"hi2\":\"there2\"}", "true",
                "[object PopStateEvent]", "{\"hi2\":\"there2\"}", "true" })
public void pushStateClone() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "<script>\n" + "  function test() {\n"
            + "    if (window.history.pushState) {\n" + "      var stateObj = { hi: 'there' };\n"
            + "      window.history.pushState(stateObj, 'page 2', 'bar.html');\n" + "    }\n" + "  }\n"

            + "  function test2() {\n" + "    if (window.history.pushState) {\n"
            + "      var stateObj = { hi2: 'there2' };\n"
            + "      window.history.pushState(stateObj, 'page 3', 'bar2.html');\n" + "    }\n" + "  }\n"

            + "  function popMe(event) {\n" + "    var e = event ? event : window.event;\n" + "    alert(e);\n"
            + "    alert(JSON.stringify(e.state));\n" + "    alert(e.state == history.state);\n" + "  }\n"

            + "  function setWindowName() {\n" + "    window.name = window.name + 'a';\n" + "  }\n"

            + "  window.addEventListener('popstate', popMe);\n" + "</script>\n" + "</head>\n"
            + "<body onpopstate='popMe(event)' onload='setWindowName()' onbeforeunload='setWindowName()' "
            + "onunload='setWindowName()'>\n" + "  <button id=myId onclick='test()'>Click me</button>\n"
            + "  <button id=myId2 onclick='test2()'>Click me</button>\n" + "</body></html>";

    final String[] expectedAlerts = getExpectedAlerts();
    int i = 0;
    final WebDriver driver = loadPage2(html);
    assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));

    final long start = (Long) ((JavascriptExecutor) driver).executeScript("return window.history.length");

    if (expectedAlerts.length != 0) {
        driver.findElement(By.id("myId")).click();
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 1, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST + "bar.html", driver.getCurrentUrl());

        driver.findElement(By.id("myId2")).click();
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 2, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST + "bar2.html", driver.getCurrentUrl());

        driver.navigate().back();
        verifyAlerts(driver, expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++],
                expectedAlerts[i++], expectedAlerts[i++]);
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 2, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST + "bar.html", driver.getCurrentUrl());

        driver.navigate().back();
        verifyAlerts(driver, expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++],
                expectedAlerts[i++], expectedAlerts[i++]);
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 2, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST.toString(), driver.getCurrentUrl());

        driver.navigate().forward();
        verifyAlerts(driver, expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++],
                expectedAlerts[i++], expectedAlerts[i++]);
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 2, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST + "bar.html", driver.getCurrentUrl());

        driver.navigate().forward();
        verifyAlerts(driver, expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++],
                expectedAlerts[i++], expectedAlerts[i++]);
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 2, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST + "bar2.html", driver.getCurrentUrl());
    }

    assertEquals(1, getMockWebConnection().getRequestCount());

    // because we have changed the window name
    releaseResources();
    shutDownAll();
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.History2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs//from   w w w.j  av  a  2s  .  c  o  m
 */
@Test
@Alerts({ "true", "true" })
public void pushStateLocationHref() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "<script>\n" + "  function test() {\n"
            + "    if (!window.history.pushState) { alert('no pushState'); return }\n" + "    try {\n"
            + "      var stateObj = { hi: 'there' };\n"
            + "      window.history.pushState(stateObj, 'page 2', 'bar.html');\n"
            + "      alert(location.href.indexOf('bar.html') > -1);\n"
            + "    } catch(e) { alert('exception'); }\n" + "  }\n"

            + "  function test2() {\n"
            + "    if (!window.history.pushState) { alert('no pushState'); return }\n" + "    try {\n"
            + "      var stateObj = { hi2: 'there2' };\n"
            + "      window.history.pushState(stateObj, 'page 3', 'bar2.html');\n"
            + "      alert(location.href.indexOf('bar2.html') > -1);\n"
            + "    } catch(e) { alert('exception'); }\n" + "  }\n"

            + "</script>\n" + "</head>\n" + "<body>\n"
            + "  <button id=myId onclick='test()'>Click me</button>\n"
            + "  <button id=myId2 onclick='test2()'>Click me</button>\n" + "</body></html>";

    final String[] expectedAlerts = getExpectedAlerts();
    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("myId")).click();
    verifyAlerts(driver, expectedAlerts[0]);

    assertEquals(URL_FIRST + "bar.html", driver.getCurrentUrl());
    assertEquals(URL_FIRST + "bar.html", ((JavascriptExecutor) driver).executeScript("return location.href"));
    driver.findElement(By.id("myId2")).click();
    verifyAlerts(driver, expectedAlerts[1]);

    assertEquals(URL_FIRST + "bar2.html", driver.getCurrentUrl());
    assertEquals(URL_FIRST + "bar2.html", ((JavascriptExecutor) driver).executeScript("return location.href"));
    driver.navigate().back();
    assertEquals(URL_FIRST + "bar.html", driver.getCurrentUrl());
    driver.navigate().back();
    assertEquals(URL_FIRST.toString(), driver.getCurrentUrl());
    driver.navigate().forward();
    assertEquals(URL_FIRST + "bar.html", driver.getCurrentUrl());
    driver.navigate().forward();
    assertEquals(URL_FIRST + "bar2.html", driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.History2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs//  w w  w . j  a  v  a2s . c om
 */
@Test
@Alerts({ "[object PopStateEvent]", "null", "[object PopStateEvent]", "null", "[object PopStateEvent]",
        "{\"hi2\":\"there2\"}", "[object PopStateEvent]", "{\"hi2\":\"there2\"}" })
public void replaceState() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "<script>\n" + "  function test() {\n"
            + "    if (window.history.pushState) {\n" + "      var stateObj = { hi: 'there' };\n"
            + "      window.history.pushState(stateObj, 'page 2', 'bar.html');\n" + "    }\n" + "  }\n"

            + "  function test2() {\n" + "    if (window.history.replaceState) {\n"
            + "      var stateObj = { hi2: 'there2' };\n"
            + "      window.history.replaceState(stateObj, 'page 3', 'bar2.html');\n" + "    }\n" + "  }\n"

            + "  function popMe(event) {\n" + "    var e = event ? event : window.event;\n" + "    alert(e);\n"
            + "    alert(JSON.stringify(e.state));\n" + "  }\n"

            + "  function setWindowName() {\n" + "    window.name = window.name + 'a';\n" + "  }\n"

            + "  window.addEventListener('popstate', popMe);\n" + "</script>\n" + "</head>\n"
            + "<body onpopstate='popMe(event)' onload='setWindowName()' onbeforeunload='setWindowName()' "
            + "onunload='setWindowName()'>\n" + "  <button id='myId' onclick='test()'>Click me</button>\n"
            + "  <button id='myId2' onclick='test2()'>Click me</button>\n" + "</body></html>";

    final String[] expectedAlerts = getExpectedAlerts();
    int i = 0;
    final WebDriver driver = loadPage2(html);

    assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
    final long start = (Long) ((JavascriptExecutor) driver).executeScript("return window.history.length");

    if (expectedAlerts.length != 0) {
        driver.findElement(By.id("myId")).click();
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 1, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST + "bar.html", driver.getCurrentUrl());

        driver.findElement(By.id("myId2")).click();
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 1, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST + "bar2.html", driver.getCurrentUrl());

        driver.navigate().back();
        verifyAlerts(driver, expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++],
                expectedAlerts[i++]);
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 1, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST.toString(), driver.getCurrentUrl());

        driver.navigate().forward();
        verifyAlerts(driver, expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++],
                expectedAlerts[i++]);
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 1, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST + "bar2.html", driver.getCurrentUrl());
    }

    assertEquals(1, getMockWebConnection().getRequestCount());

    // because we have changed the window name
    releaseResources();
    shutDownAll();
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.History2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs//w  w w  . jav a  2  s  .  c om
 */
@Test
@Alerts(CHROME = { "[object PopStateEvent]", "null", "true", "[object PopStateEvent]", "null", "true",
        "[object PopStateEvent]", "{\"hi2\":\"there2\"}", "false", "[object PopStateEvent]",
        "{\"hi2\":\"there2\"}", "false" }, FF = { "[object PopStateEvent]", "null", "true",
                "[object PopStateEvent]", "null", "true", "[object PopStateEvent]", "{\"hi2\":\"there2\"}",
                "true", "[object PopStateEvent]", "{\"hi2\":\"there2\"}", "true" })
public void replaceStateClone() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "<script>\n" + "  function test() {\n"
            + "    if (window.history.pushState) {\n" + "      var stateObj = { hi: 'there' };\n"
            + "      window.history.pushState(stateObj, 'page 2', 'bar.html');\n" + "    }\n" + "  }\n"

            + "  function test2() {\n" + "    if (window.history.replaceState) {\n"
            + "      var stateObj = { hi2: 'there2' };\n"
            + "      window.history.replaceState(stateObj, 'page 3', 'bar2.html');\n" + "    }\n" + "  }\n"

            + "  function popMe(event) {\n" + "    var e = event ? event : window.event;\n" + "    alert(e);\n"
            + "    alert(JSON.stringify(e.state));\n" + "    alert(e.state == history.state);\n" + "  }\n"

            + "  function setWindowName() {\n" + "    window.name = window.name + 'a';\n" + "  }\n"

            + "  window.addEventListener('popstate', popMe);\n" + "</script>\n" + "</head>\n"
            + "<body onpopstate='popMe(event)' onload='setWindowName()' onbeforeunload='setWindowName()' "
            + "onunload='setWindowName()'>\n" + "  <button id=myId onclick='test()'>Click me</button>\n"
            + "  <button id=myId2 onclick='test2()'>Click me</button>\n" + "</body></html>";

    final String[] expectedAlerts = getExpectedAlerts();
    int i = 0;
    final WebDriver driver = loadPage2(html);

    assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
    final long start = (Long) ((JavascriptExecutor) driver).executeScript("return window.history.length");

    if (expectedAlerts.length != 0) {
        driver.findElement(By.id("myId")).click();
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 1, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST + "bar.html", driver.getCurrentUrl());

        driver.findElement(By.id("myId2")).click();
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 1, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST + "bar2.html", driver.getCurrentUrl());

        driver.navigate().back();
        verifyAlerts(driver, expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++],
                expectedAlerts[i++], expectedAlerts[i++]);
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 1, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST.toString(), driver.getCurrentUrl());

        driver.navigate().forward();
        verifyAlerts(driver, expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++], expectedAlerts[i++],
                expectedAlerts[i++], expectedAlerts[i++]);
        assertEquals("a", ((JavascriptExecutor) driver).executeScript("return window.name"));
        assertEquals(start + 1, ((JavascriptExecutor) driver).executeScript("return window.history.length"));
        assertEquals(URL_FIRST + "bar2.html", driver.getCurrentUrl());
    }

    assertEquals(1, getMockWebConnection().getRequestCount());

    // because we have changed the window name
    releaseResources();
    shutDownAll();
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.Location2Test.java

License:Apache License

/**
 * Test for <tt>replace</tt>.
 * @throws Exception if the test fails/*from  ww  w.  j  a va2s  .c  om*/
 */
@Test
public void replaceLastInHistory() throws Exception {
    final String startContent = "<html><head><title>First Page</title></head><body></body></html>";

    final String secondContent = "<html><head><title>Second Page</title><script>\n" + "function doTest() {\n"
            + "  location.replace('" + URL_THIRD + "');\n" + "}\n"
            + "</script></head><body onload='doTest()'>\n" + "</body></html>";

    final String thirdContent = "<html><head><title>Third Page</title></head><body></body></html>";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);
    getMockWebConnection().setResponse(URL_THIRD, thirdContent);

    final WebDriver webdriver = loadPageWithAlerts2(startContent);
    webdriver.get(URL_SECOND.toExternalForm());

    assertEquals("Third Page", webdriver.getTitle());

    // navigate back
    webdriver.navigate().back();
    assertEquals("First Page", webdriver.getTitle());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.Location2Test.java

License:Apache License

/**
 * @throws Exception if the test fails/*from  www .  java  2s.  c om*/
 */
@Test
@Alerts("onhashchange #/foo")
public void getNextPageWithOnlyHashChangeShouldTriggerHashChangeEvent() throws Exception {
    final String html = "<html><body><script>\n" + " window.onhashchange = function(event) {\n"
            + "    alert('onhashchange ' + window.location.hash);\n" + " }\n" + "</script></body></html>";

    final WebDriver driver = loadPage2(html);
    driver.navigate().to(driver.getCurrentUrl() + "#/foo");

    verifyAlerts(driver, getExpectedAlerts());
}

From source file:com.google.appengine.tck.login.UserLogin.java

License:Open Source License

public void login(@Observes EventContext<Before> event) throws Exception {
    Before before = event.getEvent();//from  w ww  .ja  v  a  2 s .c o m

    UserIsLoggedIn userIsLoggedIn = null;
    if (before.getTestMethod().isAnnotationPresent(UserIsLoggedIn.class)) {
        userIsLoggedIn = before.getTestMethod().getAnnotation(UserIsLoggedIn.class);
    } else if (before.getTestClass().isAnnotationPresent(UserIsLoggedIn.class)) {
        userIsLoggedIn = before.getTestClass().getAnnotation(UserIsLoggedIn.class);
    }

    if (userIsLoggedIn != null) {
        final URI baseUri = getBaseURI(before.getTestMethod());
        final WebDriver driver = createWebDriver();
        try {
            driver.manage().deleteAllCookies();

            driver.navigate().to(baseUri + USER_LOGIN_SERVLET_PATH + "?location="
                    + URLEncoder.encode(userIsLoggedIn.location(), "UTF-8"));

            // did we navigate to this requested page, or did we get redirected/forwarded to login already
            List<WebElement> loginUrlElts = driver.findElements(By.id("login-url"));
            if (loginUrlElts.size() > 0) {
                String loginURL = loginUrlElts.get(0).getText();

                // check
                if (isInternalLink(loginURL)) {
                    loginURL = baseUri + loginURL;
                }

                // go-to login page
                driver.navigate().to(loginURL);
            }

            // find custom login handler, if exists
            LoginHandler loginHandler = TestBase.instance(getClass(), LoginHandler.class);
            if (loginHandler == null) {
                loginHandler = new DefaultLoginHandler();
            }
            loginHandler.login(driver, new UserLoginContext(userIsLoggedIn));
            // copy cookies
            Set<Cookie> cookies = driver.manage().getCookies();
            for (Cookie cookie : cookies) {
                ModulesApi.addCookie(cookie.getName(), cookie.getValue());
            }
        } finally {
            driver.close();
        }
    }

    event.proceed();
}