Example usage for org.openqa.selenium.interactions Actions sendKeys

List of usage examples for org.openqa.selenium.interactions Actions sendKeys

Introduction

In this page you can find the example usage for org.openqa.selenium.interactions Actions sendKeys.

Prototype

public Actions sendKeys(CharSequence... keys) 

Source Link

Document

Sends keys to the active element.

Usage

From source file:org.zanata.page.AbstractPage.java

License:Open Source License

/**
 * Enter text into an element.//from  www  .  ja  va 2 s. c  o  m
 *
 * Waits for notifications to be dismissed and element to be ready and visible before entering
 * the text.
 * If no checking is performed, the resulting screenshot may not be accurate.
 * @param element element to pass text to
 * @param text text to be entered
 * @param clear clear the element's text before entering new text
 * @param inject use sendKeys rather than the Actions chain (direct injection)
 * @param check check the 'value' attribute for success, and accurate screenshot delay
 */
public void enterText(final WebElement element, final String text, boolean clear, boolean inject,
        final boolean check) {
    removeNotifications();
    waitForNotificationsGone();
    scrollIntoView(element);
    triggerScreenshot("_pretext");
    waitForAMoment().withMessage("editable: " + element.toString())
            .until(ExpectedConditions.elementToBeClickable(element));
    if (inject) {
        if (clear) {
            element.clear();
        }
        element.sendKeys(text);
    } else {
        Actions enterTextAction = new Actions(getDriver()).moveToElement(element);
        enterTextAction = enterTextAction.click();
        // Fields can 'blur' on click
        waitForPageSilence();
        if (clear) {
            enterTextAction = enterTextAction.sendKeys(Keys.chord(Keys.CONTROL, "a")).sendKeys(Keys.DELETE);
            // Fields can 'blur' on clear
            waitForPageSilence();
        }
        enterTextAction.sendKeys(text).perform();
    }
    if (check) {
        waitForAMoment().withMessage("Text equal to entered").until((Predicate<WebDriver>) webDriver -> {
            String foundText = element.getAttribute("value");
            if (!text.equals(foundText)) {
                log.info("Found: {}", foundText);
                triggerScreenshot("_textWaiting");
                return false;
            }
            return true;
        });
    } else {
        log.info("Not checking text entered");
    }
    triggerScreenshot("_text");
}

From source file:renascere.Renascere.java

License:Open Source License

/**
 * @Description Method that send an ACTION to a selected object
 *///from   w  w  w.ja v  a 2s .co  m
public static void performAction(tAction uiAction, String sValue, WebDriver driver, WebElement wElement) {
    try {
        Actions actions = new Actions(driver);
        actions.moveToElement(wElement);
        actions.click();
        pauseExecution(Global.fTimeQSecond);
        switch (uiAction) {
        case ENTER:
            actions.sendKeys(sValue);
            actions.build().perform();
            break;
        case SELECT:
            actions.sendKeys(sValue);
            actions.build().perform();
            pauseExecution(Global.fTimeQSecond);
            actions.sendKeys(Keys.ENTER);
            actions.build().perform();
            break;
        case CHOOSE:
            actions.sendKeys(sValue);
            actions.sendKeys(Keys.ENTER);
            actions.build().perform();
            pauseExecution(Global.fTimeQSecond);
            break;
        default:
            break;
        }
        return;
    } catch (Exception e) {
        frmError("performing action " + uiAction + " with: " + sValue, e.getMessage(), severity.HIGH);
    }
}