Example usage for org.openqa.selenium WebDriver findElement

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

Introduction

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

Prototype

@Override
WebElement findElement(By by);

Source Link

Document

Find the first WebElement using the given method.

Usage

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

License:Apache License

/**
 * @throws Exception if the test fails/* w ww  .j  a v  a 2s .  co m*/
 */
@Test
public void linkUrlEncoding() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title>\n"
            + "  <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>\n" + "</head>\n"
            + "<body>\n" + "  <a href='bug.html?k\u00F6nig' id='myLink'>Click me</a>\n" + "</body></html>";

    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setDefaultResponse(html, "text/html", ISO_8859_1);

    final WebDriver driver = loadPage2(html);
    assertEquals(URL_FIRST.toExternalForm(), driver.getCurrentUrl());
    driver.findElement(By.id("myLink")).click();
    final String linkSuffix;
    if (getBrowserVersion().isIE()) {
        linkSuffix = "bug.html?k\u00F6nig";
    } else {
        linkSuffix = "bug.html?k%F6nig";
    }
    assertEquals(URL_FIRST.toExternalForm() + linkSuffix, driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   ww w .ja  v  a2  s. com*/
 */
@Test
public void base() throws Exception {
    final String html = "<html><head>\n" + "  <base href='" + URL_SECOND + "'>\n" + "</head><body>\n"
            + "<form action='two.html'>\n" + "  <input type='submit'>\n" + "</form></body></html>";

    getMockWebConnection().setDefaultResponse("<html><head></head><body>foo</body></html>");

    final WebDriver driver = loadPage2(html);
    driver.findElement(new ByTagName("input")).click();

    final URL requestedUrl = getMockWebConnection().getLastWebRequest().getUrl();
    final URL expectedUrl = new URL(URL_SECOND, "two.html");
    assertEquals(expectedUrl, requestedUrl);
}

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

License:Apache License

/**
 * @throws Exception if the test fails// w ww .j a  v  a  2  s  .c  o m
 */
@Test
public void emptyActionWithBase() throws Exception {
    final String html = "<html><head>\n" + "  <base href='" + URL_SECOND + "'>\n" + "</head><body>\n"
            + "<form>\n" + "  <input type='submit'>\n" + "</form></body></html>";

    getMockWebConnection().setDefaultResponse("<html><head></head><body>foo</body></html>");

    final WebDriver driver = loadPage2(html);
    driver.findElement(new ByTagName("input")).click();

    final URL requestedUrl = getMockWebConnection().getLastWebRequest().getUrl();
    assertEquals(URL_FIRST.toExternalForm(), requestedUrl);
}

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

License:Apache License

/**
 * @throws Exception if the test fails/* w w  w  . j a v  a 2 s. c o  m*/
 */
@Test
public void emptyActionWithBase2() throws Exception {
    final String html = "<html><head>\n" + "  <base href='" + URL_SECOND + "'>\n" + "</head><body>\n"
            + "<form>\n" + "  <input name='myName' value='myValue'>\n" + "  <input type='submit'>\n"
            + "</form></body></html>";

    getMockWebConnection().setDefaultResponse("<html><head></head><body>foo</body></html>");

    final WebDriver driver = loadPage2(html);
    driver.findElement(new ByTagName("input")).click();

    final URL requestedUrl = getMockWebConnection().getLastWebRequest().getUrl();
    assertEquals(URL_FIRST.toExternalForm(), requestedUrl);
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*  w w  w . jav a  2 s. c  o  m*/
 */
@Test
@Alerts({ "URL?par%F6m=Hello+G%FCnter", "par\ufffdm", "Hello G\ufffdnter" })
public void encodingSubmit() throws Exception {
    final String html = "<html>\n" + "<head>\n"
            + "  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>\n" + "</head>\n"
            + "<body>\n" + "  <form>\n" + "    <input name='par\u00F6m' value='Hello G\u00FCnter'>\n"
            + "    <input id='mySubmit' type='submit' value='Submit'>\n" + "  </form>\n" + "</body></html>";

    expandExpectedAlertsVariables(URL_FIRST);
    final WebDriver driver = loadPage2(html, URL_FIRST);
    driver.findElement(new ById("mySubmit")).click();

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

    final List<NameValuePair> requestedParams = getMockWebConnection().getLastWebRequest()
            .getRequestParameters();
    assertEquals(1, requestedParams.size());
    assertEquals(getExpectedAlerts()[1], requestedParams.get(0).getName());
    assertEquals(getExpectedAlerts()[2], requestedParams.get(0).getValue());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/* w w w.java2  s. c o  m*/
 */
@Test
@Alerts(DEFAULT = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", FF = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", IE = "text/html, application/xhtml+xml, */*")
public void acceptHeader() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head></head><body>\n"
            + "  <form action='test2'>\n" + "    <input type=submit id='mySubmit'>\n" + "  </form>\n"
            + "</body></html>";

    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/test2", AcceptHeaderServlet.class);

    final WebDriver driver = loadPage2(html, servlets);
    driver.findElement(By.id("mySubmit")).click();
    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//  ww w  . j a v  a2 s  .co  m
 */
@Test
@Alerts(DEFAULT = "gzip, deflate", CHROME = "gzip, deflate, sdch, br")
@NotYetImplemented(CHROME)
public void acceptEncodingHeader() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head></head><body>\n"
            + "  <form action='test2'>\n" + "    <input type=submit id='mySubmit'>\n" + "  </form>\n"
            + "</body></html>";

    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/test2", AcceptEncodingHeaderServlet.class);

    final WebDriver driver = loadPage2(html, servlets);
    driver.findElement(By.id("mySubmit")).click();
    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * Was failing as of HtmlUnit-2.11.//from  w w w.j  a va2s. c o m
 * @see <a href="sourceforge.net/p/htmlunit/bugs/1443/">Bug #1443</a>
 * @throws Exception if the test fails
 */
@Test
@Alerts("foo")
public void onloadInNavigatedFrame() throws Exception {
    final String html = "<html><head><title>first</title></head>\n" + "<frameset cols='20%,80%'>\n"
            + "  <frame src='frame1.html' id='frame1'>\n" + "</frameset></html>";

    final String firstHtml = "<html><body>\n" + "<a id='a1' href='frame2.html'>hello</a>\n" + "</body></html>";

    final String secondHtml = "<html><body onload='alert(\"foo\")'></body></html>";

    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setResponse(new URL(URL_FIRST, "frame1.html"), firstHtml);
    webConnection.setResponse(new URL(URL_FIRST, "frame2.html"), secondHtml);

    final WebDriver driver = loadPage2(html);
    driver.switchTo().frame(0);

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

    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from  w w  w. j  a v  a2 s . co  m*/
 */
@Test
@Alerts("[object HTMLHeadingElement]")
public void simpleScriptable() throws Exception {
    final String html = "<html><head>\n" + "<script>\n" + "  function test() {\n"
            + "    alert(document.getElementById('myId'));\n" + "  }\n" + "</script>\n"
            + "</head><body onload='test()'>\n" + "  <h2 id='myId'>asdf</h2>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlElement element = toHtmlElement(driver.findElement(By.id("myId")));
        assertTrue(element instanceof HtmlHeading2);
    }
}

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

License:Apache License

/**
 * @throws Exception if the test fails// w w w .j a  v  a  2s.c  o  m
 */
@Test
@Alerts(FF = "[object HTMLHeadingElement]", IE = "[object]")
public void simpleScriptable() throws Exception {
    final String html = "<html><head>\n" + "<script>\n" + "  function test() {\n"
            + "    alert(document.getElementById('myId'));\n" + "  }\n" + "</script>\n"
            + "</head><body onload='test()'>\n" + "  <h2 id='myId'>asdf</h2>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlElement element = toHtmlElement(driver.findElement(By.id("myId")));
        assertTrue(element instanceof HtmlHeading2);
    }
}