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

License:Apache License

/**
 * @throws Exception if an error occurs//from  ww  w  .java  2 s  .  c  o m
 */
@Test
public void chunked() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/upload1", Upload1Servlet.class);
    servlets.put("/upload2", ChunkedUpload2Servlet.class);
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();
    driver.get(URL_FIRST + "upload1");
    driver.findElement(By.id("mySubmit")).click();
    assertFalse(driver.getPageSource().contains("chunked"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from  w  w w.  ja  v a  2  s .c  om*/
 */
@Test
@Alerts(CHROME = { "Host", "Connection", "Accept", "User-Agent", "Accept-Encoding", "Accept-Language" }, FF = {
        "Host", "User-Agent", "Accept", "Accept-Language", "Accept-Encoding", "Connection" }, IE = { "Accept",
                "Accept-Language", "User-Agent", "Accept-Encoding", "Host", "DNT", "Connection" })
@NotYetImplemented(IE)
public void headers() throws Exception {
    final String response = "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Content-Type: text/plain\r\n"
            + "\r\n" + "Hi";

    primitiveWebServer_ = new PrimitiveWebServer(PORT, response.getBytes());
    primitiveWebServer_.start();
    final WebDriver driver = getWebDriver();

    driver.get("http://localhost:" + PORT + "");
    final String request = primitiveWebServer_.getRequests().get(0);
    final String[] headers = request.split("\\r\\n");
    final String[] result = new String[headers.length - 1];
    for (int i = 0; i < result.length; i++) {
        final String header = headers[i + 1];
        result[i] = header.substring(0, header.indexOf(':'));
    }
    assertEquals(Arrays.asList(getExpectedAlerts()).toString(), Arrays.asList(result).toString());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/* w  w  w  .  j av a 2  s  .c  o  m*/
 */
@Test
@Alerts(CHROME = { "Host", "Connection", "Accept", "User-Agent", "Referer", "Accept-Encoding",
        "Accept-Language", "Cookie" }, FF = { "Host", "User-Agent", "Accept", "Accept-Language",
                "Accept-Encoding", "Referer", "Cookie", "Connection" }, IE = { "Accept", "Referer",
                        "Accept-Language", "User-Agent", "Accept-Encoding", "Host", "DNT", "Connection",
                        "Cookie" })
@NotYetImplemented(IE)
public void headers_cookie_referer() throws Exception {
    final String htmlResponse = "<a href='2.html'>Click me</a>";
    final String response = "HTTP/1.1 200 OK\r\n" + "Content-Length: " + htmlResponse.length() + "\r\n"
            + "Content-Type: text/html\r\n" + "Set-Cookie: name=value\r\n" + "\r\n" + htmlResponse;

    primitiveWebServer_ = new PrimitiveWebServer(PORT, response.getBytes());
    primitiveWebServer_.start();
    final WebDriver driver = getWebDriver();

    driver.get("http://localhost:" + PORT + "");
    driver.findElement(By.linkText("Click me")).click();

    final Wait<WebDriver> wait = new WebDriverWait(driver, 5);
    wait.until(currentUrlContains("2.html"));

    int index = 1;
    String request;
    do {
        request = primitiveWebServer_.getRequests().get(index++);
    } while (request.contains("/favicon.ico"));

    final String[] headers = request.split("\\r\\n");
    final String[] result = new String[headers.length - 1];
    for (int i = 0; i < result.length; i++) {
        final String header = headers[i + 1];
        result[i] = header.substring(0, header.indexOf(':'));
    }
    assertEquals(Arrays.asList(getExpectedAlerts()).toString(), Arrays.asList(result).toString());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheetTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from www. j  av a  2  s .  c om
 */
@Test
public void isDisplayed() throws Exception {
    final String html = "<!DOCTYPE html>\n" + "<head>\n" + "<style>\n" + "  .tab div {\n"
            + "    display: none;\n" + "  }\n" + "\n" + "  .tab div:target {\n" + "    display: block;\n"
            + "  }\n" + "</style></head><body>\n" + "<div class='tab'>\n" + "  <div id='anchor'>\n"
            + "    <p>Content</p>\n" + "  </div>\n" + "</div>\n" + "</body></html>";
    getMockWebConnection().setDefaultResponse(html);
    final WebDriver webDriver = loadPage2(html);
    assertFalse(webDriver.findElement(By.id("anchor")).isDisplayed());
    webDriver.get(URL_FIRST + "#anchor");
    assertTrue(webDriver.findElement(By.id("anchor")).isDisplayed());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.dom.MutationObserverTest.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from  ww w  .  j  a  va  2  s.  co m*/
 */
@Test
@Alerts("[object HTMLHeadingElement]-attributes")
public void attributeValue2() throws Exception {
    final String html = "<html><head><script>\n" + "  function makeRed() {\n"
            + "    document.getElementById('headline').setAttribute('style', 'color: red');\n" + "  }\n"

            + "  function print(mutation) {\n" + "    alert(mutation.target + '-' + mutation.type);\n" + "  }\n"

            + "  function test() {\n" + "    var mobs = new MutationObserver(function(mutations) {\n"
            + "      mutations.forEach(print)\n" + "    });\n"

            + "    mobs.observe(document.getElementById('container'), {\n" + "      attributes: true,\n"
            + "      childList: true,\n" + "      characterData: true,\n" + "      subtree: true\n"
            + "    });\n"

            + "    document.addEventListener('beforeunload', function() {\n" + "      mobs.disconnect();\n"
            + "    });\n" + "  }\n" + "</script></head><body onload='test()'>\n" + "  <div id='container'>\n"
            + "    <h1 id='headline' style='font-style: italic'>Some headline</h1>\n"
            + "    <input id='id1' type='button' onclick='makeRed()' value='Make Red'>\n" + "  </div>\n"
            + "</body></html>\n";
    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("id1")).click();
    verifyAlerts(driver, getExpectedAlerts());

    if (driver instanceof HtmlUnitDriver) {
        driver.get(URL_FIRST.toExternalForm());
        final HtmlElement element = toHtmlElement(driver.findElement(By.id("headline")));
        element.setAttribute("style", "color: red");
        verifyAlerts(driver, getExpectedAlerts());
    }
}

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   w ww  . j  av  a2  s  .  c o  m*/
 */
@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.StorageTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from ww  w  .jav  a 2  s.co  m
 */
@Test
@Alerts({ "string", "1" })
public void localStorage() throws Exception {
    final String firstHtml = "<html><head></head><body>\n" + "<script>\n" + "  if (window.localStorage) {\n"
            + "    localStorage.hello = 1;\n" + "  }\n" + "</script>\n" + "</body></html>";
    final String secondHtml = "<html><head></head><body>\n" + "<script>\n" + "  if (window.localStorage) {\n"
            + "    alert(typeof localStorage.hello);\n" + "    alert(localStorage.hello);\n" + "  }\n"
            + "</script>\n" + "</body></html>";
    loadPage2(firstHtml);
    getMockWebConnection().setResponse(URL_SECOND, secondHtml);

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

    final List<String> actualAlerts = getCollectedAlerts(driver);
    assertEquals(getExpectedAlerts(), actualAlerts);
}

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

License:Apache License

/**
 * Note that this test will work only with WebDriver instances that support starting 2 instances in parallel.
 * @throws Exception if the test fails/*w  w w .j  a  v a2  s .  c  om*/
 */
@Test
@Alerts("I was here")
@BuggyWebDriver({ CHROME, FF })
// The way ChromeDriver and FFDriver start the real browsers clears the LocalStorage somehow.
// But when executed manually the LocalStorage is shared.
@NotYetImplemented
// TODO somehow persist the LocalStorage
public void localStorageShouldBeShared() throws Exception {
    final String html1 = "<html><body><script>\n" + "try {\n" + "  localStorage.clear();\n"
            + "  localStorage.setItem('hello', 'I was here');\n" + "} catch(e) { alert('exception'); }\n"
            + "</script></body></html>";
    final WebDriver driver = loadPage2(html1);
    final List<String> alerts = getCollectedAlerts(driver);

    final String html2 = "<html><body><script>\n" + "try {\n" + "  alert(localStorage.getItem('hello'));\n"
            + "} catch(e) { alert('exception'); }\n" + "</script></body></html>";
    getMockWebConnection().setResponse(URL_FIRST, html2);

    releaseResources();
    // we have to control 2nd driver by ourself
    WebDriver driver2 = null;
    try {
        driver2 = buildWebDriver();
        driver2.get(URL_FIRST.toString());
        final List<String> newAlerts = getCollectedAlerts(driver2);
        alerts.addAll(newAlerts);
        assertEquals(getExpectedAlerts(), alerts);
    } finally {
        if (!(driver2 instanceof HtmlUnitDriver)) {
            shutDownAll();
        }
    }
}

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

License:Apache License

/**
 * Test case taken from <a href="http://angelozerr.wordpress.com/2011/07/23/websockets_jetty_step1/">here</a>.
 * @throws Exception if the test fails//  w w  w  .  jav a 2  s . c  o m
 */
@Test
public void chat() throws Exception {
    final String firstResponse = "Browser: has joined!";
    final String secondResponse = "Browser: Hope you are fine!";

    startWebServer("src/test/resources/com/gargoylesoftware/htmlunit/javascript/host", null, null,
            new ChatWebSocketHandler());
    final WebDriver driver = getWebDriver();
    driver.get(URL_FIRST + "WebSocketTest_chat.html");

    driver.findElement(By.id("username")).sendKeys("Browser");
    driver.findElement(By.id("joinB")).click();

    assertVisible("joined", driver);

    final WebElement chatE = driver.findElement(By.id("chat"));
    int counter = 0;
    do {
        Thread.sleep(100);
    } while (chatE.getText().isEmpty() && counter++ < 10);

    assertEquals(firstResponse, chatE.getText());

    driver.findElement(By.id("phrase")).sendKeys("Hope you are fine!");
    driver.findElement(By.id("sendB")).click();
    counter = 0;
    do {
        Thread.sleep(100);
    } while (!chatE.getText().contains(secondResponse) && counter++ < 10);

    assertEquals(firstResponse + "\n" + secondResponse, chatE.getText());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from www  .  ja v  a 2 s  .  c o  m
 */
@Test
@Alerts({ ": myname=My value!1", ": myname=My value!2" })
public void cookies() throws Exception {
    startWebServer("src/test/resources/com/gargoylesoftware/htmlunit/javascript/host", null, null,
            new CookiesWebSocketHandler());
    final WebDriver driver = getWebDriver();
    driver.get(URL_FIRST + "WebSocketTest_cookies.html");

    driver.findElement(By.id("username")).sendKeys("Browser");
    driver.findElement(By.id("joinB")).click();
    final WebElement chatE = driver.findElement(By.id("chat"));
    int counter = 0;
    do {
        Thread.sleep(100);
    } while (chatE.getText().isEmpty() && counter++ < 10);

    final String[] expected = getExpectedAlerts();
    assertEquals(expected[0], chatE.getText());

    driver.findElement(By.id("phrase")).sendKeys("Hope you are fine!");
    driver.findElement(By.id("sendB")).click();
    counter = 0;
    do {
        Thread.sleep(100);
    } while (!chatE.getText().contains(expected[1]) && counter++ < 10);

    assertEquals(expected[0] + "\n" + expected[1], chatE.getText());
}