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.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails// w ww.  ja v a2  s . c  o m
 */
@Test
@Alerts("URL?myName=ab")
public void maxLength2() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><script>\n" + "</script></head>\n"
            + "<body>\n" + "  <form>\n" + "    <input id='myInput' name='myName' maxlength='2'>\n"
            + "    <input id='mySubmit' type='submit'>\n" + "  </form>\n" + "</body></html>";

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

    driver.findElement(By.id("mySubmit")).click();

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

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

License:Apache License

/**
 * @throws Exception if the test fails//from  ww w  . j a  v  a  2s  .  co  m
 */
@Test
@Alerts({ "3", "1", "3", "2" })
public void setSelectedIndex() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html>\n" + "<head>\n" + "  <script>\n"
            + "    function doTest() {\n" + "      alert(document.form1.select1.length);\n"
            + "      alert(document.form1.select1.selectedIndex);\n"

            + "      document.form1.select1.selectedIndex = 2;\n"
            + "      alert(document.form1.select1.length);\n"
            + "      alert(document.form1.select1.selectedIndex);\n"
            + "      document.form1.select1.selectedIndex = -1;\n" + "    }\n" + "  </script>\n" + "</head>\n"
            + "<body onload='doTest()'>\n" + "  <form name='form1' action='/foo' method='get'>\n"
            + "    <select name='select1'>\n" + "      <option value='option1' name='option1'>One</option>\n"
            + "      <option value='option2' name='option2' selected>Two</option>\n"
            + "      <option value='option3' name='option3'>Three</option>\n" + "    </select>\n"
            + "    <input type='submit' id='clickMe' name='submit' value='button'>\n" + "</form>\n"
            + "</body></html>";

    getMockWebConnection().setDefaultResponse("");

    final WebDriver webdriver = loadPageWithAlerts2(html);
    webdriver.findElement(By.id("clickMe")).click();

    assertEquals(URL_FIRST + "foo?submit=button", webdriver.getCurrentUrl());
    assertSame("method", HttpMethod.GET, getMockWebConnection().getLastMethod());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   w  w w.ja v a 2s . co m*/
 */
@Test
@Alerts(DEFAULT = { "", "foo3.html", "foo2.html" }, CHROME = { "", "foo2.html" })
public void onlick_set_location_WithHref() throws Exception {
    final String html = "<html><head></head>\n" + "<body>\n"
            + "  <a href='foo2.html' onclick='document.location = \"foo3.html\"'>click me</a>\n"
            + "</body></html>";

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

    assertEquals(getExpectedAlerts(), getMockWebConnection().getRequestedUrls(URL_FIRST));
    assertEquals(URL_FIRST + "foo2.html", driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   w  ww  .j  a v  a2s  .  co  m*/
 */
@Test
@Alerts({ "", "foo3.html" })
public void onlick_set_location_WithoutHref() throws Exception {
    final String html = "<html><head></head>\n" + "<body>\n"
            + "  <a onclick='document.location = \"foo3.html\"'>click me</a>\n" + "</body></html>";

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

    assertEquals(getExpectedAlerts(), getMockWebConnection().getRequestedUrls(URL_FIRST));
    assertEquals(URL_FIRST + "foo3.html", driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from w  w  w. j  a v a 2  s . c  o m*/
 */
@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.gargoylesoftware.htmlunit.javascript.host.Location2Test.java

License:Apache License

/**
 * @throws Exception if the test fails//  w w  w .  jav a  2  s  . com
 */
@Test
//real browsers don't show the alert, since it is quickly closed through JavaScript
@NotYetImplemented
@BuggyWebDriver(IE)
public void locationAfterOpenClosePopup() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>test</title>\n" + "  <script>\n"
            + "    function test() {\n" + "      var win = window.open('" + URL_SECOND + "','test','',true);\n"
            + "      win.close();\n" + "      location.href = 'test.html';\n" + "    }\n" + "  </script>\n"
            + "</head>\n" + "<body>\n" + "  <button id='click' onclick='test()'>Test</button>\n" + "</body>\n"
            + "</html>";
    final String popup = "<html>\n" + "<head>\n" + "  <title>popup with script</title>\n" + "  <script>\n"
            + "    alert('the root of all evil');\n" + "  </script>\n" + "</head>\n" + "<body>Popup</body>\n"
            + "</html>";
    final String target = "<html>\n" + "<head>\n" + "  <title>target</title>\n" + "</head>\n"
            + "<body>Target</body>\n" + "</html>";

    getMockWebConnection().setResponse(URL_SECOND, popup);
    getMockWebConnection().setResponse(new URL(URL_FIRST, "test.html"), target);

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("click")).click();
    try {
        assertEquals(URL_FIRST + "test.html", driver.getCurrentUrl());
    } finally {
        // TODO [IE] when run with real IE the window is closed and all following tests are broken
        releaseResources();
        shutDownAll();
    }
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from w ww  . j  ava  2  s.  c  o m
 */
@Test
public void onlick_set_location() throws Exception {
    final String html = "<html><head></head>\n" + "<body>\n"
            + "<a href='foo2.html' onclick='document.location = \"foo3.html\"'>click me</a>\n"
            + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPageWithAlerts2(html);
    driver.findElement(By.tagName("a")).click();

    final String[] expectedRequests = { "", "foo3.html", "foo2.html" };
    assertEquals(expectedRequests, getMockWebConnection().getRequestedUrls(getDefaultUrl()));

    assertEquals(new URL(getDefaultUrl(), "foo2.html").toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * As of 2.12-SNAPSHOT on 19.02.2013, a task started by setTimeout in an event handler could be executed before
 * all events handlers have been executed due to a missing synchronization.
 * @throws Exception if the test fails/*from ww w. jav  a 2 s  .co m*/
 */
@Test
public void setTimeoutShouldNotBeExecutedBeforeHandlers() throws Exception {
    final String html = "<html><body><script>\n" + "function stop() {\n" + "  window.stopIt = true;\n" + "}\n"
            + "for (var i = 0; i < 1000; i++) {\n" + "  var handler = function(e) {\n"
            + "    if (window.stopIt) {\n"
            + "      e.preventDefault ?  e.preventDefault() : e.returnValue = false;\n" + "    }\n" + "  }\n"
            + "  window.addEventListener('click', handler, false);\n" + "}\n" + "</script>\n"
            + "<form action='page2' method='post'>\n"
            + "<input id='it' type='submit' onclick='setTimeout(stop, 0)'>\n" + "</form>\n" + "</body></html>";

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

    assertEquals(URL_FIRST + "page2", driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*w w w.ja  v  a  2s  . com*/
 */
@Test
public void setLocation() throws Exception {
    final String firstContent = "<html>\n" + "<head><title>First</title></head>\n" + "<body>\n"
            + "<form name='form1'>\n" + "  <a id='link' onClick='location=\"" + URL_SECOND
            + "\";'>Click me</a>\n" + "</form>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);

    final WebDriver driver = loadPage2(firstContent);
    assertEquals("First", driver.getTitle());
    assertEquals(1, driver.getWindowHandles().size());

    driver.findElement(By.id("link")).click();
    assertEquals("Second", driver.getTitle());

    assertEquals(1, driver.getWindowHandles().size());
    assertEquals(new String[] { "", "second/" }, getMockWebConnection().getRequestedUrls(URL_FIRST));
    assertEquals(URL_SECOND.toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//  www . j  a  va  2  s.  c  o  m
 */
@Test
public void setWindowLocation() throws Exception {
    final String firstContent = "<html>\n" + "<head><title>First</title></head>\n" + "<body>\n"
            + "<form name='form1'>\n" + "  <a id='link' onClick='window.location=\"" + URL_SECOND
            + "\";'>Click me</a>\n" + "</form>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);

    final WebDriver driver = loadPage2(firstContent);
    assertEquals("First", driver.getTitle());
    assertEquals(1, driver.getWindowHandles().size());

    driver.findElement(By.id("link")).click();
    assertEquals("Second", driver.getTitle());

    assertEquals(1, driver.getWindowHandles().size());
    assertEquals(new String[] { "", "second/" }, getMockWebConnection().getRequestedUrls(URL_FIRST));
    assertEquals(URL_SECOND.toString(), driver.getCurrentUrl());
}