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

License:Apache License

/**
 * @throws Exception if the test fails//from  w w w  . jav  a 2 s.  c  o m
 */
@Test(expected = NoSuchElementException.class)
public void submitWithoutForm() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <input id='t' type='number' value='1234'>\n" + "</body>\n"
            + "</html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("t")).submit();

    assertEquals(1, getMockWebConnection().getRequestCount());
}

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

License:Apache License

/**
 * @throws Exception if the test fails// w  w w  .j av a 2 s .c om
 */
@Test
@Alerts(DEFAULT = "oDown,sDown,dDown,oUp,sUp,dUp,", IE = "sDown,dDown,sUp,dUp,")
// there seems to be a bug in selenium; for FF >= 10 this triggers
// "sDown,dDown,sUp,dUp,oDown,sDown,dDown,oUp,sUp,dUp," but a
// manual test shows, that this is wrong.
// for Chrome selenium shows only "sUp,dUp," but again
// manual test are showing something different
@BuggyWebDriver({ CHROME, FF })
public void onMouse() throws Exception {
    final String html = "<html><head><title>foo</title>\n" + "<script>\n" + "  function debug(string) {\n"
            + "    document.getElementById('myTextarea').value += string + ',';\n" + "  }\n" + "</script>\n"
            + "</head><body>\n" + "  <form>\n" + "    <div" + " onMouseDown='debug(\"dDown\");'"
            + " onMouseUp='debug(\"dUp\");'>\n" + "    <select name='select1' size='4'"
            + " onMouseDown='debug(\"sDown\");'" + " onMouseUp='debug(\"sUp\");'>\n"
            + "      <option id='opt1' value='option1'>Option1</option>\n"
            + "      <option id='opt2' value='option2' selected='selected'>Option2</option>\n"
            + "      <option id='opt3' value='option3'" + " onMouseDown='debug(\"oDown\");'"
            + " onMouseUp='debug(\"oUp\");'>Option3</option>\n" + "    </select>\n" + "    </div>\n"
            + "  </form>\n" + "  <textarea id='myTextarea'></textarea>\n" + "</body></html>";

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

    assertEquals(Arrays.asList(getExpectedAlerts()).toString(),
            '[' + driver.findElement(By.id("myTextarea")).getAttribute("value") + ']');
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from  www. j  a  v  a 2  s. c o  m
 */
@Test
public void emptyJavaScript() throws Exception {
    final String html = "<body>\n" + "<a id='myAnchor' href='javascript:'>Hello</a>\n" + "</body>";

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

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

License:Apache License

/**
 * @throws Exception if the test fails//from ww w . ja v  a 2s .  c om
 */
@Test
public void getInputByName() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title></head>\n" + "<body>\n" + "<p>hello world</p>\n"
            + "<form id='form1' action='/formSubmit' method='post'>\n"
            + "  <input type='text' NAME='textInput1' value='textInput1'/>\n"
            + "  <input type='text' name='textInput2' value='textInput2'/>\n"
            + "  <input type='hidden' name='hidden1' value='hidden1'/>\n"
            + "  <input type='submit' name='submitInput1' value='push me'/>\n" + "</form>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);

    final WebElement form = driver.findElement(By.id("form1"));
    final WebElement input = form.findElement(By.name("textInput1"));
    assertEquals("name", "textInput1", input.getAttribute("name"));

    assertEquals("value", "textInput1", input.getAttribute("value"));
    assertEquals("type", "text", input.getAttribute("type"));
}

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

License:Apache License

private void basePath(final String baseUrl, final String expected) throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <base href='" + baseUrl + "'>\n" + "</head>\n" + "<body>\n"
            + "  <a id='testLink' href='path'>click me</a>\n" + "</body></html>";
    final WebDriver webDriver = loadPage2(html, URL_SECOND);
    webDriver.findElement(By.id("testLink")).click();
    assertEquals(expected, webDriver.getCurrentUrl());
}

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

License:Apache License

/**
 * Verifies that a asText() returns the value string.
 * @throws Exception if the test fails/*  w ww  . ja va 2s  .c om*/
 */
@Test
public void asText() throws Exception {
    final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "  <input type='password' name='foo' id='foo' value='bla'>\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.HtmlPasswordInputTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*from ww  w  .  j  ava2s  . c o m*/
 */
@Test
public void type() throws Exception {
    final String html = "<html><head></head><body><input type='password' id='p'/></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));
    p.sendKeys("abc");
    assertEquals("abc", p.getAttribute("value"));
    p.sendKeys("\b");
    assertEquals("ab", p.getAttribute("value"));
    p.sendKeys("\b");
    assertEquals("a", p.getAttribute("value"));
    p.sendKeys("\b");
    assertEquals("", p.getAttribute("value"));
    p.sendKeys("\b");
    assertEquals("", p.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   w w w .j av a  2 s.c o  m*/
 */
@Test
public void typeWhileDisabled() throws Exception {
    final String html = "<html><body><input type='password' id='p' disabled='disabled'/></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));
    try {
        p.sendKeys("abc");
        fail();
    } catch (final InvalidElementStateException e) {
        // as expected
    }
    assertEquals("", p.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails// w ww.  j a v  a2s. c  om
 */
@Test
@Alerts({ "null", "null" })
public void typeDoesNotChangeValueAttribute() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n" + "  <input type='password' id='p'/>\n"
            + "  <button id='check' onclick='alert(document.getElementById(\"p\").getAttribute(\"value\"));'>"
            + "DoIt</button>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));

    final WebElement check = driver.findElement(By.id("check"));
    check.click();
    verifyAlerts(driver, getExpectedAlerts()[0]);

    p.sendKeys("abc");
    check.click();
    verifyAlerts(driver, getExpectedAlerts()[1]);
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from w  w w .  j a v  a2 s  . co m*/
 */
@Test
@Alerts({ "HtmlUnit", "HtmlUnit" })
public void typeDoesNotChangeValueAttributeWithInitialValue() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n"
            + "  <input type='password' id='p' value='HtmlUnit'/>\n"
            + "  <button id='check' onclick='alert(document.getElementById(\"p\").getAttribute(\"value\"));'>"
            + "DoIt</button>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));

    final WebElement check = driver.findElement(By.id("check"));
    check.click();
    verifyAlerts(driver, getExpectedAlerts()[0]);

    p.sendKeys("abc");
    check.click();
    verifyAlerts(driver, getExpectedAlerts()[1]);
}