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.rstudio.studio.selenium.RConsoleInteraction.java

License:Open Source License

@Test
public void testBasicRInteraction() {
    Actions do42 = new Actions(driver_);
    do42.sendKeys(Keys.chord(Keys.CONTROL, "l"));
    do42.sendKeys(Keys.ESCAPE);
    do42.sendKeys("41 + 1");
    do42.sendKeys(Keys.ENTER);
    do42.perform();

    ConsoleTestUtils.waitForConsoleContainsText(driver_, "42");
}

From source file:org.rstudio.studio.selenium.RConsoleInteraction.java

License:Open Source License

@Test
public void testPopupCompletion() {
    // Test invoking autocomplete
    List<WebElement> elements = driver_
            .findElements(By.id(ElementIds.getElementId(ElementIds.POPUP_COMPLETIONS)));
    assertEquals(elements.size(), 0);/*  w  ww  . j ava  2 s.c o m*/

    Actions popup = new Actions(driver_);
    popup.sendKeys(Keys.ESCAPE);
    popup.sendKeys("print");
    popup.sendKeys(Keys.TAB);
    popup.perform();

    (new WebDriverWait(driver_, 5)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            List<WebElement> elements = driver_
                    .findElements(By.id(ElementIds.getElementId(ElementIds.POPUP_COMPLETIONS)));
            return elements.size() > 0;
        }
    });

    // Test cancelling autocomplete once invoked
    Actions close = new Actions(driver_);
    close.sendKeys(Keys.ESCAPE).perform();

    (new WebDriverWait(driver_, 5)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            List<WebElement> elements = driver_
                    .findElements(By.id(ElementIds.getElementId(ElementIds.POPUP_COMPLETIONS)));
            return elements.size() == 0;
        }
    });
}

From source file:org.rstudio.studio.selenium.RConsoleInteraction.java

License:Open Source License

@Test
public void testPlotGeneration() {
    ConsoleTestUtils.resumeConsoleInteraction(driver_);

    Actions plotCars = new Actions(driver_);
    plotCars.sendKeys(Keys.ESCAPE + "plot(cars)" + Keys.ENTER);
    plotCars.perform();/*from   ww  w . j  a va  2s. co  m*/

    // Wait for the Plot window to activate
    final WebElement plotWindow = (new WebDriverWait(driver_, 5)).until(ExpectedConditions
            .presenceOfElementLocated(By.id(ElementIds.getElementId(ElementIds.PLOT_IMAGE_FRAME))));

    // Wait for a plot to appear in the window
    Assert.assertEquals(plotWindow.getTagName(), "iframe");
    driver_.switchTo().frame(plotWindow);

    (new WebDriverWait(driver_, 5)).until(ExpectedConditions.presenceOfElementLocated(By.tagName("img")));

    // Switch back to document context
    driver_.switchTo().defaultContent();
}

From source file:org.rstudio.studio.selenium.RConsoleInteraction.java

License:Open Source License

@Test
public void testInvokeHelp() {
    ConsoleTestUtils.resumeConsoleInteraction(driver_);
    Actions help = new Actions(driver_);
    help.sendKeys(Keys.ESCAPE + "?lapply" + Keys.ENTER);
    help.perform();/*from w  w w.j  a  va 2 s.c  o m*/

    // Wait for the Help window to activate
    final WebElement helpWindow = (new WebDriverWait(driver_, 5)).until(
            ExpectedConditions.presenceOfElementLocated(By.id(ElementIds.getElementId(ElementIds.HELP_FRAME))));

    // Wait for help to appear in the window
    Assert.assertEquals(helpWindow.getTagName(), "iframe");
    driver_.switchTo().frame(helpWindow);

    (new WebDriverWait(driver_, 5))
            .until(ExpectedConditions.textToBePresentInElement(By.tagName("body"), "lapply"));

    // Switch back to document context
    driver_.switchTo().defaultContent();
}

From source file:org.rstudio.studio.selenium.SourceInteraction.java

License:Open Source License

@Test
public void createAndSourceRFile() {
    createRFile();/*from ww w  . j  a v  a  2s  . c o m*/

    // Type some code into the file. Note that the matching brace is auto 
    // completed.
    Actions a = new Actions(driver_);
    a.sendKeys("f <- function() {" + Keys.ENTER);
    a.sendKeys(Keys.TAB + "42");
    a.perform();

    // Source the entire file
    WebElement sourceMenuEntry = MenuNavigator.getMenuItem(driver_, "Code", "Source");
    sourceMenuEntry.click();

    // Wait for the console to contain the string "source"
    ConsoleTestUtils.waitForConsoleContainsText(driver_, "source(");

    closeUnsavedRFile();
}

From source file:org.rstudio.studio.selenium.SourceInteraction.java

License:Open Source License

@Test
public void findAndReplace() {
    createRFile();//w  ww. j a  v a 2  s .co m

    // Type some code into the file
    String preReplaceCode = "foo <- 'bar'";
    Actions a = new Actions(driver_);
    a.sendKeys(preReplaceCode + Keys.ENTER);
    a.perform();

    // Find the ACE editor instance that the code appears in. (CONSIDER: 
    // This is not the best way to find the code editor instance.)
    WebElement editor = null;
    List<WebElement> editors = driver_.findElements(By.className("ace_content"));
    for (WebElement e : editors) {
        if (e.getText().contains(preReplaceCode)) {
            editor = e;
            break;
        }
    }
    Assert.assertNotNull(editor);

    // Invoke find and replace
    WebElement findMenuEntry = MenuNavigator.getMenuItem(driver_, "Edit", "Find...");
    findMenuEntry.click();

    // Wait for the find and replace panel to come up
    (new WebDriverWait(driver_, 2)).until(ExpectedConditions
            .presenceOfElementLocated(By.id(ElementIds.getElementId(ElementIds.FIND_REPLACE_BAR))));

    // Type the text and the text to be replaced (replace 'bar' with 'foo')
    Actions rep = new Actions(driver_);
    rep.sendKeys("bar" + Keys.TAB + "foo" + Keys.ENTER);
    rep.perform();

    DialogTestUtils.respondToModalDialog(driver_, "OK");

    Actions dismiss = new Actions(driver_);
    dismiss.sendKeys(Keys.ESCAPE);
    dismiss.perform();

    // Ensure that the source has been updated
    Assert.assertTrue(editor.getText().contains("foo <- 'foo'"));

    closeUnsavedRFile();
}

From source file:org.safs.selenium.webdriver.lib.WDLibrary.java

License:Open Source License

/**
 * Does NOT clear any existing text in the control, but does attempt to insure the window/control has focus.
 * <br><em>Purpose:</em>
 * <a href="http://safsdev.sourceforge.net/sqabasic2000/SeleniumGenericMasterFunctionsReference.htm#detail_InputKeys" alt="inputKeys Keyword Reference" title="inputKeys Keyword Reference">inputKeys</a>
 * <p>//from   w  w  w.j  a  v a 2s  .c o  m
 * Bypasses attempts to use AWT Robot for keystrokes.
 * Attempts to convert SAFS keystrokes to Selenium low-level Actions keystrokes.
 * @param we WebElement to send SAFS keystrokes (or plain text).
 * @param keystrokes SAFS keystrokes or plain text to type.
 * @throws SeleniumPlusException if we are unable to process the keystrokes successfully.
 **/
public static void inputKeysSAFS2Selenium(WebElement we, String keystrokes) throws SeleniumPlusException {
    String debugmsg = StringUtils.debugmsg(false);
    IndependantLog.debug(debugmsg + " processing '" + keystrokes + "' on webelement " + we);
    if (!focus(we))
        IndependantLog.warn(debugmsg + " Fail to set focus to webelement " + we);

    RemoteDriver wd = null;
    try {
        wd = (RemoteDriver) getWebDriver();
    } catch (Exception x) {
    }
    // convert to Selenium low-level Action keystrokes.
    if (keysparser != null) {
        Vector keys = keysparser.parseInput(keystrokes);
        Actions actions = new Actions(wd);

        if (we != null)
            actions = actions.moveToElement(we);

        Iterator events = keys.iterator();
        RobotKeyEvent event;
        Keys k = null;
        CharSequence c = null;
        while (events.hasNext()) {
            try {
                event = (RobotKeyEvent) events.next();
                c = null;
                k = convertToKeys(event);
                if (k == null) {
                    c = convertToCharacter(event);
                } else {

                }
                switch (event.get_event()) {

                case RobotKeyEvent.KEY_PRESS:
                    if (k != null) {
                        IndependantLog.debug(debugmsg + " handling keyDown '" + k.name() + "'");
                        actions = actions.keyDown(k);
                    } else {
                        IndependantLog.debug(debugmsg + " send char '" + c + "'");
                        actions = actions.sendKeys(c);
                    }
                    break;

                case RobotKeyEvent.KEY_RELEASE:
                    if (k != null) {
                        IndependantLog.debug(debugmsg + " handling keyUp '" + k.name() + "'");
                        actions = actions.keyUp(k);
                    } else {
                        IndependantLog.debug(debugmsg + " send char '" + c + "'");
                        actions = actions.sendKeys(c);
                    }
                    break;

                case RobotKeyEvent.KEY_TYPE:
                    if (k != null) {
                        IndependantLog.debug(debugmsg + " send Key '" + k.name() + "'");
                        actions = actions.sendKeys(k);
                    } else {
                        IndependantLog.debug(debugmsg + " send char '" + c + "'");
                        actions = actions.sendKeys(c);
                    }
                    break;
                default:
                }
            } catch (Exception x) {
                IndependantLog.debug(debugmsg + " IGNORING RobotKeyEvent exception:", x);
            }
        }
        try {
            actions.build().perform();
        } catch (StaleElementReferenceException x) {
            // the click probably was successful because the elements have changed!
            IndependantLog.debug(debugmsg + " StaleElementException (not found).");
        } catch (Throwable x) {
            IndependantLog.debug(debugmsg + " " + x.getClass().getName() + ", " + x.getMessage());
        } finally {
            IndependantLog.debug(debugmsg + " selenium actions.build().perform() complete.");
        }
    } else {
        // TODO what if keyparser cannot load a keys converter???
    }
}

From source file:org.sonarqube.qa.util.pageobjects.measures.MeasuresPage.java

License:Open Source License

public MeasuresPage backShortcut() {
    SelenideElement panel = Selenide.$(".layout-page-header-panel");

    // panel.sendKeys(Keys.LEFT) does not work correctly on Chrome
    // The workaround is to use Actions
    // https://bugs.chromium.org/p/chromedriver/issues/detail?id=35
    Actions actions = new Actions(WebDriverRunner.getWebDriver());
    actions.moveToElement(panel);//ww  w.  j a  va 2 s  .co m
    actions.click();
    actions.sendKeys(Keys.LEFT);
    actions.build().perform();
    return this;
}

From source file:org.testeditor.fixture.web.angularjs.materialdesign.NgMdWebDriverFixture.java

License:Open Source License

/**
 * @return the Options of a selection Field as a {@code JsonObject}
 * @param elementLocator Locator for Selection
 *///from  w  w w .j  av a2 s. c  o  m
@FixtureMethod
public JsonObject getOptionsInSelection(String elementLocator, LocatorStrategy locatorStrategy)
        throws FixtureException {
    JsonObject availableOptions = new JsonObject();
    clickOn(elementLocator, locatorStrategy);
    wrappedSleep(300, "get option in selection interrupted", FixtureException.keyValues("elementLocator",
            elementLocator, "locatorStrategy", locatorStrategy.toString()));
    waitForAngularCompleteOperations();
    ByAngularCssContainingText cssContainingText = ByAngular.cssContainingText("md-option", "");
    List<WebElement> options = getDriver().findElements(cssContainingText);
    for (WebElement element : options) {
        if (element.isDisplayed()) {
            availableOptions.addProperty(element.getText(), element.getText());
        }
    }
    Actions actions = new Actions(getDriver());
    actions.sendKeys(Keys.ESCAPE).perform();
    return availableOptions;
}

From source file:org.testeditor.fixture.web.WebFixture.java

License:Open Source License

/**
 * Finds an element on the web page by its Element List key and inputs a
 * special key stroke in its context. <br />
 * <br />//w w  w . j ava 2  s .c  o m
 * 
 * FitNesse usage..: |enter special key|arg1| <br />
 * FitNesse example: |enter special key|RIGHT| <br />
 * <br />
 * 
 * @param key
 *            the key stroke to enter (see
 *            https://code.google.com/p/selenium
 *            /source/browse/java/client/src/org/openqa/selenium/Keys.java)
 * @return the value of the target element; empty String, if the element
 *         could not be found or is not visible (i.e. hidden using CSS,
 *         etc.).
 */
public boolean enterSpecialKey(String key) {
    boolean result = false;

    Keys seleniumKey = Keys.NULL;
    try {
        seleniumKey = Keys.valueOf(key.toUpperCase());
    } catch (IllegalArgumentException e) {
        String message = "The specified key \"" + key
                + "\" is invalid and could not be found in selenium enum Keys!";
        LOGGER.error(message, e);
        throw new StopTestException(message);
    }

    Actions action = new Actions(webDriver);
    action.sendKeys(seleniumKey).build().perform();
    result = true;

    return result;
}