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.javascript.host.History2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from   w w  w.jav  a 2s.co m*/
 */
@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.html.HTMLDocumentTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*ww w  . jav a 2  s .c o m*/
 */
@Test
@Alerts(DEFAULT = "?%C3%A8=%C3%A8", IE = "?\u00E8=\u00E8")
public void encoding7() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "<meta charset='UTF-8'>\n" + "</head><body>\n"
            + "  <a id='myId' href='test?\u00E8=\u00E8'>test</a>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html, URL_FIRST, "text/html", UTF_8);
    driver.findElement(By.id("myId")).click();
    String actualQuery = driver.getCurrentUrl();
    actualQuery = actualQuery.substring(actualQuery.indexOf('?'));
    assertTrue(actualQuery.endsWith(getExpectedAlerts()[0]));
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLFormElementTest.java

License:Apache License

private void changeFormActionAfterSubmit(final String clickable, final String expectedFile) throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <script type='text/javascript'>\n"
            + "    function submitForm() {\n" + "      document.myForm.submit();\n"
            + "      document.myForm.action = 'page2.html';\n" + "    }\n" + "  </script>\n" + "</head>\n"
            + "<body>\n" + "  <form action='page1.html' name='myForm'>\n" + "    <" + clickable
            + " id='x' onclick='submitForm();'>foo\n" + "  </form>\n" + "</body>\n" + "</html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPageWithAlerts2(html);
    driver.findElement(By.id("x")).click();
    // caution: IE7 doesn't put a trailing "?"
    assertEquals(URL_FIRST + expectedFile, driver.getCurrentUrl().replaceAll("\\?", ""));
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLFormElementTest.java

License:Apache License

private void changesAfterCallToSubmit(final String id, final String expectedUrlSuffix) throws Exception {
    final String html = "<html><head><script>\n" + "function submitForm() {\n"
            + "  var f = document.forms[0];\n" + "  f.action = 'page3.html';\n" + "\n"
            + "  var h = document.createElement('input');\n" + "  h.name = 'f1';\n" + "  h.value = 'v1';\n"
            + "  f.appendChild(h);\n" + "\n" + "  f.submit();\n" + "\n" + "  f.action = 'page4.html';\n"
            + "  var h = document.createElement('input');\n" + "  h.name = 'f2';\n" + "  h.value = 'v2';\n"
            + "  f.appendChild(h);\n" + "  return false;\n" + "}\n" + "</script></head><body>\n"
            + "<form action='page1.html' name='myForm'>\n"
            + "  <input type='submit' id='inputSubmitReturnTrue' value='With on click on the button, return true' "
            + "onclick='submitForm(); return true'>\n"
            + "  <input type='submit' id='inputSubmitReturnFalse' value='With on click on the button, return false' "
            + "onclick='submitForm(); return false'>\n"
            + "  <input type='submit' id='inputSubmitVoid' value='With on click on the button, no return' "
            + "onclick='submitForm();'>\n"
            + "  <a id='link' href='#'  onclick='return submitForm()'><input type='submit' "
            + "value='With on click on the link'></a>\n" + "</form></body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver wd = loadPageWithAlerts2(html);
    wd.findElement(By.id(id)).click();//from ww  w  .j a v a  2  s  .  c o m
    assertEquals(URL_FIRST + expectedUrlSuffix, wd.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from  w w  w  .j a  v a2s  .  c  o  m
 */
@Test
public void inputValue() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title><script>\n"
            + "function doTest() {\n" + "  document.form1.textfield1.value = 'blue';\n" + "}\n"
            + "</script></head>\n" + "<body>\n" + "<p>hello world</p>\n"
            + "<form name='form1' onsubmit='doTest()'>\n"
            + "  <input type='text' name='textfield1' id='textfield1' value='foo' />\n"
            + "  <input type='submit' id='clickMe'/>\n" + "</form>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("clickMe")).click();
    assertEquals(URL_FIRST + "?textfield1=blue", driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*from  w  w w.j av a2  s .  c  om*/
 */
@Test
public void inputSelect_NotDefinedAsPropertyAndFunction() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title><script>\n"
            + "function doTest() {\n" + "  document.form1.textfield1.select();\n" + "}\n" + "</script></head>\n"
            + "<body>\n" + "<p>hello world</p>\n" + "<form name='form1' onsubmit='doTest()'>\n"
            + "  <input type='text' name='textfield1' id='textfield1' value='foo' />\n"
            + "  <input type='submit' id='clickMe'/>\n" + "</form>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("clickMe")).click();
    assertEquals(URL_FIRST + "?textfield1=foo", driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*from w w w .  j a va  2  s . co m*/
 */
@Test
public void inputNameChange() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title><script>\n"
            + "function doTest() {\n" + "  document.form1.textfield1.name = 'changed';\n" + "}\n"
            + "</script></head><body>\n" + "<p>hello world</p>\n" + "<form name='form1' onsubmit='doTest()'>\n"
            + "  <input type='text' name='textfield1' id='textfield1' value='foo' />\n"
            + "  <input type='submit' name='button1' id='clickMe' value='pushme' />\n" + "</form>\n"
            + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("clickMe")).click();

    assertEquals(URL_FIRST + "?changed=foo&button1=pushme", driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from   w w w .j  ava  2s.  c  o  m
 */
@Test
@Alerts("onsubmit")
public void submitNonRequired() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><script>\n"
            + "function submitMe() {\n" + "  alert('onsubmit');\n" + "}\n" + "</script></head>\n" + "<body>\n"
            + "  <form onsubmit='submitMe()'>\n" + "    <input id='myInput' name='myName' value=''>\n"
            + "    <input id='mySubmit' type='submit'>\n" + "  </form>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("mySubmit")).click();
    verifyAlerts(driver, getExpectedAlerts());
    Thread.sleep(10);
    assertTrue(driver.getCurrentUrl().contains("myName"));

    // because we have a new page
    assertTrue(getCollectedAlerts(driver, 1).isEmpty());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from   ww  w  .  j a  v a  2 s .  c o m
 */
@Test
@Alerts({ "1", "URL" })
public void submitRequired() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><script>\n"
            + "function submitMe() {\n" + "  alert('onsubmit');\n" + "}\n" + "</script></head>\n" + "<body>\n"
            + "  <form onsubmit='submitMe()'>\n" + "    <input id='myInput' name='myName' value='' required>\n"
            + "    <input id='mySubmit' type='submit'>\n" + "  </form>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("mySubmit")).click();

    expandExpectedAlertsVariables(URL_FIRST);
    assertEquals(getExpectedAlerts()[0], Integer.toString(getMockWebConnection().getRequestCount()));
    assertEquals(getExpectedAlerts()[1], driver.getCurrentUrl());
    assertTrue(getCollectedAlerts(driver).isEmpty());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from w  ww.  ja  va  2 s.  c  o m
 */
@Test
@Alerts(DEFAULT = "URL?myName=abcdefg", IE = "URL")
@NotYetImplemented(IE)
public void maxLengthJavaScript() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><script>\n"
            + "function updateValue() {\n" + "  document.getElementById('myInput').value = 'abcdefg';\n" + "}\n"
            + "</script></head>\n" + "<body>\n" + "  <form>\n"
            + "    <input id='myInput' name='myName' maxlength='2'>\n"
            + "    <input id='mySubmit' type='submit'>\n" + "  </form>\n"
            + "  <button id='myButton' onclick='updateValue()'>Update Value</button>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("myButton")).click();
    assertEquals("abcdefg", driver.findElement(By.id("myInput")).getAttribute("value"));
    driver.findElement(By.id("mySubmit")).click();

    expandExpectedAlertsVariables(URL_FIRST);
    assertEquals(getExpectedAlerts()[0], driver.getCurrentUrl());
}