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.selenium.TypingTest.java

License:Apache License

/**
 * A test./*w  w w. ja  v  a2 s. c o  m*/
 */
@Test
public void deleteAndBackspaceKeys() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement element = driver.findElement(By.id("keyReporter"));

    element.sendKeys("abcdefghi");
    assertThat(element.getAttribute("value"), is("abcdefghi"));

    element.sendKeys(Keys.LEFT, Keys.LEFT, Keys.DELETE);
    assertThat(element.getAttribute("value"), is("abcdefgi"));

    element.sendKeys(Keys.LEFT, Keys.LEFT, Keys.BACK_SPACE);
    assertThat(element.getAttribute("value"), is("abcdfgi"));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test.//from ww  w  .  jav a2 s .c  o m
 */
@Test
public void specialSpaceKeys() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement element = driver.findElement(By.id("keyReporter"));

    element.sendKeys("abcd" + Keys.SPACE + "fgh" + Keys.SPACE + "ij");
    assertThat(element.getAttribute("value"), is("abcd fgh ij"));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test.//from  w w w  .java2s  .  c  om
 */
@Test
public void numberpadKeys() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement element = driver.findElement(By.id("keyReporter"));

    element.sendKeys("abcd" + Keys.MULTIPLY + Keys.SUBTRACT + Keys.ADD + Keys.DECIMAL + Keys.SEPARATOR
            + Keys.NUMPAD0 + Keys.NUMPAD9 + Keys.ADD + Keys.SEMICOLON + Keys.EQUALS + Keys.DIVIDE + Keys.NUMPAD3
            + "abcd");
    assertThat(element.getAttribute("value"), is("abcd*-+.,09+;=/3abcd"));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test./*  w  w  w .  j a v a 2  s  . co  m*/
 */
@Test
public void shiftSelectionDeletes() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement element = driver.findElement(By.id("keyReporter"));

    element.sendKeys("abcd efgh");
    assertThat(element.getAttribute("value"), is("abcd efgh"));

    element.sendKeys(Keys.SHIFT, Keys.LEFT, Keys.LEFT, Keys.LEFT);
    element.sendKeys(Keys.DELETE);
    assertThat(element.getAttribute("value"), is("abcd e"));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test.//w  w w .  j av  a  2s.co m
 */
@Test
public void chordControlHomeShiftEndDelete() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement result = driver.findElement(By.id("result"));
    final WebElement element = driver.findElement(By.id("keyReporter"));

    element.sendKeys("!\"#$%&'()*+,-./0123456789:;<=>?@ ABCDEFG");

    element.sendKeys(Keys.HOME);
    element.sendKeys("" + Keys.SHIFT + Keys.END);
    assertThat(result.getText(), containsString(" up: 16"));

    element.sendKeys(Keys.DELETE);
    assertThat(element.getAttribute("value"), is(""));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test./*from  w ww.jav  a  2s.  com*/
 */
@Test
public void chordReveseShiftHomeSelectionDeletes() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement result = driver.findElement(By.id("result"));
    final WebElement element = driver.findElement(By.id("keyReporter"));

    element.sendKeys("done" + Keys.HOME);
    assertThat(element.getAttribute("value"), is("done"));

    element.sendKeys("" + Keys.SHIFT + "ALL " + Keys.HOME);
    assertThat(element.getAttribute("value"), is("ALL done"));

    element.sendKeys(Keys.DELETE);
    assertThat(element.getAttribute("value"), is("done"));

    element.sendKeys("" + Keys.END + Keys.SHIFT + Keys.HOME);
    assertThat(element.getAttribute("value"), is("done"));
    // Note: trailing SHIFT up here
    assertThat(result.getText().trim(), containsString(" up: 16"));

    element.sendKeys("" + Keys.DELETE);
    assertThat(element.getAttribute("value"), is(""));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test.//from  w  w  w.java2  s  .  c om
 */
@Test
@Alerts(DEFAULT = { "keydown (target) keyup (target) keyup (body)",
        "keydown (target) keyup (target) keyup (body) keydown (target) a pressed; removing" }, CHROME = {
                "keydown (target) keyup (target) keyup (body)",
                "keydown (target) keyup (target) keyup (body) keydown (target) a pressed; removing keyup (body)" })
public void canSafelyTypeOnElementThatIsRemovedFromTheDomOnKeyPress() {
    final WebDriver driver = getWebDriver("/key_tests/remove_on_keypress.html");

    final WebElement input = driver.findElement(By.id("target"));
    final WebElement log = driver.findElement(By.id("log"));

    assertEquals("", log.getAttribute("value"));

    input.sendKeys("b");
    assertEquals(getExpectedAlerts()[0], getValueText(log).replace('\n', ' '));

    input.sendKeys("a");

    // Some drivers (IE, Firefox) do not always generate the final keyup event since the element
    // is removed from the DOM in response to the keypress (note, this is a product of how events
    // are generated and does not match actual user behavior).
    assertEquals(getExpectedAlerts()[1], getValueText(log).replace('\n', ' '));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

private static String getValueText(final WebElement el) {
    // Standardize on \n and strip any trailing whitespace.
    return el.getAttribute("value").replace("\r\n", "\n").trim();
}

From source file:com.github.codewrs.selenium.FindElementAdvanced.java

License:Open Source License

/**
 * Find all elements within the current page using the given mechanism.
 * Returns WebElement by matching the value of the attribute.
 * @param driver WebDriver instance.// w  w w .j a va 2s . c o  m
 * @param by The locating mechanism.
 * @param attributeToSearch The attribute to locate. 
 * @param valueToMatch Text to match with attribute's value.
 * @param wait Explicit Wait Time.
 * @return WebElement or null if nothing matches.
 */
protected WebElement findElementOnList(WebDriver driver, By by, String attributeToSearch, String valueToMatch,
        WebDriverWait wait) {
    try {
        List<WebElement> elementList = driver.findElements(by);
        wait.until(ExpectedConditions.visibilityOfAllElements(elementList));
        int listSize = elementList.size();
        System.out.println("The number of WebElements found : " + listSize);
        if (listSize > 0) {
            for (WebElement ele : elementList) {
                if (ele.getAttribute(attributeToSearch) != null) {
                    if (ele.getAttribute(attributeToSearch).contentEquals(valueToMatch)) {
                        System.out.println("The returned WebElement text : " + ele.getText());
                        return ele;
                    }
                }
            }
        }
    } catch (NoSuchElementException e) {
        System.out.println("No Element found.");
    }
    System.out.println("No Element found.");
    return null;
}

From source file:com.github.codewrs.selenium.FindElementAdvanced.java

License:Open Source License

/**
 * Find all the elements within the WebElement using the given mechanism.
 * Returns WebElement by matching with the value of the attribute.
 * @param webElement Base WebElement to find other WebElements.
 * @param by The locating mechanism./*w w w  . ja v  a 2 s  .  c om*/
 * @param attributeToSearch The attribute to locate. 
 * @param valueToMatch Text to match with attribute's value.
 * @param wait Explicit Wait Time.
 * @return WebElement or null if nothing matches.
 */
protected WebElement findElementOnList(WebElement webElement, By by, String attributeToSearch,
        String valueToMatch, WebDriverWait wait) {
    try {
        List<WebElement> elementList = webElement.findElements(by);
        wait.until(ExpectedConditions.visibilityOfAllElements(elementList));
        int listSize = elementList.size();
        System.out.println("The number of WebElements found : " + listSize);
        if (listSize > 0) {
            for (WebElement ele : elementList) {
                if (ele.getAttribute(attributeToSearch) != null) {
                    if (ele.getAttribute(attributeToSearch).contentEquals(valueToMatch)) {
                        System.out.println("The returned WebElement text : " + ele.getText());
                        return ele;
                    }
                }
            }
        }
    } catch (NoSuchElementException e) {
        System.out.println("No Element found.");
    }
    System.out.println("No Element found.");
    return null;
}