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

License:Apache License

/**
 * Verifies that a HtmlCheckBox is unchecked by default.
 * The onClick tests make this assumption.
 * @throws Exception if the test fails/*from  www  . j  av a 2  s. c o  m*/
 */
@Test
public void defaultState() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title></head><body>\n"
            + "<form id='form1'>\n"
            + "  <input type='checkbox' name='checkbox' id='checkbox'>Check me</input>\n"
            + "</form></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement checkbox = driver.findElement(By.id("checkbox"));
    assertFalse(checkbox.isSelected());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from  w  ww  . j a va  2s. c  om*/
 */
@Test
@Alerts({ "true", "null", "false", "", "false", "yes" })
public void checkedAttribute() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title>\n" + "<script>\n"
            + "  function test() {\n" + "    var checkbox = document.getElementById('c1');\n"
            + "    alert(checkbox.checked);\n" + "    alert(checkbox.getAttribute('checked'));\n"

            + "    checkbox = document.getElementById('c2');\n" + "    alert(checkbox.checked);\n"
            + "    alert(checkbox.getAttribute('checked'));\n"

            + "    checkbox = document.getElementById('c3');\n" + "    alert(checkbox.checked);\n"
            + "    alert(checkbox.getAttribute('checked'));\n" + "  }\n" + "</script>\n" + "</head><body>\n"
            + "<form>\n" + "  <input type='checkbox' id='c1' name='radar' value='initial'>\n"
            + "  <input type='checkbox' id='c2' name='radar' value='initial' checked>\n"
            + "  <input type='checkbox' id='c3' name='radar' value='initial' checked='yes'>\n" + "</form>\n"
            + "  <button id='clickMe' onClick='test()'>do it</button>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("c1")).click();
    driver.findElement(By.id("c2")).click();
    driver.findElement(By.id("c3")).click();

    driver.findElement(By.id("clickMe")).click();
    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * Verifies that a asText() returns the value string.
 * @throws Exception if the test fails/*from w w w.jav  a 2s . co  m*/
 */
@Test
public void asText() throws Exception {
    final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "  <input type='color' name='foo' id='foo' value='#ff0000'>\n" + "</form></body></html>";

    final WebDriver driver = loadPage2(htmlContent);

    final WebElement input = driver.findElement(By.id("foo"));
    assertEquals("", input.getText());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//ww  w .j a va  2s.c om
 */
@Test
@NotYetImplemented
//TODO: fails because of HTMLElement.getContentEditable doesn't detect DomElement.ATTRIBUTE_VALUE_EMPTY
// this could be a general attribute issue
public void contentEditable() throws Exception {
    final String html = "<html>\n" + "<body contentEditable><p>initial</p></body>\n" + "</html>";

    final WebDriver driver = loadPage2(html);
    final WebElement body = driver.findElement(By.xpath("//body"));
    body.clear();
    body.sendKeys("something");
    assertEquals("something", body.getText());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from   w w w .  j  av  a  2 s  . c  o m*/
 */
@Test
@Alerts(DEFAULT = "down: 16,0 down: 49,0 press: 33,33 up: 49,0 up: 16,0"
        + " down: 16,0 down: 220,0 press: 124,124 up: 220,0 up: 16,0", FF = "down: 16,0 down: 49,0 press: 0,33 up: 49,0 up: 16,0"
                + " down: 16,0 down: 220,0 press: 0,124 up: 220,0 up: 16,0")
//https://github.com/SeleniumHQ/selenium/issues/639
@BuggyWebDriver(Browser.FF)
public void shiftKeys() throws Exception {
    final String html = "<html><head><script>\n" + "  function appendMessage(message) {\n"
            + "    document.getElementById('result').innerHTML += message + ' ';\n" + "  }\n"
            + "</script></head>\n" + "<body >\n"
            + "  <input id='input1' onkeyup=\"appendMessage('up: ' + event.keyCode + ',' + event.charCode)\" "
            + "onkeypress=\"appendMessage('press: ' + event.keyCode + ',' + event.charCode)\" "
            + "onkeydown=\"appendMessage('down: ' + event.keyCode + ',' + event.charCode)\"><br>\n"
            + "<p id='result'></p>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement input = driver.findElement(By.id("input1"));
    final WebElement result = driver.findElement(By.id("result"));
    input.sendKeys("!|");
    assertEquals(getExpectedAlerts()[0], result.getText());
}

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

License:Apache License

/**
 * @throws Exception on test failure/*from  w  ww .jav  a  2s  .  c o  m*/
 */
@Test
public void keyPressEventWhenPreventsDefault() throws Exception {
    final String html = "<html>\n" + "<body>\n"
            + "  <input id='suppress' onkeydown='event.preventDefault()' onkeypress='alert(\"press\")'>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("suppress")).sendKeys("s");
    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception on test failure//from w  ww  .j a  v  a  2 s.c  o  m
 */
@Test
@Alerts("press")
public void keyUpEventWhenPreventsDefault() throws Exception {
    final String html = "<html>\n" + "<body>\n"
            + "  <input id='suppress' onkeydown='event.preventDefault()' onkeyup='alert(\"press\")'>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("suppress")).sendKeys("s");
    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from  w w  w  . j  a v  a  2 s  .  c o  m
 */
@Test
public void typing() throws Exception {
    final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "  <input type='email' id='foo'>\n" + "</form></body></html>";

    final WebDriver driver = loadPage2(htmlContent);

    final WebElement input = driver.findElement(By.id("foo"));
    input.sendKeys("hello");
    assertEquals("hello", input.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails// w  w  w  . j  a  v  a2  s . co  m
 */
@Test
@Alerts("[object HTMLEmbedElement]")
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" + "  <embed id='myId'/>\n" + "</body></html>";

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

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

License:Apache License

/**
 * @throws Exception if the test fails//from  w w  w.j a  va 2 s .c  om
 */
@Test
@Alerts("[object HTMLEmbedElement]")
public void saveAs() 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" + "  <embed id='myId' src='helloworld.bin'/>\n"
            + "</body></html>";

    getMockWebConnection().setDefaultResponse("something");
    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlEmbed element = (HtmlEmbed) toHtmlElement(driver.findElement(By.id("myId")));
        final File file = new File(System.getProperty("user.home"), "htmlunit-embed.bin");
        element.saveAs(file);
        final long length = file.length();
        file.delete();
        assertTrue(length > 0);
    }
}