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

License:Apache License

/**
 * @throws Exception if an error occurs/*from  www.j a v a2 s.  com*/
 */
@Test
public void preventDefault_OnKeyDown() throws Exception {
    final String html = "<html><head><script>\n" + "  function handler(e) {\n"
            + "    if (e && e.target.value.length > 2)\n" + "      e.preventDefault();\n"
            + "    else if (!e && window.event.srcElement.value.length > 2)\n" + "      return false;\n"
            + "  }\n" + "  function init() {\n" + "    document.getElementById('p').onkeydown = handler;\n"
            + "  }\n" + "</script></head>\n" + "<body onload='init()'>\n"
            + "<input type='password' id='p'></input>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));
    p.sendKeys("abcd");
    assertEquals("abc", p.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//  w ww  .ja  va  2  s  . c  om
 */
@Test
public void preventDefault_OnKeyPress() throws Exception {
    final String html = "<html><head><script>\n" + "  function handler(e) {\n"
            + "    if (e && e.target.value.length > 2)\n" + "      e.preventDefault();\n"
            + "    else if (!e && window.event.srcElement.value.length > 2)\n" + "      return false;\n"
            + "  }\n" + "  function init() {\n" + "    document.getElementById('p').onkeypress = handler;\n"
            + "  }\n" + "</script></head>\n" + "<body onload='init()'>\n"
            + "<input type='password' id='p'></input>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));
    p.sendKeys("abcd");
    assertEquals("abc", p.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from  w  w  w  .ja  v a 2s. c o m*/
 */
@Test
public void typeOnChange() throws Exception {
    final String html = "<html><head></head><body>\n" + "<input type='password' id='p' value='Hello world'"
            + " onChange='alert(\"foo\");alert(event.type);'" + " onBlur='alert(\"boo\");alert(event.type);'>\n"
            + "<button id='b'>some button</button>\n" + "</body></html>";

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

    assertTrue(getCollectedAlerts(driver, 1).isEmpty());

    // trigger lost focus
    driver.findElement(By.id("b")).click();
    final String[] expectedAlerts1 = { "foo", "change", "boo", "blur" };
    assertEquals(expectedAlerts1, getCollectedAlerts(driver, 4));

    // set only the focus but change nothing
    p.click();
    assertTrue(getCollectedAlerts(driver, 1).isEmpty());

    // trigger lost focus
    driver.findElement(By.id("b")).click();
    final String[] expectedAlerts2 = { "boo", "blur" };
    assertEquals(expectedAlerts2, getCollectedAlerts(driver, 2));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from w  ww.  j av a  2 s.  co m
 */
@Test
public void setValueOnChange() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n"
            + "  <input type='password' id='p' value='Hello world'"
            + " onChange='alert(\"foo\");alert(event.type);'>\n" + "  <button id='b'>some button</button>\n"
            + "  <button id='set' onclick='document.getElementById(\"p\").value=\"HtmlUnit\"'>setValue</button>\n"
            + "</body></html>";

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

    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));

    // trigger lost focus
    driver.findElement(By.id("b")).click();
    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from www  .j  a v a  2 s  . c o m*/
 */
@Test
public void setDefaultValueOnChange() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n"
            + "  <input type='password' id='p' value='Hello world'"
            + " onChange='alert(\"foo\");alert(event.type);'>\n" + "  <button id='b'>some button</button>\n"
            + "  <button id='set' onclick='document.getElementById(\"p\").defaultValue=\"HtmlUnit\"'>"
            + "setValue</button>\n" + "</body></html>";

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

    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));

    // trigger lost focus
    driver.findElement(By.id("b")).click();
    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));
}

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

License:Apache License

/**
 * @throws Exception if the test fails//w ww  .  j  a  v a  2  s  . c o  m
 */
@Test
@Alerts("  hello   abc")
public void asText() throws Exception {
    final String html = "<html><head></head><body>\n" + "<pre id='foo'>  hello \t abc</pre>" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("foo")).getText());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from ww w  . ja v a2  s  . c  om*/
 */
@Test
@Alerts("1\n2\n3\n4")
public void asTextDifferentLineBreaks() throws Exception {
    final String html = "<html><head></head><body>\n" + "<pre id='foo'>1\n2\r\n3\r4</pre>" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("foo")).getText());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//w w w  . j  av a2s.co  m
 */
@Test
@Alerts("start\n  hello   abc \nend")
public void asTextInsideDiv() throws Exception {
    final String html = "<html><head></head><body>\n" + "<div id='foo'>start<pre>  hello \t abc </pre>end</div>"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);
    assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("foo")).getText());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from   ww w .j ava  2  s  . c  o  m
 */
@Test
@Alerts(DEFAULT = { "[object HTMLQuoteElement]", "[object HTMLQuoteElement]" }, IE = {
        "[object HTMLQuoteElement]", "[object HTMLBlockElement]" })
public void simpleScriptable() throws Exception {
    final String html = "<html><head>\n" + "<script>\n" + "  function test() {\n"
            + "    alert(document.getElementById('myId1'));\n"
            + "    alert(document.getElementById('myId2'));\n" + "  }\n" + "</script>\n"
            + "</head><body onload='test()'>\n" + "  <q id='myId1'>First Quote</q>\n"
            + "  <blockquote id='myId2'>Second Quote</blockquote>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        assertTrue(toHtmlElement(driver.findElement(By.id("myId1"))) instanceof HtmlInlineQuotation);
        assertTrue(toHtmlElement(driver.findElement(By.id("myId2"))) instanceof HtmlBlockQuote);
    }
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   w  w  w.  ja v  a2 s  .  c  o  m*/
 */
@Test
@Alerts("foo,change,")
public void onchangeFires() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<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" + "<input type='radio' name='radioGroup' id='radio'"
            + " onchange='debug(\"foo\");debug(event.type);'>Check me</input>\n" + "</form>\n"
            + "<textarea id='myTextarea'></textarea>\n" + "</body></html>";

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

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