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.html.HtmlPage3Test.java

License:Apache License

/**
 * Test for 3306491./*from  w w w. j  a v  a 2s  .  c o m*/
 * @throws Exception if an error occurs
 */
@Test
public void formElementCreatedFromJavascript() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "<script type='text/javascript'>\n"
            + "  function modifyForm() {\n" + "    var myForm = document.forms['test_form'];\n"
            + "    var el = document.createElement('input');\n" + "    el.setAttribute('addedBy','js');\n"
            + "    el.name = 'myHiddenField';\n" + "    el.value = 'myValue';\n" + "    el.type = 'hidden';\n"
            + "    myForm.appendChild(el);\n" + "}\n" + "</script>\n" + "</head>\n"
            + "<body onLoad='modifyForm()'>\n"
            + "  <form id='test_form' action='http://www.sourceforge.com/' method='post'>\n"
            + "    <input type='submit' value='click'/>\n" + "  </form>\n" + "</body>\n" + "</html>";

    final WebDriver driver = loadPage2(html);
    final List<WebElement> elements = driver.findElements(By.xpath("//*"));
    assertEquals(7, elements.size());

    assertEquals("html", elements.get(0).getTagName());
    assertEquals("head", elements.get(1).getTagName());
    assertEquals("script", elements.get(2).getTagName());
    assertEquals("body", elements.get(3).getTagName());
    assertEquals("form", elements.get(4).getTagName());
    assertEquals("input", elements.get(5).getTagName());

    final WebElement input = elements.get(6);
    assertEquals("input", input.getTagName());
    assertEquals("myHiddenField", input.getAttribute("name"));
    assertEquals("js", input.getAttribute("addedBy"));
    assertEquals("js", input.getAttribute("addedby"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails//  w  w  w .j  a v  a2  s . c  om
 */
@Test
public void getInputByName() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title></head>\n" + "<body>\n" + "<p>hello world</p>\n"
            + "<form id='form1' action='/formSubmit' method='post'>\n"
            + "  <input type='text' NAME='textInput1' value='textInput1'/>\n"
            + "  <input type='text' name='textInput2' value='textInput2'/>\n"
            + "  <input type='hidden' name='hidden1' value='hidden1'/>\n"
            + "  <input type='submit' name='submitInput1' value='push me'/>\n" + "</form>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);

    final WebElement form = driver.findElement(By.id("form1"));
    final WebElement input = form.findElement(By.name("textInput1"));
    assertEquals("name", "textInput1", input.getAttribute("name"));

    assertEquals("value", "textInput1", input.getAttribute("value"));
    assertEquals("type", "text", input.getAttribute("type"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from w w  w  . j av a  2  s.  c  om*/
 */
@Test
public void type() throws Exception {
    final String html = "<html><head></head><body><input type='password' id='p'/></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));
    p.sendKeys("abc");
    assertEquals("abc", p.getAttribute("value"));
    p.sendKeys("\b");
    assertEquals("ab", p.getAttribute("value"));
    p.sendKeys("\b");
    assertEquals("a", p.getAttribute("value"));
    p.sendKeys("\b");
    assertEquals("", p.getAttribute("value"));
    p.sendKeys("\b");
    assertEquals("", p.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from  w  w  w .jav  a  2  s  . co  m
 */
@Test
public void typeWhileDisabled() throws Exception {
    final String html = "<html><body><input type='password' id='p' disabled='disabled'/></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));
    try {
        p.sendKeys("abc");
        fail();
    } catch (final InvalidElementStateException e) {
        // as expected
    }
    assertEquals("", p.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//w ww. j  a  v  a 2s . co m
 */
@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/*from   w w w.j a  v  a  2  s  . c  o m*/
 */
@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.HtmlSearchInputTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from   ww w. ja v  a2s  .c om
 */
@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.HtmlTelInputTest.java

License:Apache License

/**
 * @throws Exception if the test fails// www  . java  2s  .  c  om
 */
@Test
public void typing() throws Exception {
    final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "  <input type='tel' id='foo'>\n" + "</form></body></html>";

    final WebDriver driver = loadPage2(htmlContent);

    final WebElement input = driver.findElement(By.id("foo"));
    input.sendKeys("hello");
    assertEquals("hello", input.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from  w ww .  j  a  va  2  s  . c o  m*/
 */
@Test
public void type() throws Exception {
    final String html = "<html><head></head><body><input type='text' id='t'/></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement t = driver.findElement(By.id("t"));
    t.sendKeys("abc");
    assertEquals("abc", t.getAttribute("value"));
    t.sendKeys("\b");
    assertEquals("ab", t.getAttribute("value"));
    t.sendKeys("\b");
    assertEquals("a", t.getAttribute("value"));
    t.sendKeys("\b");
    assertEquals("", t.getAttribute("value"));
    t.sendKeys("\b");
    assertEquals("", t.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from  ww w . ja v a 2s .  c o m
 */
@Test
public void typeWhileDisabled() throws Exception {
    final String html = "<html><body><input type='text' id='p' disabled='disabled'/></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));
    try {
        p.sendKeys("abc");
        fail();
    } catch (final InvalidElementStateException e) {
        // as expected
    }
    assertEquals("", p.getAttribute("value"));
}