Example usage for org.openqa.selenium WebElement isSelected

List of usage examples for org.openqa.selenium WebElement isSelected

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement isSelected.

Prototype

boolean isSelected();

Source Link

Document

Determine whether or not this element is selected or not.

Usage

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//from  w  w  w  . j a v  a2  s .com
 */
@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 v a2 s  .co  m*/
 */
@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.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*w  ww.ja  v  a2 s. co  m*/
 */
@Test
@Alerts({ "false", "true" })
public void checkedAttribute_Checkbox() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title><script>\n"
            + "function test() {\n" + "  alert(document.form1.checkbox1.checked);\n"
            + "  document.form1.checkbox1.checked = true;\n" + "  alert(document.form1.checkbox1.checked);\n"
            + "}\n" + "</script></head><body>\n" + "<p>hello world</p>\n" + "<form name='form1'>\n"
            + "  <input type='cheCKbox' name='checkbox1' id='checkbox1' value='foo' />\n" + "</form>\n"
            + "<a href='javascript:test()' id='clickme'>click me</a>\n" + "</body></html>";

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

    driver.findElement(By.id("clickme")).click();
    verifyAlerts(driver, getExpectedAlerts());
    assertTrue(checkBox.isSelected());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails/* www .j av a2s. c o  m*/
 */
@Test
@Alerts({ "true", "false", "false", "false", "true", "false" })
public void checkedAttribute_Radio() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title><script>\n"
            + "function test() {\n" + "  alert(document.form1.radio1[0].checked);\n"
            + "  alert(document.form1.radio1[1].checked);\n" + "  alert(document.form1.radio1[2].checked);\n"
            + "  document.form1.radio1[1].checked = true;\n" + "  alert(document.form1.radio1[0].checked);\n"
            + "  alert(document.form1.radio1[1].checked);\n" + "  alert(document.form1.radio1[2].checked);\n"
            + "}\n" + "</script></head><body>\n" + "<p>hello world</p>\n" + "<form name='form1'>\n"
            + "  <input type='radio' name='radio1' id='radioA' value='a' checked='checked'/>\n"
            + "  <input type='RADIO' name='radio1' id='radioB' value='b' />\n"
            + "  <input type='radio' name='radio1' id='radioC' value='c' />\n" + "</form>\n"
            + "<a href='javascript:test()' id='clickme'>click me</a>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement radioA = driver.findElement(By.id("radioA"));
    final WebElement radioB = driver.findElement(By.id("radioB"));
    final WebElement radioC = driver.findElement(By.id("radioC"));
    assertTrue(radioA.isSelected());
    assertFalse(radioB.isSelected());
    assertFalse(radioC.isSelected());

    driver.findElement(By.id("clickme")).click();
    verifyAlerts(driver, getExpectedAlerts());
    assertFalse(radioA.isSelected());
    assertTrue(radioB.isSelected());
    assertFalse(radioC.isSelected());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLabelElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails/* w w  w.j  a  v  a 2s .  c  o  m*/
 */
@Test
public void htmlFor() throws Exception {
    final String html = "<html>\n" + "<head><title>First</title>\n" + "<script>\n" + "  function doTest() {\n"
            + "    document.getElementById('label1').htmlFor = 'checkbox1';\n" + "  }\n" + "</script>\n"
            + "</head>\n" + "<body onload='doTest()'>\n" + "  <label id='label1'>My Label</label>\n"
            + "  <input type='checkbox' id='checkbox1'>\n" + "</body></html>";

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

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLabelElementTest.java

License:Apache License

/**
 * Tests that clicking the label by JavaScript does not change 'htmlFor' attribute in FF!!
 *
 * @throws Exception if the test fails//from  www .  ja v a 2s  .  c om
 */
@Test
@Alerts("true")
// in fact not used as JS alerts...
public void htmlFor_click() throws Exception {
    final String html = "<html><head><title>First</title><script>\n" + "function doTest() {\n"
            + "  document.getElementById('label1').htmlFor = 'checkbox1';\n" + "}\n"
            + "function delegateClick() {\n" + "  try {\n" + "    document.getElementById('label1').click();\n"
            + "  } catch (e) {}\n" + "}\n" + "</script></head><body onload='doTest()'>\n"
            + "<label id='label1'>My Label</label>\n" + "<input type='checkbox' id='checkbox1'><br>\n"
            + "<input type=button id='button1' value='Test' onclick='delegateClick()'>\n" + "</body></html>";

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

    assertEquals(getExpectedAlerts()[0], "" + checkbox.isSelected());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSelectElementTest.java

License:Apache License

/**
 * @throws Exception if an error occurs//from  w ww .  j av a 2 s . c  o m
 */
@Test
public void deselectMultiple() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html>\n" + "<body>\n" + "  <form name='f'>\n"
            + "    <select name='s1' multiple>\n" + "      <option name='option1'>One</option>\n"
            + "      <option id='it' name='option2' selected='true'>Two</option>\n"
            + "      <option name='option3'>Three</option>\n" + "    </select>\n" + "  </form>\n"
            + "</body></html>";

    final WebDriver webdriver = loadPageWithAlerts2(html);
    final WebElement firstOption = webdriver.findElement(By.id("it"));
    assertTrue(firstOption.isSelected());
    firstOption.click();
    assertFalse(firstOption.isSelected());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSelectElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from   w w w .  ja  v a 2 s  .  c o m
 */
@Test
public void deselectFromMultiple() throws Exception {
    final String html = "<html><body>\n" + "<select id='s' multiple>\n"
            + "  <option selected value='one'>One</option>\n" + "  <option value='two'>Two</option>\n"
            + "  <option selected value='three'>Three</option>\n" + "</select>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement multiSelect = driver.findElement(By.id("s"));
    final List<WebElement> options = multiSelect.findElements(By.tagName("option"));

    WebElement option = options.get(0);
    assertTrue(option.isSelected());
    option.click();
    assertFalse(option.isSelected());
    option.click();
    assertTrue(option.isSelected());

    option = options.get(2);
    assertTrue(option.isSelected());
}

From source file:com.ggasoftware.uitest.control.RadioButton.java

License:Open Source License

/**
 * Check if this RadioButton is not checked eat, do nothing otherwise.
 *
 * @return Parent instance/* www . ja  v  a  2  s.  c om*/
 */
public ParentPanel check() {
    doJAction("Check RadioButton", () -> {
        WebElement webEl = getWebElement();
        if (!webEl.isSelected()) {
            webEl.click();
        }
    });
    return super.parent;
}

From source file:com.github.wiselenium.elements.component.impl.MultiSelectImpl.java

License:Open Source License

@Override
public MultiSelect selectAll() {
    List<WebElement> selectOptions = this.getWrappedSelect().getOptions();
    for (WebElement option : selectOptions)
        if (!option.isSelected())
            option.click();/*from w  w w. j  av  a  2s  .c o  m*/
    return this;
}