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

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

Introduction

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

Prototype

public Actions keyDown(CharSequence key) 

Source Link

Document

Performs a modifier key press.

Usage

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

License:Open Source License

/**
 * Press down a Key by Selenium's Actions API. Call {@link #keyUp(Keys)} to release the key.
 * @param keycode Keys, keycode to press (e.g. <code>Keys.CONTROL</code>)
 * @throws SeleniumPlusException if fail
 * @see #keyUp(Keys)/*from  ww w.j  av a2s .com*/
 */
public static void keyDown(Keys keycode) throws SeleniumPlusException {
    try {
        WebDriver wd = (WebDriver) getWebDriver();
        Actions actions = new Actions(wd);
        actions.keyDown(keycode);
        actions.build().perform();
    } catch (Exception e) {
        throw new SeleniumPlusException("Unable to successfully complete Selenium keyDown for key '" + keycode
                + "' due to " + e.getMessage(), e);
    }
}

From source file:org.xmlium.test.web.commons.xml.XMLTestSteps.java

License:LGPL

protected void processStep(StepType step) throws Exception {
    WebElement element = null;//from  w ww  .j a  va  2s .  com
    Alert a = null;
    if (step.getScrollX() != null && step.getScrollY() != null) {
        JavascriptExecutor js = (JavascriptExecutor) getSuite().getDriver();
        js.executeScript("window.scrollTo(" + step.getScrollX() + ", " + step.getScrollY() + ");");
    }
    if (step.isBack() != null && step.isBack()) {
        getSuite().getDriver().navigate().back();
    }
    if (step.isForward() != null && step.isForward()) {
        getSuite().getDriver().navigate().forward();
    }
    Element e = null;
    if (step.getLoad() != null && step.getLoad().getKey() != null) {
        e = store.get(step.getLoad().getKey());
        element = findElement(e.getFinds());
    }
    if (step.getElement() != null) {
        if (step.getElement().getFinds() != null) {
            e = step.getElement();
            checkStore(e);

            element = checkWaitFor(e);
            if (element == null && step.getElement().isCheckNullElement()) {
                return;
            }
            checkStoreValue(e, element);
            checkSetValue(e, element);
            checkSendKeys(e, element);
            checkClick(e, element);
            checkChangeState(e);
            checkText(e, element);
            checkMoveX(e, element);
        }
    }
    if (step.getSwitchTo() != null) {
        Object to = switchTo(step.getSwitchTo());
        if (to instanceof Alert) {
            a = (Alert) to;
            if (step.getSwitchTo().getAlert() != null && step.getSwitchTo().getAlert().isAccept()) {
                a.accept();
            }
        } else {
            a = null;
        }
    }
    if (step.getSelect() != null) {
        org.xmlium.testsuite.Select stepSelect = step.getSelect();

        if (element != null && stepSelect.getSelectBy().getByIndex() != null) {
            Index index = stepSelect.getSelectBy().getByIndex();
            SelectData selectData = new SelectData();
            initSelectIndexes(element, index, selectData);
            if (selectData.selectIndex >= 0) {
                Select select = new Select(element);
                List<WebElement> options = select.getOptions();
                WebElement option = options.get(selectData.selectIndex);
                option.click();
            } else {
                throw new RuntimeException("selectedIndex=" + selectData.selectIndex);
            }
        } else {
            if (element == null) {
                element = findElement(step.getSelect().getFinds());
            }
            if (element != null) {
                Select select = new Select(element);
                if (step.getSelect().getSelectBy() != null) {
                    selectBy(select, step.getSelect().getSelectBy());
                }
            }
        }
    }
    if (step.getPrettySelect() != null) {
        PrettySelect stepSelect = step.getPrettySelect();
        if (element == null) {
            if (stepSelect.getFinds() != null) {
                element = findElement(stepSelect.getFinds());
            }
        }
        if (element != null && stepSelect.getSelectBy() != null) {
            SelectData selectData = new SelectData();
            Select select = null;
            select = new Select(element);
            Index index = null;
            if (stepSelect.getSelectBy().getByIndex() != null) {
                index = stepSelect.getSelectBy().getByIndex();
                initSelectIndexes(element, index, selectData);
            } else if (stepSelect.getSelectBy().getByVisibleText() != null) {
                List<WebElement> elems = select.getOptions();
                getOptionIndexBuVisibleText(stepSelect.getSelectBy().getByVisibleText(), elems, selectData);
            }

            if (selectData.selectIndex >= 0) {
                WebElement arrowArea = null;
                WebElement scrollArea = null;
                if (stepSelect.getSelectBy().getArrowArea() != null) {
                    arrowArea = findElement(stepSelect.getSelectBy().getArrowArea());
                } else {
                    throw new RuntimeException("arrowArea not defined in xml");
                }

                if (arrowArea != null) {
                    arrowArea.click();

                    if (stepSelect.getSelectBy().getScrollArea() != null) {
                        Finds scrollAreaFinds = stepSelect.getSelectBy().getScrollArea();
                        if (scrollAreaFinds.getFind() != null) {
                            scrollArea = findElement(scrollAreaFinds.getFind());
                        } else if (scrollAreaFinds.getWaitFor() != null) {
                            scrollArea = waitFor(scrollAreaFinds.getWaitFor());
                        }
                        if (scrollArea == null) {
                            if (scrollAreaFinds.getFind() != null || scrollAreaFinds.getWaitFor() != null) {
                                String text = "";
                                if (scrollAreaFinds.getFind() != null
                                        && scrollAreaFinds.getFind().getByXPath() != null) {
                                    text = unformatValue(scrollAreaFinds.getFind().isUnformat(),
                                            scrollAreaFinds.getFind().getByXPath().getValue());
                                } else if (scrollAreaFinds.getWaitFor() != null
                                        && scrollAreaFinds.getWaitFor().getByXPath() != null) {
                                    text = unformatValue(scrollAreaFinds.getWaitFor().isUnformat(),
                                            scrollAreaFinds.getWaitFor().getByXPath().getValue());
                                }

                                throw new RuntimeException("scrollArea not found: " + text);
                            } else {
                                throw new RuntimeException("scrollArea not defined in xml");
                            }
                        }
                    }

                    if (index.isFirst() != null || index.isLast() != null) {
                        Actions actions = new Actions(getSuite().getDriver());
                        String text = null;
                        if (index.isFirst() != null && index.isFirst()) {
                            actions.keyDown(Keys.CONTROL).sendKeys(Keys.HOME).perform();
                        } else if (index.isLast() != null && index.isLast()) {
                            actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();
                        }

                        if (index.isFirst() != null && index.isFirst()) {
                            text = select.getOptions().get(0).getText();
                        } else if (index.isLast() != null && index.isLast()) {
                            text = select.getOptions().get(select.getOptions().size() - 1).getText();
                        }
                        if (stepSelect.getSelectBy().getOptionsTag() != null) {
                            Find f = new Find();
                            ByXPath xPath = new ByXPath();
                            if (text != null && !text.isEmpty()) {
                                xPath.setValue("//" + stepSelect.getSelectBy().getOptionsTag()
                                        + "[contains(@class, '" + stepSelect.getSelectBy().getOptionsClasses()
                                        + "') and text()='" + text + "']");
                            } else {
                                xPath.setValue("(//" + stepSelect.getSelectBy().getOptionsTag()
                                        + "[contains(@class, '" + stepSelect.getSelectBy().getOptionsClasses()
                                        + "')])[" + Integer.toString(selectData.selectIndex + 1) + "]");
                            }
                            f.setByXPath(xPath);
                            WebElement optionElemnt = findElement(f);
                            WebDriverWait wait = new WebDriverWait(getSuite().getDriver(), 30);
                            wait.until(ExpectedConditions.elementToBeClickable(optionElemnt));
                            optionElemnt.click();
                        }
                    } else {
                        if (selectData.selectIndex < 0
                                || selectData.selectIndex >= select.getOptions().size()) {
                            throw new RuntimeException("selectIndex: " + selectData.selectIndex
                                    + ", options.size: " + select.getOptions().size());
                        }
                        WebElement option = select.getOptions().get(selectData.selectIndex);
                        String text = option.getText();
                        if (text == null || text.isEmpty()) {
                            logger.debug("text is null or empty: " + option);
                            text = null;
                        }
                        if (stepSelect.getSelectBy().getOptionsTag() != null) {
                            Find f = new Find();
                            ByXPath xPath = new ByXPath();
                            if (text != null) {
                                xPath.setValue("//" + stepSelect.getSelectBy().getOptionsTag()
                                        + "[contains(@class, '" + stepSelect.getSelectBy().getOptionsClasses()
                                        + "') and text()='" + text + "']");
                            } else {
                                xPath.setValue("(//" + stepSelect.getSelectBy().getOptionsTag()
                                        + "[contains(@class, '" + stepSelect.getSelectBy().getOptionsClasses()
                                        + "')])[" + Integer.toString(selectData.selectIndex + 1) + "]");
                            }
                            f.setByXPath(xPath);
                            WebElement optionElemnt = findElement(f);

                            Coordinates coordinate = ((Locatable) optionElemnt).getCoordinates();
                            coordinate.onPage();
                            coordinate.inViewPort();
                            WebDriverWait wait = new WebDriverWait(getSuite().getDriver(), 30);
                            wait.until(ExpectedConditions.elementToBeClickable(optionElemnt));
                            optionElemnt.click();
                        }
                    }
                } else {
                    throw new RuntimeException("can't click the arrow");
                }
            } else if (stepSelect.getSelectBy().getByVisibleText() != null) {
                WebElement arrowArea = null;
                WebElement scrollArea = null;
                if (stepSelect.getSelectBy().getArrowArea() != null) {
                    arrowArea = findElement(stepSelect.getSelectBy().getArrowArea());
                } else {
                    throw new RuntimeException("arrowArea not defined in xml");
                }

                if (arrowArea != null) {
                    arrowArea.click();
                    String scrollAreaXPath = null;
                    if (stepSelect.getSelectBy().getScrollArea() != null) {
                        Finds scrollAreaFinds = stepSelect.getSelectBy().getScrollArea();
                        if (scrollAreaFinds.getFind() != null) {
                            scrollArea = findElement(scrollAreaFinds.getFind());
                        } else if (scrollAreaFinds.getWaitFor() != null) {
                            scrollArea = waitFor(scrollAreaFinds.getWaitFor());
                        }

                        if (scrollAreaXPath == null) {
                            if (scrollAreaFinds.getFind() != null || scrollAreaFinds.getWaitFor() != null) {
                                if (scrollAreaFinds.getFind() != null
                                        && scrollAreaFinds.getFind().getByXPath() != null) {
                                    scrollAreaXPath = unformatValue(scrollAreaFinds.getFind().isUnformat(),
                                            scrollAreaFinds.getFind().getByXPath().getValue());
                                } else if (scrollAreaFinds.getWaitFor() != null
                                        && scrollAreaFinds.getWaitFor().getByXPath() != null) {
                                    scrollAreaXPath = unformatValue(scrollAreaFinds.getWaitFor().isUnformat(),
                                            scrollAreaFinds.getWaitFor().getByXPath().getValue());
                                }
                                // throw new RuntimeException("scrollArea
                                // not found: " + text);
                            } else {
                                throw new RuntimeException("scrollArea not defined in xml");
                            }
                        }
                    }

                    Actions actions = new Actions(getSuite().getDriver());
                    String text = null;
                    // actions.keyDown(Keys.CONTROL).sendKeys(Keys.HOME).perform();
                    WebDriverWait wait = new WebDriverWait(getSuite().getDriver(), 30);
                    wait.until(ExpectedConditions.elementToBeClickable(scrollArea));
                    actions.moveToElement(scrollArea).build().perform();

                    text = stepSelect.getSelectBy().getByVisibleText();
                    logger.debug("text: " + text);
                    if (stepSelect.getSelectBy().getOptionsTag() != null) {
                        Find f = new Find();
                        ByXPath xPath = new ByXPath();
                        if (text != null) {

                            xPath.setValue(scrollAreaXPath + "/descendant::"
                                    + stepSelect.getSelectBy().getOptionsTag() + "[contains(@class, '"
                                    + stepSelect.getSelectBy().getOptionsClasses() + "') and text()='" + text
                                    + "']");
                        } else {
                            xPath.setValue("(//" + stepSelect.getSelectBy().getOptionsTag()
                                    + "[contains(@class, '" + stepSelect.getSelectBy().getOptionsClasses()
                                    + "')])[" + Integer.toString(selectData.selectIndex + 1) + "]");
                        }
                        f.setByXPath(xPath);
                        logger.debug("xPath: " + xPath);
                        WebElement optionElemnt = findElement(f);

                        Coordinates coordinate = ((Locatable) optionElemnt).getCoordinates();
                        coordinate.onPage();
                        coordinate.inViewPort();
                        wait = new WebDriverWait(getSuite().getDriver(), 30);
                        wait.until(ExpectedConditions.visibilityOf(optionElemnt));
                        actions.moveToElement(optionElemnt).click().build().perform();
                    }
                }
            } else {
                throw new RuntimeException("selectedIndex=" + selectData.selectIndex);
            }
        } else {
            if (element == null) {
                element = findElement(step.getPrettySelect().getFinds());
            }
            if (element != null) {
                Select select = new Select(element);
                if (step.getPrettySelect().getSelectBy() != null) {
                    selectBy(select, step.getPrettySelect().getSelectBy());
                }
            }
        }
    }

    if (step.getSleepAfter() != null) {
        long sleep = step.getSleepAfter().longValue();
        try {
            Thread.sleep(sleep);
        } catch (Exception e2) {
            // TODO: handle exception
        }
    }
}