Example usage for org.openqa.selenium WebElement getAttribute

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

Introduction

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

Prototype

String getAttribute(String name);

Source Link

Document

Get the value of the given attribute of the element.

Usage

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

License:Apache License

/**
 * @throws Exception if the test fails/*from www  .  j av a2 s  .c  o m*/
 */
@Test
@Alerts("foo")
public void onChangeSetByJavaScript() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title>\n"
            + "</head><body>\n" + "<p>hello world</p>\n" + "<form name='form1'>\n"
            + "  <input type='text' name='text1' id='text1'>\n"
            + "<input name='myButton' type='button' onclick='document.form1.text1.value=\"from button\"'>\n"
            + "</form>\n" + "<script>\n"
            + "document.getElementById('text1').onchange = function(event) { alert(this.value) };\n"
            + "</script>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final WebElement textinput = driver.findElement(By.name("text1"));
    textinput.sendKeys("foo");
    final WebElement button = driver.findElement(By.name("myButton"));
    button.click();
    verifyAlerts(driver, getExpectedAlerts());
    Thread.sleep(10);
    assertEquals("from button", textinput.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from   ww  w  .  j  av  a 2s . c o  m
 */
@Test
public void typeMaxLength() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><body>\n" + "<form>\n"
            + "<input type='text' id='text1' maxlength='5'/>\n"
            + "<input type='password' id='password1' maxlength='6'/>\n" + "</form></body></html>";

    final WebDriver webDriver = loadPage2(html);
    final WebElement textField = webDriver.findElement(By.id("text1"));
    textField.sendKeys("123456789");
    assertEquals("12345", textField.getAttribute("value"));
    textField.sendKeys("\b7");
    assertEquals("12347", textField.getAttribute("value"));

    final WebElement passwordField = webDriver.findElement(By.id("password1"));
    passwordField.sendKeys("123456789");
    assertEquals("123456", passwordField.getAttribute("value"));
    passwordField.sendKeys("\b7");
    assertEquals("123457", passwordField.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from  w  ww.  j av a  2 s .c o m*/
 */
@Test
public void typeMaxLengthZero() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><body>\n" + "<form>\n"
            + "<input type='text' id='text1' maxlength='0'/>\n"
            + "<input type='password' id='password1' maxlength='0'/>\n" + "</form></body></html>";

    final WebDriver webDriver = loadPage2(html);
    final WebElement textField = webDriver.findElement(By.id("text1"));
    textField.sendKeys("123456789");
    assertEquals("", textField.getAttribute("value"));
    textField.sendKeys("\b7");
    assertEquals("", textField.getAttribute("value"));

    final WebElement passwordField = webDriver.findElement(By.id("password1"));
    passwordField.sendKeys("123456789");
    assertEquals("", passwordField.getAttribute("value"));
    passwordField.sendKeys("\b7");
    assertEquals("", passwordField.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   w w  w.  j av a 2s .  com*/
 */
@Test
public void typeMaxLengthAndBlanks() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><body>\n" + "<form>\n"
            + "<input type='text' id='text1' maxlength=' 2 '/>\n"
            + "<input type='password' id='password1' maxlength='    4  '/>\n" + "</form></body></html>";

    final WebDriver webDriver = loadPage2(html);
    final WebElement textField = webDriver.findElement(By.id("text1"));
    textField.sendKeys("123456789");
    assertEquals("12", textField.getAttribute("value"));
    textField.sendKeys("\b7");
    assertEquals("17", textField.getAttribute("value"));

    final WebElement passwordField = webDriver.findElement(By.id("password1"));
    passwordField.sendKeys("123456789");
    assertEquals("1234", passwordField.getAttribute("value"));
    passwordField.sendKeys("\b7");
    assertEquals("1237", passwordField.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from  www . jav a2 s  . c  o  m*/
 */
@Test
@Alerts("SELECT;")
@BuggyWebDriver
//https://bugs.chromium.org/p/chromedriver/issues/detail?id=1352
public void clickSelect() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title><script>\n"
            + "  function log(x) {\n" + "    document.getElementById('log_').value += x + '; ';\n" + "  }\n"

            + "  function init() {\n" + "    var s = document.getElementById('s');\n"
            + "    s.addEventListener('click', handle, false);\n" + "  }\n"

            + "  function handle(event) {\n" + "    if (event.target) {\n"
            + "      log(event.target.nodeName);\n" + "    } else {\n"
            + "      log(event.srcElement.nodeName);\n" + "    }\n" + "  }\n" + "</script></head>\n"

            + "<body onload='init()'>\n" + "<form>\n" + "  <textarea id='log_' rows='4' cols='50'></textarea>\n"
            + "  <select id='s' size='7'>\n" + "    <option value='opt-a'>A</option>\n"
            + "    <option id='opt-b' value='b'>B</option>\n" + "    <option value='opt-c'>C</option>\n"
            + "  </select>\n" + "</form>\n" + "</body></html>";

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

    final List<String> alerts = new LinkedList<>();
    final WebElement log = driver.findElement(By.id("log_"));
    alerts.add(log.getAttribute("value").trim());
    assertEquals(getExpectedAlerts(), alerts);
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*  w  w w  .ja  va 2 s  .co  m*/
 */
@Test
@Alerts(DEFAULT = "opt-a; opt-b", CHROME = "opt-b")
@BuggyWebDriver
@NotYetImplemented
//TODO: Needs further investigation of clicking an option without clicking the select
// See the first comment in http://code.google.com/p/selenium/issues/detail?id=2131#c1
// Additionally, FF and Chrome drivers look buggy as they don't allow to capture
// what happens when running the test manually in the browser.
public void click2() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title><script>\n"
            + "  function log(x) {\n" + "    document.getElementById('log_').value += x + '; ';\n" + "  }\n"

            + "  function init() {\n" + "    s = document.getElementById('s');\n"
            + "    s.addEventListener('click', handle, false);\n" + "  }\n"

            + "  function handle(event) {\n" + "    log(s.options[s.selectedIndex].value);\n" + "  }\n"
            + "</script></head>\n"

            + "<body onload='init()'>\n" + "<form>\n" + "  <textarea id='log_' rows='4' cols='50'></textarea>\n"
            + "  <select id='s'>\n" + "    <option value='opt-a'>A</option>\n"
            + "    <option id='opt-b' value='b'>B</option>\n" + "    <option value='opt-c'>C</option>\n"
            + "  </select>\n" + "</form>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("s")).click();
    driver.findElement(By.id("opt-b")).click();

    final List<String> alerts = new LinkedList<>();
    final WebElement log = driver.findElement(By.id("log_"));
    alerts.add(log.getAttribute("value").trim());
    assertEquals(getExpectedAlerts(), alerts);
}

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

License:Apache License

/**
 * Test for the right event sequence when clicking.
 *
 * @throws Exception if the test fails/*w  ww .  ja v  a2 s  .  co  m*/
 */
@Test
@Alerts(DEFAULT = "onchange-select; onclick-option; onclick-select;", IE = "onchange-select; onclick-select;")
@BuggyWebDriver({ CHROME, FF })
public void clickOptionEventSequence1() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head>\n" + "<script>\n"
            + "  function log(x) {\n" + "    document.getElementById('log_').value += x + '; ';\n" + "  }\n"
            + "</script></head>\n"

            + "<body>\n" + "<form>\n" + "  <textarea id='log_' rows='4' cols='50'></textarea>\n"
            + "  <select id='s' size='2' onclick=\"log('onclick-select')\""
            + " onchange=\"log('onchange-select')\">\n"
            + "    <option id='clickId' value='a' onclick=\"log('onclick-option')\""
            + " onchange=\"log('onchange-option')\">A</option>\n" + "  </select>\n" + "</form>\n"

            + "</body></html>";

    final WebDriver driver = loadPage2(html);

    driver.findElement(By.id("clickId")).click();

    final List<String> alerts = new LinkedList<>();
    final WebElement log = driver.findElement(By.id("log_"));
    alerts.add(log.getAttribute("value").trim());
    assertEquals(getExpectedAlerts(), alerts);
}

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

License:Apache License

/**
 * Test for the right event sequence when clicking.
 *
 * @throws Exception if the test fails//from www. j a  va 2 s . c om
 */
@Test
@Alerts(DEFAULT = "change-SELECT; click-OPTION; click-OPTION;", IE = "change-SELECT; click-SELECT;")
@BuggyWebDriver({ CHROME, FF })
public void clickOptionEventSequence2() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head>\n" + "<script>\n"
            + "  function log(x) {\n" + "    document.getElementById('log_').value += x + '; ';\n" + "  }\n"

            + "  function init() {\n" + "    var s = document.getElementById('s');\n"
            + "    var o = document.getElementById('clickId');\n"
            + "    s.addEventListener('click', handle, false);\n"
            + "    s.addEventListener('change', handle, false);\n"
            + "    o.addEventListener('click', handle, false);\n"
            + "    o.addEventListener('change', handle, false);\n" + "  }\n"

            + "  function handle(event) {\n" + "    if (event.target) {\n"
            + "      log(event.type + '-' + event.target.nodeName);\n" + "    } else {\n"
            + "      log(event.type + '-' + event.srcElement.nodeName);\n" + "    }\n" + "  }\n"
            + "</script></head>\n"

            + "<body onload='init()'>\n" + "<form>\n" + "  <textarea id='log_' rows='4' cols='50'></textarea>\n"
            + "  <select id='s' size='2' >\n" + "    <option id='clickId' value='a' >A</option>\n"
            + "  </select>\n" + "</form>\n"

            + "</body></html>";

    final WebDriver driver = loadPage2(html);

    driver.findElement(By.id("clickId")).click();

    final List<String> alerts = new LinkedList<>();
    final WebElement log = driver.findElement(By.id("log_"));
    alerts.add(log.getAttribute("value").trim());
    assertEquals(getExpectedAlerts(), alerts);
}

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

License:Apache License

/**
 * Test for the right event sequence when clicking.
 *
 * @throws Exception if the test fails//from ww  w  .  ja va  2 s .c o m
 */
@Test
@Alerts(DEFAULT = "onchange-select; change-SELECT; onclick-option; click-OPTION; onclick-select; click-OPTION;", IE = "onchange-select; change-SELECT; onclick-select; click-SELECT;")
@BuggyWebDriver({ CHROME, FF })
public void clickOptionEventSequence3() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head>\n" + "<script>\n"
            + "  function log(x) {\n" + "    document.getElementById('log_').value += x + '; ';\n" + "  }\n"

            + "  function init() {\n" + "    var s = document.getElementById('s');\n"
            + "    var o = document.getElementById('clickId');\n"
            + "    s.addEventListener('click', handle, false);\n"
            + "    s.addEventListener('change', handle, false);\n"
            + "    o.addEventListener('click', handle, false);\n"
            + "    o.addEventListener('change', handle, false);\n" + "  }\n"

            + "  function handle(event) {\n" + "    if (event.target) {\n"
            + "      log(event.type + '-' + event.target.nodeName);\n" + "    } else {\n"
            + "      log(event.type + '-' + event.srcElement.nodeName);\n" + "    }\n" + "  }\n"
            + "</script></head>\n"

            + "<body onload='init()'>\n" + "<form>\n" + "  <textarea id='log_' rows='4' cols='50'></textarea>\n"
            + "  <select id='s' size='2' onclick=\"log('onclick-select')\""
            + " onchange=\"log('onchange-select')\">\n"
            + "    <option id='clickId' value='a' onclick=\"log('onclick-option')\""
            + " onchange=\"log('onchange-option')\">A</option>\n" + "  </select>\n" + "</form>\n"

            + "</body></html>";

    final WebDriver driver = loadPage2(html);

    driver.findElement(By.id("clickId")).click();

    final List<String> alerts = new LinkedList<>();
    final WebElement log = driver.findElement(By.id("log_"));
    alerts.add(log.getAttribute("value").trim());
    assertEquals(getExpectedAlerts(), alerts);
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.WebSocketTest.java

License:Apache License

/**
 * @throws Exception if the test fails// ww w .  j  a v a 2  s  .  c o m
 */
@Test
@Alerts(DEFAULT = { "onOpenListener", "onOpen", "onMessageTextListener", "server_text", "ws://localhost:12345",
        "", "null", "onMessageText", "server_text", "ws://localhost:12345", "", "null",
        "onMessageBinaryListener", "[object ArrayBuffer]", "ws://localhost:12345", "", "null",
        "onMessageBinary", "[object ArrayBuffer]", "ws://localhost:12345", "", "null",
        "onCloseListener code: 1000  wasClean: true", "onClose code: 1000  wasClean: true" }, IE = {
                "onOpenListener", "onOpen", "onMessageTextListener", "server_text", "", "undefined", "null",
                "onMessageText", "server_text", "", "undefined", "null", "onMessageBinaryListener",
                "[object ArrayBuffer]", "", "undefined", "null", "onMessageBinary", "[object ArrayBuffer]", "",
                "undefined", "null", "onCloseListener code: 1005  wasClean: true",
                "onClose code: 1005  wasClean: true" })
@NotYetImplemented(IE)
public void events() throws Exception {
    startWebServer("src/test/resources/com/gargoylesoftware/htmlunit/javascript/host", null, null,
            new EventsWebSocketHandler());
    final WebDriver driver = getWebDriver();
    driver.get(URL_FIRST + "WebSocketTest_events.html");

    final WebElement logElement = driver.findElement(By.id("log"));
    int counter = 0;
    String text;
    do {
        Thread.sleep(100);

        text = logElement.getAttribute("value").trim().replaceAll("\r", "");
    } while (text.length() > 0 && counter++ < 10);

    assertEquals(String.join("\n", getExpectedAlerts()), text);
    stopWebServers();
}