Java Code Examples for org.openqa.selenium.interactions.Actions#sendKeys()

The following examples show how to use org.openqa.selenium.interactions.Actions#sendKeys() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SeleniumSpec.java    From bdt with Apache License 2.0 6 votes vote down vote up
/**
 * Delete or replace the text on a numbered {@code index} previously found element.
 *
 * @param index
 */
@When("^I delete the text '(.+?)' on the element on index '(\\d+)'( and replace it for '(.+?)')?$")
public void seleniumDelete(String text, Integer index, String replacement) {
    assertThat(this.commonspec, scenario, commonspec.getPreviousWebElements()).as("There are less found elements than required")
            .hasAtLeast(index);

    Actions actions = new Actions(commonspec.getDriver());
    actions.moveToElement(commonspec.getPreviousWebElements().getPreviousWebElements().get(index), (text.length() / 2), 0);
    for (int i = 0; i < (text.length() / 2); i++) {
        actions.sendKeys(Keys.ARROW_LEFT);
        actions.build().perform();
    }
    for (int i = 0; i < text.length(); i++) {
        actions.sendKeys(Keys.DELETE);
        actions.build().perform();
    }
    if (replacement != null && replacement.length() != 0) {
        actions.sendKeys(replacement);
        actions.build().perform();
    }
}
 
Example 2
Source File: SeleniumSpec.java    From bdt with Apache License 2.0 6 votes vote down vote up
/**
 * Type a {@code text} on an numbered {@code index} previously found element.
 *
 * @param nullablestring
 * @param index
 */
@When("I type {string} on the element on index '{int}'")
public void seleniumType(String nullablestring, Integer index) {
    String text = NullableString.transform(nullablestring);
    assertThat(this.commonspec, scenario, commonspec.getPreviousWebElements()).as("There are less found elements than required")
            .hasAtLeast(index);
    while (text.length() > 0) {
        Actions actions = new Actions(commonspec.getDriver());
        if (-1 == text.indexOf("\\n")) {
            actions.moveToElement(commonspec.getPreviousWebElements().getPreviousWebElements().get(index));
            actions.click();
            actions.sendKeys(text);
            actions.build().perform();
            text = "";
        } else {
            actions.moveToElement(commonspec.getPreviousWebElements().getPreviousWebElements().get(index));
            actions.click();
            actions.sendKeys(text.substring(0, text.indexOf("\\n")));
            actions.build().perform();
            text = text.substring(text.indexOf("\\n") + 2);
        }
    }
}
 
Example 3
Source File: Edition046_W3C_Keys.java    From appiumpro with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendKeysAction() {
    wait.until(ExpectedConditions.presenceOfElementLocated(loginScreen)).click();
    WebElement usernameField = driver.findElement(username);
    usernameField.click();
    Actions a = new Actions(driver);
    a.sendKeys("foo");
    a.perform();
    Assert.assertEquals("foo", usernameField.getText());
}
 
Example 4
Source File: ShortcutsIT.java    From flow with Apache License 2.0 5 votes vote down vote up
private void sendKeys(CharSequence... keys) {
    Actions actions = new Actions(driver);
    for (CharSequence keySeq : keys) {
        if (modifiers.contains(keySeq)) {
            actions.keyDown(keySeq);
        } else {
            actions.sendKeys(keySeq);
        }
    }
    actions.build().perform();
    // Implementation that worked for driver < 75.beta:
    // new Actions(driver).sendKeys(keys).build().perform();
    // if keys are not reset, alt will remain down and start flip-flopping
    resetKeys();
}
 
Example 5
Source File: LibraryControlCheckboxDefaultAft.java    From rice with Educational Community License v2.0 5 votes vote down vote up
protected void actionSendKeysArrowDown(String id) {
    Actions actions = new Actions(driver);
    actions.moveToElement(driver.findElement(By.id(id)));
    actions.click();
    actions.sendKeys(Keys.ARROW_DOWN);
    actions.build().perform();
}
 
Example 6
Source File: ElementAction.java    From PatatiumWebUi with Apache License 2.0 4 votes vote down vote up
public void type_action(Locator locator,String value)
{
	Actions actions =new Actions(driver);
	WebElement weElement=findElement(locator);
	actions.sendKeys(weElement, value);
}
 
Example 7
Source File: ElementAction.java    From PatatiumAppUi with GNU General Public License v2.0 4 votes vote down vote up
public void type_action(Locator locator,String value)
{
	Actions actions =new Actions(driver);
	WebElement weElement=findElement(locator);
	actions.sendKeys(weElement, value);
}
 
Example 8
Source File: SeleniumDriver.java    From jsflight with Apache License 2.0 4 votes vote down vote up
public void processKeyDownKeyUpEvents(WebDriver wd, JSONObject event) throws UnsupportedEncodingException
{
    WebElement element = findTargetWebElement(wd, event, UserScenario.getTargetForEvent(event));

    if (isNoOp(event, element))
    {
        return;
    }

    //TODO remove this when recording of cursor in text box is implemented
    if (skipKeyboardForElement(element))
    {
        LOG.warn("Keyboard processing for non empty Date is disabled");
        return;
    }

    if (!event.has(EventConstants.KEY_CODE) && !event.has(EventConstants.CHAR_CODE))
    {
        throw new IllegalStateException("Keydown/Keyup event don't have keyCode/charCode property");
    }

    Actions actions = new Actions(wd);

    SPECIAL_KEYS_MAPPING.keySet().forEach(property -> {
        if (event.getBoolean(property))
        {
            actions.keyDown(element, SPECIAL_KEYS_MAPPING.get(property));
        }
    });

    // Backward compatibility
    int code = event.getInt(EventConstants.CHAR_CODE);
    if (code == 0)
    {
        code = event.getInt(EventConstants.KEY_CODE);
    }
    switch (code)
    {
    case 8:
        actions.sendKeys(element, Keys.BACK_SPACE);
        break;
    case 27:
        actions.sendKeys(element, Keys.ESCAPE);
        break;
    case 46:
        actions.sendKeys(element, Keys.DELETE);
        break;
    case 13:
        actions.sendKeys(element, Keys.ENTER);
        break;
    case 37:
        actions.sendKeys(element, Keys.ARROW_LEFT);
        break;
    case 38:
        actions.sendKeys(element, Keys.ARROW_UP);
        break;
    case 39:
        actions.sendKeys(element, Keys.ARROW_RIGHT);
        break;
    case 40:
        actions.sendKeys(element, Keys.ARROW_DOWN);
        break;
    }

    SPECIAL_KEYS_MAPPING.keySet().forEach(property -> {
        if (event.getBoolean(property))
        {
            actions.keyUp(element, SPECIAL_KEYS_MAPPING.get(property));
        }
    });

    try
    {
        actions.perform();
    }
    catch (Exception ex)
    {
        // TODO Fix correctly
        LOG.error("Sending keys to and invisible element. must have JS workaround: " + ex.toString(), ex);
    }
}