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

License:Apache License

/**
 * @throws Exception if the test fails//from   w ww .  j  a va2s. c o  m
 */
@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='radio' name='radioGroup' id='radio1'"
            + " onChange='debug(\"foo\");debug(event.type);'" + " onBlur='debug(\"boo\");debug(event.type);'"
            + ">Check Me</input>\n" + "<input type='radio' name='radioGroup' id='radio2'>Or Check Me</input>\n"
            + "</form>\n" + "<textarea id='myTextarea'></textarea>\n" + "</body></html>";

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

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

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

License:Apache License

/**
 * @throws Exception if the test fails/* w w w.j  ava  2s  .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 type='radio' name='radioGroup' id='radio'" + " 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("radio")).click();
    assertEquals(getExpectedAlerts()[0], driver.getTitle());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/* w  w  w .j  av  a2 s .  c  om*/
 */
@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 type='radio' name='radioGroup' id='radio'" + " 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("radio")).click();
    assertEquals(getExpectedAlerts()[0], driver.getTitle());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from  w w w  .  j  a  v  a2s .c  o m*/
 */
@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('radio1').onclick = handler;\n" + "  }\n" + "</script></head>\n"
            + "<body onload='init()'>\n" + "  <input type='radio' id='radio1' name='radio1' />\n"
            + "</body></html>";

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

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

License:Apache License

/**
 * Verifies that a HtmlRadioButton is unchecked by default.
 * The onClick tests make this assumption.
 * @throws Exception if the test fails// w ww.  j  a va2 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='radio' name='radio' id='radio'>Check me</input>\n"
            + "</form></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement radio = driver.findElement(By.id("radio"));
    assertFalse(radio.isSelected());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//w w w  .  j  a  va  2  s  .  co m
 */
@Test
@Alerts({ "true", "null", "true", "", "true", "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('r1');\n"
            + "    alert(checkbox.checked);\n" + "    alert(checkbox.getAttribute('checked'));\n"

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

            + "    checkbox = document.getElementById('r3');\n" + "    alert(checkbox.checked);\n"
            + "    alert(checkbox.getAttribute('checked'));\n" + "  }\n" + "</script>\n" + "</head><body'>\n"
            + "<form name='myForm'>\n" + "  <input type='radio' id='r1' name='myRadio'>\n"
            + "  <input type='radio' name='myRadio'>\n" + "</form>\n" + "<form name='myForm'>\n"
            + "  <input type='radio' id='r2' name='myRadio' checked>\n"
            + "  <input type='radio' name='myRadio'>\n" + "</form>\n" + "<form name='myForm'>\n"
            + "  <input type='radio' id='r3' name='myRadio' checked='yes'>\n"
            + "  <input type='radio' name='myRadio'>\n" + "</form>\n"
            + "  <button id='clickMe' onClick='test()'>do it</button>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("r1")).click();
    driver.findElement(By.id("r2")).click();
    driver.findElement(By.id("r3")).click();

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

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

License:Apache License

/**
 * @throws Exception if the test fails/*  w w  w.  j  ava  2s .c  om*/
 */
@Test
public void setCheckedOutsideForm() throws Exception {
    final String html = "<html><head>\n" + "</head>\n" + "<body>\n"
            + "<input type='radio' id='radio1' name='myRadio'>\n"
            + "<input type='radio' id='radio2' name='myRadio'>\n" + "<form name='myForm'>\n"
            + "  <input type='radio' id='radio3' name='myRadio'>\n"
            + "  <input type='radio' id='radio4' name='myRadio'>\n" + "</form>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);

    final WebElement radio1 = driver.findElement(By.id("radio1"));
    final WebElement radio2 = driver.findElement(By.id("radio2"));
    final WebElement radio3 = driver.findElement(By.id("radio3"));
    final WebElement radio4 = driver.findElement(By.id("radio4"));

    assertFalse(radio1.isSelected());
    assertFalse(radio2.isSelected());
    assertFalse(radio3.isSelected());
    assertFalse(radio4.isSelected());

    radio1.click();

    assertTrue(radio1.isSelected());
    assertFalse(radio2.isSelected());
    assertFalse(radio3.isSelected());
    assertFalse(radio4.isSelected());

    radio2.click();

    assertFalse(radio1.isSelected());
    assertTrue(radio2.isSelected());
    assertFalse(radio3.isSelected());
    assertFalse(radio4.isSelected());

    radio3.click();

    assertFalse(radio1.isSelected());
    assertTrue(radio2.isSelected());
    assertTrue(radio3.isSelected());
    assertFalse(radio4.isSelected());

    radio4.click();

    assertFalse(radio1.isSelected());
    assertTrue(radio2.isSelected());
    assertFalse(radio3.isSelected());
    assertTrue(radio4.isSelected());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from  w w w .j  a  v  a2s. com
 */
@Test
@Alerts("[object HTMLScriptElement]")
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" + "  <script id='myId'></script>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    assertEquals("script", driver.findElement(By.id("myId")).getTagName());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*w w  w.j  a  v a2  s  .  co  m*/
 */
@Test
public void type() throws Exception {
    final String html = "<html><head></head><body><input id='t' type='search'/></body></html>";

    final WebDriver webDriver = loadPage2(html);
    final WebElement input = webDriver.findElement(By.id("t"));
    input.sendKeys("abc");
    assertEquals("abc", input.getAttribute("value"));
    input.sendKeys("\b");
    assertEquals("ab", input.getAttribute("value"));
    input.sendKeys("\b");
    assertEquals("a", input.getAttribute("value"));
    input.sendKeys("\b");
    assertEquals("", input.getAttribute("value"));
    input.sendKeys("\b");
    assertEquals("", input.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from w w w.  j av a2  s  .c om
 */
@Test
public void asText() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>foo</title>\n"
            + "  <style type='text/css' id='s'>\n" + "    img { border: 0px }\n" + "  </style>\n" + "</head>\n"
            + "<body>\n" + "</body>\n" + "</html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    final String text = driver.findElement(By.id("s")).getText();
    assertEquals("", text);
}