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.activex.javascript.msxml.XMLSerializerTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*from ww  w.j a v a 2 s .  co  m*/
 */
@Test
@Alerts(DEFAULT = "no ActiveX", IE = {})
public void attributes() throws Exception {
    final String expectedString = getExpectedAlerts().length != 0 ? ""
            : "<document32attrib=\"attribValue\"><outer32attrib=\"attribValue\">"
                    + "<inner32attrib=\"attribValue\"/><meta32attrib=\"attribValue\"/></outer></document>1310";

    final String serializationText = "<document attrib=\"attribValue\">" + "<outer attrib=\"attribValue\">"
            + "<inner attrib=\"attribValue\"/>" + "<meta attrib=\"attribValue\"/>" + "</outer></document>";

    final WebDriver driver = loadPageWithAlerts2(constructPageContent(serializationText));
    final WebElement textArea = driver.findElement(By.id("myTextArea"));
    assertEquals(expectedString, textArea.getAttribute("value"));
}

From source file:com.gargoylesoftware.htmlunit.activex.javascript.msxml.XMLSerializerTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*from w  w w . jav  a 2 s  . c  o  m*/
 */
@Test
@Alerts(DEFAULT = "no ActiveX", IE = {})
@NotYetImplemented(IE)
// so far we are not able to add the XML header
public void htmlAttributes() throws Exception {
    final String expectedString = getExpectedAlerts().length != 0 ? ""
            : "<?xml32version=\"1.0\"?>1310<html32xmlns=\"http://www.w3.org/1999/xhtml\">"
                    + "<head><title>html</title></head>" + "<body32id=\"bodyId\">"
                    + "<span32class=\"spanClass\">foo</span>" + "</body>" + "</html>1310";

    final String serializationText = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
            + "<html xmlns=\"http://www.w3.org/1999/xhtml\">" + "<head><title>html</title></head>"
            + "<body id=\"bodyId\">" + "<span class=\"spanClass\">foo</span>" + "</body>" + "</html>";

    final WebDriver driver = loadPageWithAlerts2(constructPageContent(serializationText));
    final WebElement textArea = driver.findElement(By.id("myTextArea"));
    assertEquals(expectedString, textArea.getAttribute("value"));
}

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

License:Apache License

/**
 * According to the HTML spec, the default type for a button is "submit".
 * IE is different than the HTML spec and has a default type of "button".
 * @throws Exception if the test fails/*  w  w  w . j  av a  2  s.c  o m*/
 */
@Test
@Alerts({ "submit", "1", "button-pushme", "Second" })
public void defaultButtonType_StandardsCompliantBrowser() throws Exception {
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "<form id='form1' action='"
            + URL_SECOND + "' method='post'>\n"
            + "  <button name='button' id='button' value='pushme'>PushMe</button>\n" + "</form></body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body'></body></html>";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);

    final WebDriver driver = loadPage2(firstContent);
    final WebElement button = driver.findElement(By.id("button"));

    assertEquals(getExpectedAlerts()[0], button.getAttribute("type"));

    button.click();

    final List<NameValuePair> params = getMockWebConnection().getLastParameters();
    assertEquals(getExpectedAlerts()[1], "" + params.size());

    if (params.size() > 0) {
        assertEquals(getExpectedAlerts()[2], params.get(0).getName() + "-" + params.get(0).getValue());
    }
    assertEquals(getExpectedAlerts()[3], driver.getTitle());
}

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

License:Apache License

/**
 * @throws Exception if the test fails// w  ww . j av  a  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='email' 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.HtmlFileInputTest.java

License:Apache License

/**
 * @throws Exception if an error occurs//from www.  ja va  2s  .  co m
 */
@Test
@Alerts(DEFAULT = "C:\\fakepath\\pom.xml", FF = "pom.xml", IE = "PATH")
public void getAttribute() throws Exception {
    final String html = "<html><body>\n" + "  <input type='file' id='f'>\n" + "</body></html>";

    final String absolutePath = new File("pom.xml").getAbsolutePath();

    final WebDriver driver = loadPage2(html);
    final WebElement e = driver.findElement(By.id("f"));
    e.sendKeys(absolutePath);
    final String[] expectedAlerts = getExpectedAlerts();
    expectedAlerts[0] = expectedAlerts[0].replace("PATH", absolutePath);
    assertEquals(expectedAlerts[0], e.getAttribute("value"));
}

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

License:Apache License

private void testClickEventSequence(final String input, final boolean onClickRetFalse) throws Exception {
    final String events = " onmousedown=\"log('mousedown')\" onmouseup=\"log('mouseup')\" "
            + "onfocus=\"log('onfocus')\" onchange=\"log('onchange')\" " + "onclick=\"log('onclick')\"";
    String tag = StringUtils.replaceOnce(input, ">", events + ">");
    if (onClickRetFalse) {
        tag = StringUtils.replaceOnce(tag, "onclick')", "onclick');return false;");
    }/*from  ww w . j  av  a  2 s.  c  o  m*/

    final String html = "<html><head>\n" + "<script>\n" + "  function log(x) {\n"
            + "    document.getElementById('log_').value += x + '; ';\n" + "  }\n" + "</script>\n"

            + "</head>\n<body>\n" + "<form>\n" + "  <textarea id='log_' rows='4' cols='50'></textarea>\n" + tag
            + "\n" + "  <input type='text' id='next'>\n" + "</form>\n"

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

    final List<String> alerts = new LinkedList<>();

    final WebDriver driver = loadPage2(html);
    final WebElement log = driver.findElement(By.id("log_"));

    driver.findElement(By.id(TEST_ID)).click();
    alerts.add(log.getAttribute("value").trim());

    log.clear();
    driver.findElement(By.id("next")).click();

    alerts.add(log.getAttribute("value").trim());
    assertEquals(getExpectedAlerts(), alerts);
}

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

License:Apache License

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

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

License:Apache License

/**
 * @throws Exception if the test fails//from   www .j a v a2s. c o m
 */
@Test
public void typeWhileDisabled() throws Exception {
    final String html = "<html><body><input type='number' 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.HtmlNumberInputTest.java

License:Apache License

/**
 * @throws Exception if an error occurs//  w w w .  j  ava2s .  com
 */
@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='number' id='p'></input>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));
    p.sendKeys("1234");
    assertEquals("123", p.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs// w  ww  .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='number' id='p'></input>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));
    p.sendKeys("1234");
    assertEquals("123", p.getAttribute("value"));
}