Example usage for org.openqa.selenium WebDriver getCurrentUrl

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

Introduction

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

Prototype

String getCurrentUrl();

Source Link

Document

Get a string representing the current URL that the browser is looking at.

Usage

From source file:com.gargoylesoftware.htmlunit.html.MalformedHtmlTest.java

License:Apache License

/**
 * @throws Exception if an error occurs//from  w  w  w .j  a v  a 2  s  . co m
 */
@Test
@Alerts({ "inFirst", "inSecond" })
public void nestedForms() throws Exception {
    final String html = "<html><body>\n" + "<form name='TransSearch'>\n" + "<input type='submit' id='button'>\n"
            + "<table>\n" + "<tr><td><input name='FromDate' value='inFirst'></form></td></tr>\n" + "</table>\n"
            + "<table>\n" + "<tr><td><form name='ImageSearch'></td></tr>\n"
            + "<tr><td><input name='FromDate' value='inSecond'></form></td></tr>\n" + "</table>\n"
            + "<script>\n" + "alert(document.forms[0].elements['FromDate'].value);\n"
            + "alert(document.forms[1].elements['FromDate'].value);\n" + "</script>\n" + "</body></html>";
    final WebDriver driver = loadPageWithAlerts2(html);
    driver.findElement(By.id("button")).click();

    assertEquals(URL_FIRST + "?FromDate=inFirst", driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from  w w w.j  a  v a2 s . co  m*/
 */
@Test
public void redirectBrokenGzip() throws Exception {
    final String html = "<html></html>";

    final List<NameValuePair> headers = Arrays.asList(new NameValuePair("Location", URL_SECOND.toString()),
            new NameValuePair("Content-Encoding", "gzip"));
    final MockWebConnection conn = getMockWebConnection();
    conn.setResponse(URL_FIRST, "12", 302, "Some error", "text/html", headers);
    conn.setResponse(URL_SECOND, html);

    final WebDriver driver = loadPageWithAlerts2(URL_FIRST);
    assertEquals(URL_SECOND.toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * An expectation for checking that the current url contains a case-sensitive substring.
 *
 * @param url the fragment of url expected
 * @return true when the url matches, false otherwise
 *//*from w  w  w.  jav  a2s.c  o  m*/
public static ExpectedCondition<Boolean> currentUrlContains(final String url) {
    return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(final WebDriver driver) {
            final String currentUrl = driver.getCurrentUrl();
            return currentUrl != null && currentUrl.contains(url);
        }
    };
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.event.Event2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*  w ww. j  a v a  2s . c  om*/
 */
@Test
public void preventDefault() throws Exception {
    final String html = "<html><head><title>First</title>\n" + "<script>\n" + "function block(e) {\n"
            + "  if (e && e.preventDefault)\n" + "    e.preventDefault();\n" + "  else\n"
            + "    return false;\n" + "}\n" + "\n" + "function test() {\n"
            + "  document.getElementById('myForm').onsubmit = block;\n" + "}\n" + "</script>\n"
            + "</head><body onload='test()'>\n" + "<form id='myForm' action='doesnt_exist.html'>\n"
            + "  <input type='submit' id='mySubmit' value='Continue'></p>\n" + "</form>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    driver.findElement(By.id("mySubmit"));
    assertEquals(URL_FIRST.toExternalForm(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from  w  w  w .ja va  2s .  c  o  m
 */
@Test
public void preventDefault() throws Exception {
    final String html = "<html><head><title>First</title>\n" + "<script>\n" + "function block(e) {\n"
            + "  if (e && e.preventDefault)\n" + "    e.preventDefault();\n" + "  else\n"
            + "    return false;\n" + "}\n" + "\n" + "function test() {\n"
            + "  document.getElementById('myForm').onsubmit = block;\n" + "}\n" + "</script>\n"
            + "</head><body onload='test()'>\n" + "<form id='myForm' action='doesnt_exist.html'>\n"
            + "  <input type='submit' id='mySubmit' value='Continue'></p>\n" + "</form>" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    driver.findElement(By.id("mySubmit"));
    assertEquals(getDefaultUrl().toExternalForm(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from  ww  w  . j a va  2 s. c o  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//from ww w  .  j av  a  2 s . c  o m
 */
@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//w ww.ja  v  a2  s .com
 */
@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  ww . j ava 2s.com
 */
@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/*from   w w  w.j  a v  a  2 s  .  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();
}