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

License:Apache License

/**
 * @throws Exception if the test fails//from   w w  w.  java 2 s.  c o  m
 */
@Test
@Alerts("1")
public void externalUnknownFrom() throws Exception {
    final String html = "<html><head><title>first</title></head><body>\n" + "  <p>hello world</p>\n"
            + "  <form id='myForm' action='" + URL_SECOND + "'>\n" + "  </form>\n"
            + "  <button type='submit' id='myButton' form='unknown'>Explicit Submit</button>\n"
            + "</body></html>";
    final String secondContent = "<html><head><title>second</title></head><body>\n" + "  <p>hello world</p>\n"
            + "</body></html>";

    getMockWebConnection().setDefaultResponse(secondContent);

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

    final int expectedReqCount = Integer.parseInt(getExpectedAlerts()[0]);
    assertEquals(expectedReqCount, getMockWebConnection().getRequestCount());
    if (expectedReqCount > 1) {
        assertEquals(URL_SECOND.toString(), getMockWebConnection().getLastWebRequest().getUrl());
    }
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*  w  w w. ja  v a2 s .  com*/
 */
@Test
@Alerts(DEFAULT = { "2", "second" }, IE = { "1", "first" })
public void externalPreferenceFrom() throws Exception {
    final String html = "<html><head><title>first</title></head><body>\n" + "  <p>hello world</p>\n"
            + "  <form id='myForm2' action='" + URL_SECOND + "'>\n" + "  </form>\n"
            + "  <form id='myForm3' action='" + URL_THIRD + "'>\n"
            + "    <button type='submit' id='myButton' form='myForm2'>Explicit Submit</button>\n"
            + "  </form>\n" + "</body></html>";
    final String secondContent = "<html><head><title>second</title></head><body>\n" + "  <p>hello world</p>\n"
            + "</body></html>";
    final String thirdContent = "<html><head><title>third</title></head><body>\n" + "  <p>hello world</p>\n"
            + "</body></html>";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);
    getMockWebConnection().setResponse(URL_THIRD, thirdContent);

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

    final int expectedReqCount = Integer.parseInt(getExpectedAlerts()[0]);
    assertEquals(expectedReqCount, getMockWebConnection().getRequestCount());
    assertEquals(getExpectedAlerts()[1], driver.getTitle());

    shutDownRealIE();
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   w w w .  j a  v  a2s  .com*/
 */
@Test
@Alerts(DEFAULT = { "2", "second" }, IE = { "1", "first" })
public void internalDifferentFrom() throws Exception {
    final String html = "<html><head><title>first</title></head><body>\n" + "  <p>hello world</p>\n"
            + "  <form id='myForm' action='" + URL_SECOND + "'>\n" + "  </form>\n"
            + "  <form id='other' action='" + URL_THIRD + "'>\n"
            + "    <button type='submit' id='myButton' form='myForm'>Explicit Submit</button>\n" + "  </form>\n"
            + "</body></html>";
    final String secondContent = "<html><head><title>second</title></head><body>\n" + "  <p>hello world</p>\n"
            + "</body></html>";
    final String thirdContent = "<html><head><title>third</title></head><body>\n" + "  <p>hello world</p>\n"
            + "</body></html>";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);
    getMockWebConnection().setResponse(URL_THIRD, thirdContent);

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

    final int expectedReqCount = Integer.parseInt(getExpectedAlerts()[0]);
    assertEquals(expectedReqCount, getMockWebConnection().getRequestCount());
    assertEquals(getExpectedAlerts()[1], driver.getTitle());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*w  w w  .j  a va  2 s  .c o m*/
 */
@Test
@Alerts("foo")
public void click_onClick() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n"
            + "<form id='form1' onSubmit='alert(\"bar\")'>\n"
            + "  <input type='button' name='button' id='button' "
            + "onClick='alert(\"foo\")'>Push me</button>\n" + "</form></body></html>";

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

    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from w  ww .j  a v  a 2  s  . c o  m*/
 */
@Test
@Alerts("foo")
public void click_onClickIgnoreCase() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "  <input type='button' name='button' id='button' "
            + "oNclICK='alert(\"foo\")'>Push me</button>\n" + "</form></body></html>";

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

    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from  w w w  .  j a v  a  2 s.  co  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='checkbox' id='chkbox' onchange='debug(\"foo\");debug(event.type);'>\n" + "</form>\n"
            + "<textarea id='myTextarea'></textarea>\n" + "</body></html>";

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

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

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

License:Apache License

/**
 * @throws Exception if the test fails//from   w  w w .j  a  v  a 2  s  .com
 */
@Test
@Alerts("foo,change,boo,blur,")
public void onchangeFires2() 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='checkbox' id='chkbox'"
            + " onChange='debug(\"foo\");debug(event.type);'" + " onBlur='debug(\"boo\");debug(event.type);'"
            + ">\n" + "<input type='checkbox' id='chkbox2'>\n" + "</form>\n"
            + "<textarea id='myTextarea'></textarea>\n" + "</body></html>";

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

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

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

License:Apache License

/**
 * @throws Exception if the test fails// w w  w .  j a va 2 s  .  c om
 */
@Test
@Alerts("Second")
public void setChecked() throws Exception {
    final String firstHtml = HtmlPageTest.STANDARDS_MODE_PREFIX_
            + "<html><head><title>First</title></head><body>\n" + "<form>\n"
            + "<input id='myCheckbox' type='checkbox' onchange=\"window.location.href='" + URL_SECOND + "'\">\n"
            + "</form>\n" + "</body></html>";
    final String secondHtml = "<html><head><title>Second</title></head><body></body></html>";

    getMockWebConnection().setDefaultResponse(secondHtml);
    final WebDriver driver = loadPage2(firstHtml);

    driver.findElement(By.id("myCheckbox")).click();
    assertEquals(getExpectedAlerts()[0], driver.getTitle());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*w ww  .ja v a 2  s .  com*/
 */
@Test
@Alerts("Second")
public void setChecked2() throws Exception {
    final String firstHtml = HtmlPageTest.STANDARDS_MODE_PREFIX_
            + "<html><head><title>First</title></head><body>\n" + "<form>\n"
            + "<input id='myCheckbox' type='checkbox' onchange=\"window.location.href='" + URL_SECOND + "'\">\n"
            + "<input id='myInput' type='text'>\n" + "</form>\n" + "</body></html>";
    final String secondHtml = "<html><head><title>Second</title></head><body></body></html>";

    getMockWebConnection().setDefaultResponse(secondHtml);
    final WebDriver driver = loadPage2(firstHtml);

    driver.findElement(By.id("myCheckbox")).click();
    assertEquals(getExpectedAlerts()[0], driver.getTitle());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from  w  w w . j a v  a2 s . c  om
 */
@Test
public void preventDefault() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><script>\n"
            + "  function handler(e) {\n" + "    if (e)\n" + "      e.preventDefault();\n" + "    else\n"
            + "      return false;\n" + "  }\n" + "  function init() {\n"
            + "    document.getElementById('checkbox1').onclick = handler;\n" + "  }\n" + "</script></head>\n"
            + "<body onload='init()'>\n" + "<input type='checkbox' id='checkbox1'/>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement checkbox = driver.findElement(By.id("checkbox1"));
    checkbox.click();
    assertFalse(checkbox.isSelected());
}