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(WebElement target, CharSequence... keys) 

Source Link

Document

Equivalent to calling: Actions.click(element).sendKeys(keysToSend). This method is different from WebElement#sendKeys(CharSequence) - see #sendKeys(CharSequence) for details how.

Usage

From source file:com.induscorp.prime.testing.ui.core.objects.DOMObjectValidator.java

License:Open Source License

public void typeText(String text, NewTextLocation location, int numRetries) {
    String newtext;//  ww w  . j a  v  a  2  s . co  m
    Actions actions;
    for (int i = 0; i < 5; i++) {
        try {
            WebElement webElem = findElement(numRetries);
            ClipboardUtil.clearContents();

            try {
                actions = new Actions(browser.getSeleniumWebDriver());
                actions.sendKeys(webElem, Keys.CONTROL + "ax").build().perform();
            } finally {
                actions = new Actions(browser.getSeleniumWebDriver());
                actions.keyUp(webElem, Keys.CONTROL).build().perform();
            }

            String existingText = ClipboardUtil.getContents();
            ClipboardUtil.clearContents();

            switch (location) {
            case start:
                newtext = text + existingText;
                actions = new Actions(browser.getSeleniumWebDriver());
                actions.sendKeys(webElem, newtext).build().perform();
                break;
            case end:
                newtext = existingText + text;
                actions = new Actions(browser.getSeleniumWebDriver());
                actions.sendKeys(webElem, newtext).build().perform();
                break;
            case replace:
                actions = new Actions(browser.getSeleniumWebDriver());
                actions.sendKeys(webElem, text).build().perform();
                break;
            }

            break;
        } catch (MoveTargetOutOfBoundsException | ElementNotVisibleException ex) {
            browser.waitForSeconds(2);
        }
    }
}

From source file:com.liferay.faces.bridge.test.integration.demo.applicant.ApplicantTesterBase.java

License:Open Source License

protected final void sendKeysTabAndWaitForRerender(BrowserDriver browserDriver, String elementXpath,
        CharSequence... keys) {// w  w w  . j a  v a  2s . com

    Actions actions = browserDriver.createActions(elementXpath);
    WebElement element = browserDriver.findElementByXpath(elementXpath);
    actions.sendKeys(element, keys);
    actions.sendKeys(Keys.TAB);
    browserDriver.performAndWaitForRerender(actions.build(), elementXpath);
}

From source file:com.liferay.faces.bridge.test.integration.demo.applicant.JSF_JSPApplicantPortletTester.java

License:Open Source License

protected final void submitPostalCodeAndWaitForPostback(BrowserDriver browserDriver,
        CharSequence... postalCode) {

    String postalCodeFieldXpath = getPostalCodeFieldXpath();
    Actions actions = browserDriver.createActions(postalCodeFieldXpath);
    WebElement postalCodeField = browserDriver.findElementByXpath(postalCodeFieldXpath);
    actions.sendKeys(postalCodeField, postalCode);
    actions.sendKeys(Keys.TAB);//from w  w  w .jav a2 s.  c om
    browserDriver.performAndWaitForRerender(actions.build(), postalCodeFieldXpath);
}

From source file:org.auraframework.components.ui.popup.PopupUITest.java

License:Apache License

/**
 * Helper to create a custom popup trigger and closer.  Trigger is
 * selecting an input box; popup closing is done via clicking on a
 * button OR via the keyboard TAB key.//from w  w w . j ava2 s.c  o m
 * 
 * @param closeOnTabKey true if using the TAB key to close the popup;
 *      false if using the button to close the popup
 * @throws Exception
 */
private void verifyCustomTrigger(boolean closeOnTabKey) throws Exception {
    WebDriver driver = this.getDriver();
    String triggerLocator = "customTriggerInput";
    String buttonLocator = "customTriggerButton";
    String targetLocator = "customTriggerTargetContainer";
    String appUrl = "/uitest/popupTest.app";
    open(appUrl);

    // Click on the trigger and verify the popup opens
    waitForElementPresent("Trigger input box not present", driver.findElement(By.className(triggerLocator)));
    WebElement triggerInput = driver.findElement(By.className(triggerLocator));
    triggerInput.click();
    WebElement popContainerTgtElem = driver.findElement(By.className(targetLocator));
    waitForElementPresent("Popup did not launch", popContainerTgtElem);
    // Close the popup either by the TAB key or the close button
    if (closeOnTabKey) {
        Actions builder = new Actions(this.currentDriver);
        builder.sendKeys(Keys.TAB, Keys.NULL).build().perform();
    } else {
        WebElement customTriggerButton = driver.findElement(By.className(buttonLocator));
        customTriggerButton.click();
    }

    waitForElementAbsent("Popup did not close", popContainerTgtElem);
}

From source file:org.auraframework.integration.test.components.ui.popup.PopupUITest.java

License:Apache License

/**
 * Helper to create a custom popup trigger and closer.  Trigger is
 * selecting an input box; popup closing is done via clicking on a
 * button OR via the keyboard TAB key.//from  w  w w . j  ava 2s. c  o m
 *
 * @param closeOnTabKey true if using the TAB key to close the popup;
 *                      false if using the button to close the popup
 * @throws Exception
 */
private void verifyCustomTrigger(boolean closeOnTabKey) throws Exception {
    WebDriver driver = this.getDriver();
    String triggerLocator = "customTriggerInput";
    String buttonLocator = "customTriggerButton";
    String targetLocator = "customTriggerTargetContainer";
    String appUrl = "/uitest/popupTest.app";
    open(appUrl);

    // Click on the trigger and verify the popup opens
    getAuraUITestingUtil().waitForElementDisplayed(By.cssSelector(triggerLocator),
            "Trigger input box not present");
    WebElement triggerInput = driver.findElement(By.className(triggerLocator));
    triggerInput.click();
    getAuraUITestingUtil().waitForElementDisplayed(By.cssSelector(targetLocator), "Popup did not launch");
    // Close the popup either by the TAB key or the close button
    if (closeOnTabKey) {
        Actions builder = new Actions(this.getDriver());
        builder.sendKeys(Keys.TAB, Keys.NULL).build().perform();
    } else {
        WebElement customTriggerButton = driver.findElement(By.className(buttonLocator));
        customTriggerButton.click();
    }

    getAuraUITestingUtil().waitForElementNotDisplayed(By.cssSelector(targetLocator), "Popup did not close");
}

From source file:org.finra.jtaf.ewd.widget.element.InteractiveElement.java

License:Apache License

@Override
public void sendKeys(CharSequence keysToSend) throws WidgetException {
    try {/*from   w w  w .  j av  a2 s  .  c om*/
        Actions builder = new Actions(getGUIDriver().getWrappedDriver());
        synchronized (InteractiveElement.class) {
            getGUIDriver().focus();
            builder.sendKeys(getWebElement(), keysToSend).build().perform();
        }
    } catch (Exception e) {
        throw new WidgetException("Error while sending keys", getLocator(), e);
    }

}

From source file:org.jitsi.meet.test.web.WebParticipant.java

License:Apache License

/**
 * {@inheritDoc}//from  ww  w  .j ava  2s .c o m
 */
@Override
public void pressShortcut(Character shortcut) {
    // We've observed intermittent failures with the keys being sent to the
    // wrong element (e.g. the chat input field). Selecting "body" instead
    // of another element seems to make this condition appear less often.
    WebDriver driver = getDriver();
    WebElement body = driver.findElement(By.tagName("body"));
    Actions actions = new Actions(driver);
    actions.moveToElement(body);
    actions.sendKeys(body, shortcut.toString());
    actions.perform();
}

From source file:org.richfaces.tests.page.fragments.impl.input.TextInputComponentImpl.java

License:Open Source License

@Override
public TextInputComponent clear(ClearType clearType) {
    int valueLength = root.getAttribute("value").length();
    Actions builder = new Actions(getWebDriver());
    JavascriptExecutor executor = (JavascriptExecutor) getWebDriver();
    switch (clearType) {
    case BACKSPACE:
        for (int i = 0; i < valueLength; i++) {
            builder.sendKeys(root, Keys.BACK_SPACE);
        }/*from ww w .  j a  v  a2 s  .  c  o m*/
        builder.build().perform();
        break;
    case DELETE:
        String ctrlADel = Keys.chord(Keys.CONTROL, "a", Keys.DELETE);
        builder.sendKeys(root, ctrlADel);
        builder.build().perform();
        break;
    case ESCAPE_SQ:
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < valueLength; i++) {
            sb.append("\b");
        }
        root.sendKeys(sb.toString());
        root.click();
        break;
    case JS:
        executor.executeScript("jQuery(arguments[0]).val('')", root);
        break;
    case WD:
        root.clear();
        break;
    default:
        throw new UnsupportedOperationException("Unknown type of clear method " + clearType);
    }
    return this;
}