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.BrowserVersion2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from w w  w . j  a v a  2  s  . co m*/
 */
@Test
@Alerts(DEFAULT = { "2", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" }, CHROME = {
        "2", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" }, IE = { "2",
                "Accept: text/html, application/xhtml+xml, */*" })
public void acceptHeaderAnchorClick() throws Exception {
    String html = "<html><body>Response</body></html>";
    getMockWebConnection().setDefaultResponse(html);

    html = "<html><head><title>First</title></head>\n" + "<body>\n"
            + "  <a id='clickme' href='test.html'>Click me</a>\n" + "</body></html>";
    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("clickme")).click();

    assertEquals(getExpectedAlerts()[0], Integer.toString(getMockWebConnection().getRequestCount()));
    assertEquals(getExpectedAlerts()[1], acceptHeaderString());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs// w w w .j a  v  a2 s .  co  m
 */
@Test
@Alerts(DEFAULT = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", CHROME = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", IE = "Accept: text/html, application/xhtml+xml, */*")
public void acceptHeaderAnchorClickWithType() throws Exception {
    String html = "<html><body>Response</body></html>";
    getMockWebConnection().setDefaultResponse(html);

    html = "<html><head><title>First</title></head>\n" + "<body>\n"
            + "  <a id='clickme' href='test.html' type='text/plain'>Click me</a>\n" + "</body></html>";
    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("clickme")).click();

    assertEquals(2, getMockWebConnection().getRequestCount());
    assertEquals(getExpectedAlerts()[0], acceptHeaderString());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from ww w .ja  v a2s.c o m*/
 */
@Test
public void domainDuplicateLeadingDotSend() throws Exception {
    final String html = "<html><body>\n" + "<a href='next.html'>next page</a>\n" + "</body></html>";

    final List<NameValuePair> responseHeader = new ArrayList<>();
    responseHeader.add(new NameValuePair("Set-Cookie", "c1=1; Domain=host1." + DOMAIN + "; Path=/"));
    responseHeader.add(new NameValuePair("Set-Cookie", "c2=2; Domain=.host1." + DOMAIN + "; Path=/"));

    getMockWebConnection().setDefaultResponse(html, 200, "Ok", "text/html", responseHeader);

    final WebDriver webDriver = getWebDriver();
    webDriver.manage().deleteAllCookies();

    loadPageWithAlerts2(new URL(URL_HOST1));
    WebRequest lastRequest = getMockWebConnection().getLastWebRequest();
    assertNull(lastRequest.getAdditionalHeaders().get("Cookie"));

    webDriver.findElement(By.linkText("next page")).click();
    lastRequest = getMockWebConnection().getLastWebRequest();
    assertEquals("c1=1; c2=2", lastRequest.getAdditionalHeaders().get("Cookie"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails//  w w w . ja  v  a2  s  . c om
 */
@Test
// TODO [IE]SINGLE-VS-BULK test runs when executed as single but breaks as bulk
public void orderCookiesByPath_fromJs() throws Exception {
    final String html = "<html><body><script>\n" + "document.cookie = 'exampleCookie=rootPath;path=/';\n"
            + "document.cookie = 'exampleCookie=currentPath;path=/testpages/';\n" + "</script>\n"
            + "<a href='/testpages/next.html'>next page</a>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");

    final WebDriver webDriver = getWebDriver();
    webDriver.manage().deleteAllCookies();

    loadPage2(html);
    webDriver.findElement(By.linkText("next page")).click();

    final WebRequest lastRequest = getMockWebConnection().getLastWebRequest();
    assertEquals("exampleCookie=currentPath; exampleCookie=rootPath",
            lastRequest.getAdditionalHeaders().get("Cookie"));
}

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

License:Apache License

/**
 * Tests going in history {@link History#back()} with {@code POST} request.
 *
 * @throws Exception if an error occurs/*from www . ja  va2 s  .c  om*/
 */
@Test
public void post() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/post1", Post1Servlet.class);
    servlets.put("/post2", Post2Servlet.class);
    servlets.put("/post3", Post3Servlet.class);
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();
    driver.get(URL_FIRST + "post1");

    driver.findElement(By.id("mySubmit")).click();
    assertEquals(URL_FIRST + "post2", driver.getCurrentUrl());
    assertTrue(driver.getPageSource().contains("POST"));
    assertTrue(driver.getPageSource().contains("para1=value1"));

    driver.findElement(By.linkText("Go to GET")).click();
    assertEquals(URL_FIRST + "post3", driver.getCurrentUrl());
    assertTrue(driver.getPageSource().contains("GET"));

    driver.navigate().back();
    assertEquals(URL_FIRST + "post2", driver.getCurrentUrl());
    assertTrue(driver.getPageSource().contains("POST"));
    assertTrue(driver.getPageSource().contains("para1=value1"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from w w w  .  j  a  va  2s  .  co  m
 */
@Test
@Alerts("1")
public void clickOnFocus() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form>\n"
            + "  <input type='button' id='textfield1' onfocus='alert(1)'>\n" + "</form>\n" + "</body></html>";

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

    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   w  w  w  . j a  va 2  s .  co m*/
 */
@Test
@Alerts("click click dblclick ")
@BuggyWebDriver({ FF, CHROME })
public void dblClick() throws Exception {
    final String content = "<html>\n" + "<head>\n" + "<script>\n" + "  function clickMe() {\n"
            + "    document.getElementById('myTextarea').value+='click ';\n" + "  }\n"
            + "  function dblClickMe() {\n" + "    document.getElementById('myTextarea').value+='dblclick ';\n"
            + "  }\n" + "</script>\n" + "</head>\n"
            + "<body id='myBody' onclick='clickMe()' ondblclick='dblClickMe()'>\n"
            + "<textarea id='myTextarea'></textarea>\n" + "</body></html>";

    final WebDriver driver = loadPage2(content);

    final Actions action = new Actions(driver);
    action.doubleClick(driver.findElement(By.id("myBody")));
    action.perform();

    assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("myTextarea")).getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   www  .  jav  a 2s  . com*/
 */
@Test
@Alerts("1")
public void clickOnFocus() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form>\n"
            + "    <input type='button' id='textfield1' onfocus='alert(1)'>\n" + "</form>\n" + "</body></html>";

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

    assertEquals(getExpectedAlerts(), getCollectedAlerts(driver));
}

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

License:Apache License

/**
 * @throws Exception on test failure/*from  ww  w  .ja va2  s  .co  m*/
 */
@Test(expected = ElementNotVisibleException.class)
public void clickInvisible() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a id='link' style='display: none'>Click me</a>\n"
            + "</body></html>";

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

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

License:Apache License

/**
 * @throws Exception on test failure//from   w w w.j  a  v  a2 s .c om
 */
@Test
public void clickDisabled() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <button id='id1' disabled>Click Me</button>\n"
            + "</body></html>";

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