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

License:Apache License

/**
 * @throws Exception on test failure/*from  www  .j a v  a 2 s  . co  m*/
 */
@Test(expected = InvalidElementStateException.class)
public void sendKeysToDisabled() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <input id='id1' disabled>\n" + "</body></html>";

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

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

License:Apache License

private void testWithClick(final String body) throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + body + "<div id='other'>other</div>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);

    driver.findElement(By.id("focusId")).click();
    driver.findElement(By.id("other")).click();

    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * Test that focus() called on a non focusable element doesn't trigger document's focus handlers.
 *
 * @throws Exception if the test fails/* w ww  .j  a  v a  2  s  .c  o m*/
 */
@Test
@Alerts(DEFAULT = "done\nfocus", CHROME = "done")
@NotYetImplemented({ FF, IE })
public void focusOnNonFocusableElementShouldNotTriggerDocumentFocus() throws Exception {
    final String html = "<html><head>\n" + "<script>\n" + "  function log(x) {\n"
            + "    document.getElementById('log').value += x + '\\n';\n" + "  }\n" + "  function ff() {\n"
            + "    log('focus');\n" + "  }\n" + "</script>\n"

            + "</head><body>\n" + "<div id='it'>div</div>\n" + "<textarea id='log'></textarea>\n"

            + "<script>\n" + "  document.addEventListener('focus', ff, true);\n"
            + "  document.getElementById('it').focus();\n" + "  document.getElementById('it').blur();\n"
            + "  log('done');\n" + "</script>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("log")).getAttribute("value").trim());
}

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

License:Apache License

/**
 * Test that focus() called on a non focusable element doesn't let focused element loose the focus.
 *
 * @throws Exception if the test fails//  www .ja va2  s  .  c  o  m
 */
@Test
@BuggyWebDriver(Browser.IE)
@Alerts({ "input1", "focus1", "div", "input2", "blur1", "focus2" })
public void focusOnNonFocusableElementShouldNotChangeCurrentFocus() throws Exception {
    final String html = "<html><head>\n" + "<script>\n" + "  function log(x) {\n"
            + "    document.getElementById('log').value += x + '\\n';\n" + "  }\n" + "</script>\n"

            + "</head><body>\n" + "<textarea id='log'></textarea>\n"
            + "<div id='div' onblur=\"log('blur')\" onfocus=\"log('focus')\">div</div>\n"
            + "<input id='input1' onblur=\"log('blur1')\" onfocus=\"log('focus1')\">\n"
            + "<input id='input2' onblur=\"log('blur2')\" onfocus=\"log('focus2')\">\n" + "<script>\n"
            + "  log('input1');\n" + "  document.getElementById('input1').focus();\n" + "  log('div');\n"
            + "  document.getElementById('div').focus();\n" + "  log('input2');\n"
            + "  document.getElementById('input2').focus();\n" + "</script></body></html>";

    final WebDriver driver = loadPage2(html);
    final String[] alerts = driver.findElement(By.id("log")).getAttribute("value").split("\r?\n");
    assertEquals(getExpectedAlerts(), Arrays.asList(alerts));
}

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

License:Apache License

/**
 * Test that click called on a non focusable element removes focus from focused element.
 *
 * @throws Exception if the test fails/*from   w w  w .j av  a2 s  .  c  o  m*/
 */
@Test
@Alerts({ "focus1", "blur1" })
public void clickOnNonFocusableElementChangesCurrentFocus() throws Exception {
    final String html = "<html><body>\n" + "<textarea id='log'></textarea>\n"
            + "<div id='div' onblur=\"log('blur')\" onfocus=\"log('focus')\">div</div>\n"
            + "<input id='input1' onblur=\"log('blur1')\" onfocus=\"log('focus1')\">\n" + "<script>\n"
            + "  function log(x) {\n" + "    document.getElementById('log').value += x + '\\n';\n" + "  }\n"
            + "  \n" + "</script></body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("input1")).click();
    driver.findElement(By.id("div")).click();
    final String[] alerts = driver.findElement(By.id("log")).getAttribute("value").split("\r?\n");
    assertEquals(getExpectedAlerts(), Arrays.asList(alerts));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from  ww w  . j  a  v  a2  s  . c o m*/
 */
@Test
@Alerts({ "hi", "%28%29" })
public void href_js_escaping() throws Exception {
    final String html = "<html><head><script>\n" + "  function sayHello(text) {\n" + "    alert(text);\n"
            + "  }\n" + "</script></head>\n" + "<body>\n"
            + "  <a id='myAnchor' href=\"javascript:sayHello%28'hi'%29\">My Link</a>\n"
            + "  <input id='myButton' type=button onclick=\"javascript:sayHello('%28%29')\" value='My Button'>\n"
            + "</body></html>";

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

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

License:Apache License

/**
 * @throws Exception if an error occurs/*  w  w w  . j  a v  a2  s  . c o m*/
 */
@Test
@Alerts({ "(*%a", "%28%A" })
public void href_js_escaping2() throws Exception {
    final String html = "<html><head><script>\n" + "  function sayHello(text) {\n" + "    alert(text);\n"
            + "  }\n" + "</script></head>\n" + "<body>\n"
            + "  <a id='myAnchor' href=\"javascript:sayHello%28'%28%2a%a'%29\">My Link</a>\n"
            + "  <input id='myButton' type=button onclick=\"javascript:sayHello('%28%A')\" value='My Button'>\n"
            + "</body></html>";

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

    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*  ww w .j  a  v  a2 s.  com*/
 */
@Test
public void clickNestedElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <span id='theSpan'>My Link</span>\n" + "  </a>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement span = driver.findElement(By.id("theSpan"));
    assertEquals("span", span.getTagName());
    span.click();
    assertEquals(new URL(getDefaultUrl(), "page2.html").toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from   www  .ja  v  a 2  s  .  c  om*/
 */
@Test
public void clickNestedButtonElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <button id='theButton'></button>\n" + "  </a>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement button = driver.findElement(By.id("theButton"));
    assertEquals("button", button.getTagName());
    button.click();
    assertEquals(new URL(getDefaultUrl(), "page2.html").toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from   www  .  j av a  2s  .  co m
 */
@Test
@Alerts(DEFAULT = "page2.html", CHROME = "", IE = "")
public void clickNestedCheckboxElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <input type='checkbox' id='theCheckbox' name='myCheckbox' value='Milk'>\n" + "  </a>\n"
            + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement checkbox = driver.findElement(By.id("theCheckbox"));
    assertEquals("input", checkbox.getTagName());
    checkbox.click();
    assertEquals(new URL(getDefaultUrl(), getExpectedAlerts()[0]).toString(), driver.getCurrentUrl());
}