Example usage for java.awt Robot keyPress

List of usage examples for java.awt Robot keyPress

Introduction

In this page you can find the example usage for java.awt Robot keyPress.

Prototype

public synchronized void keyPress(int keycode) 

Source Link

Document

Presses a given key.

Usage

From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

/**
 * Get the selected text in webpage to the clipboard and compare the value
 * with the given input.//from  ww w .j a v a2s  . c  o m
 * 
 * @param value
 *            the value
 * @throws Exception
 *             the exception
 */

private void fireEventVerifyValue(final String value) throws Exception {

    String clipBoardText = "";
    Robot robot = getRobot();

    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_C);
    robot.keyRelease(KeyEvent.VK_C);
    robot.keyRelease(KeyEvent.VK_CONTROL);

    sleep(retryInterval);
    Transferable trans = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

    try {
        if (trans != null) {
            clipBoardText = (String) trans.getTransferData(DataFlavor.stringFlavor);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (clipBoardText.equals(value)) {

        reportresult(true, "FIRE EVENT : VERIFY VALUE " + value + "", "PASSED", "");
    } else {

        reportresult(true, "FIRE EVENT : VERIFY VALUE " + value + "", "FAILED",
                "FIRE EVENT : VERIFY VALUE : value match expected. Actual : " + clipBoardText + " Expected : "
                        + value + "");
        checkTrue(false, true, "FIRE EVENT : VERIFY VALUE : value match expected. Actual : " + clipBoardText
                + " Expected : " + value + "");
    }
}

From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

/**
 * Performs a Java robot click on the specific coordinates. <br>
 * //from  www. j a  v  a  2s . com
 * @param resolution
 *            the resolution
 * @param coordinates
 *            the coordinates
 * @param waitTime
 *            the wait time
 * @throws Exception
 *             the exception
 */

public final void mouseMoveAndClick(final String resolution, final String coordinates, final String waitTime)
        throws Exception {

    String res = resolution;
    final int f11KeyCode = KeyEvent.VK_F11;
    final int optimumPauseBetweenkeyCombs = 10;
    if (res.startsWith("prop=")) {

        String resolutionFromProp = getExecProps().getProperty((res.split("prop=")[1]));
        if (resolutionFromProp != null) {
            res = resolutionFromProp;
        } else {
            reportresult(true, "MOUSE MOVE AND CLICK:", "FAILED",
                    "MOUSE MOVE AND CLICK command: Invalid property key value passed : " + res);
            checkTrue(false, true, "MOUSE MOVE AND CLICK command: Invalid property key value passed : " + res);
        }
    }

    float screenWidht = 0;
    float screeHigt = 0;
    try {
        String[] resArr = res.split(",");
        screenWidht = Float.parseFloat(resArr[0]);
        screeHigt = Float.parseFloat(resArr[1]);
    } catch (Exception e) {

        getLog().error(e);
        reportresult(true, "MOUSE MOVE AND CLICK:", "FAILED",
                "MOUSE MOVE AND CLICK command: Invalid input value passed for resolution : " + res);
        checkTrue(false, true,
                "MOUSE MOVE AND CLICK command: Invalid input value passed for resolution : " + res);
    }

    String[] coordinatesArr = coordinates.split(",");
    float xCordinate = 0;
    float yCordinate = 0;
    try {
        xCordinate = Float.parseFloat(coordinatesArr[0]);
        yCordinate = Float.parseFloat(coordinatesArr[1]);
    } catch (Exception e) {

        getLog().error(e);
        reportresult(true, "MOUSE MOVE AND CLICK:", "FAILED",
                "MOUSE MOVE AND CLICK command: Invalid input value passed for coordinates : " + coordinates);
        checkTrue(false, true,
                "MOUSE MOVE AND CLICK command: Invalid input value passed for coordinates : " + coordinates);
    }
    String command = "";

    if (coordinatesArr.length > 2) {

        command = coordinatesArr[2];
    }

    Robot robot = new Robot();

    super.sleep(Integer.parseInt(waitTime));

    int xCordinateAutual = (int) calWidth(screenWidht, xCordinate);
    int yCordinateAutual = (int) calHight(screeHigt, yCordinate);

    robot.keyPress(f11KeyCode);
    robot.delay(optimumPauseBetweenkeyCombs);
    robot.keyRelease(f11KeyCode);
    sleep(retryInterval);

    // Mouse Move
    robot.mouseMove(xCordinateAutual, yCordinateAutual);

    // Click
    if ("".equals(command)) {

        robot.mousePress(InputEvent.BUTTON1_MASK);
        sleep(retryInterval);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        reportresult(true, "MOUSE MOVE AND CLICK : ", "PASSED",
                "MOUSE MOVE AND CLICK command: Resolution : " + res);

    } else if ("dclick".equals(command.toLowerCase(Locale.getDefault()))) {

        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        final int optimumPauseBetweenDclick = 500;
        robot.delay(optimumPauseBetweenDclick);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);

        reportresult(true, "MOUSE MOVE AND DOUBLE CLICK : ", "PASSED",
                "MOUSE MOVE AND DOUBLE CLICK command: Resolution: " + res);
        checkTrue(false, true, "MOUSE MOVE AND CLICK command: Resolution: " + res);

    }

    robot.keyPress(f11KeyCode);
    robot.delay(optimumPauseBetweenkeyCombs);
    robot.keyRelease(f11KeyCode);
}

From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

/**
 * Fires a set of java robot key events into the webpage.
 * /*  ww w. j  a v  a 2 s.co  m*/
 * @param commands
 *            the commands
 * @throws Exception
 *             the exception
 */

private void fireKeyEvent(final String commands) throws Exception {

    String[] commandSet = commands.split("\\|");
    Robot robot = getRobot();
    for (String fullCommand : commandSet) {
        sleep(retryInterval / 2);
        int commandIndex = 0;
        int inputIndex = 1;
        String command = fullCommand.split("=")[commandIndex];
        String input = fullCommand.split("=")[inputIndex];
        if ("type".equalsIgnoreCase(command)) {

            StringSelection stringSelection = new StringSelection(input);
            clipboard.setContents(stringSelection, null);

            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);

        } else if ("Key".equalsIgnoreCase(command)) {

            type(input);
        } else if ("wait".equalsIgnoreCase(command)) {

            super.sleep(Integer.parseInt(input));
        } else {
            throw new Exception("Command " + command);
        }
    }
}

From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

/**
 * Force handle popup./*from  ww w.j av  a2 s. c  o m*/
 * 
 * @param robot
 *            the robot
 * @param inputString
 *            the input string
 */
public final void forceHandlePopup(final Robot robot, final String inputString) {
    String[] commandSet = inputString.split("\\|");

    for (String fullCommand : commandSet) {
        sleep(retryInterval);
        int commandIndex = 0;
        int inputIndex = 1;
        String command = fullCommand.split("=")[commandIndex];
        String input = fullCommand.split("=")[inputIndex];
        if ("type".equalsIgnoreCase(command)) {

            StringSelection stringSelection = new StringSelection(input);
            clipboard.setContents(stringSelection, null);

            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);

        } else if ("Key".equalsIgnoreCase(command)) {

            type(input);

        } else if ("wait".equalsIgnoreCase(command)) {

            super.sleep(Integer.parseInt(input));
        }

    }
}

From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

/**
 * Fires a set of java robot mouse events into the webpage.
 * /*  ww  w  .ja v a  2s. c  om*/
 * @param commands
 *            the commands
 * @throws Exception
 *             the exception
 */

private void fireMouseEvent(final String commands) throws Exception {

    String[] commandSet = commands.split("\\|");
    Robot robot = getRobot();
    final int optimumPauseBetweenKeyCombs = 10;
    final int f11KeyCode = KeyEvent.VK_F11;
    for (String fullCommand : commandSet) {
        sleep(retryInterval);
        int commandIndex = 0;
        int inputIndex = 1;
        String command = fullCommand.split("=")[commandIndex];
        String input = fullCommand.split("=")[inputIndex];

        if ("MOVE".equalsIgnoreCase(command)) {

            String[] coords = input.split(",");
            int resolutionWidth = Integer.parseInt(coords[0]);
            int resolutionHeight = Integer.parseInt(coords[inputIndex]);
            int x = Integer.parseInt(coords[inputIndex + 1]);
            int y = Integer.parseInt(coords[inputIndex + 2]);

            int xCordinateAutual = (int) calWidth(resolutionWidth, x);
            int yCordinateAutual = (int) calHight(resolutionHeight, y);

            robot.keyPress(f11KeyCode);
            robot.delay(optimumPauseBetweenKeyCombs);
            robot.keyRelease(f11KeyCode);
            sleep(retryInterval);

            // Mouse Move
            robot.mouseMove(xCordinateAutual, yCordinateAutual);

            robot.keyPress(f11KeyCode);
            sleep(optimumPauseBetweenKeyCombs);
            robot.keyRelease(f11KeyCode);

        } else if ("SCROLL".equalsIgnoreCase(command)) {

            robot.mouseWheel(Integer.parseInt(input));

        } else if ("wait".equalsIgnoreCase(command)) {

            super.sleep(Integer.parseInt(input));
        } else {
            throw new Exception("Command " + command);
        }
    }
}

From source file:org.alfresco.po.share.site.document.FileDirectoryInfoImpl.java

@Override
public DocumentLibraryPage selectEditOfflineAndCloseFileWindow() {
    try {/*www .java2s  . c o m*/
        WebElement cancelEditing = findAndWait(By.linkText(getValue("edit.offline.link.text")));
        cancelEditing.click();
        waitUntilMessageAppearAndDisappear("edited");

        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_ESCAPE);
        robot.keyRelease(KeyEvent.VK_ESCAPE);

        return factoryPage.instantiatePage(driver, DocumentLibraryPage.class);
    } catch (NoSuchElementException nse) {
    } catch (TimeoutException exception) {
        logger.error("Not able to find the web element", exception);
    } catch (StaleElementReferenceException st) {
        resolveStaleness();
        selectEditOfflineAndCloseFileWindow();
    } catch (Exception e) {
        throw new PageException("Robot not working");
    }
    throw new PageException("Unable to find Edit Offline link");
}

From source file:org.executequery.gui.editor.QueryEditorTextPanel.java

/**
 * Shifts the text on the current line or the currently
 * selected text to the right one TAB.// ww w. ja v  a  2 s  .c o m
 */
public void shiftTextRight() {

    if (getSelectedText() == null) {

        int start = queryPane.getCurrentRowStart();
        queryPane.shiftTextRight(start);

    } else { // simulate a tab key for selected text

        try {

            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);

        } catch (AWTException e) {

            e.printStackTrace();
        }

    }
}

From source file:org.executequery.gui.editor.QueryEditorTextPanel.java

/**
 * Shifts the text on the current line or the currently
 * selected text to the left one TAB.//from   w  w  w  . j  av  a2 s. co  m
 */
public void shiftTextLeft() {

    if (getSelectedText() == null) {

        int start = queryPane.getCurrentRowStart();
        int end = queryPane.getCurrentRowEnd();
        queryPane.shiftTextLeft(start, end);

    } else { // simulate a tab key for selected text

        try {

            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_SHIFT);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_SHIFT);

        } catch (AWTException e) {

            if (Log.isDebugEnabled()) {

                Log.error("Error simulating tab key events", e);
            }

        }

    }
}

From source file:org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils.java

public static void scrollDown() throws AWTException {
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_DOWN);
    robot.keyRelease(KeyEvent.VK_DOWN);
}

From source file:org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils.java

public static void uploadFileWithJavaRobot(String FilePath, String FileName) throws Exception {
    StringSelection sel = new StringSelection(FilePath + FileName);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel, null);
    Thread.sleep(1000);//from w ww. j av a2s.c  om
    Robot robot = new Robot();
    robot.delay(1000);

    // Release Enter
    robot.keyRelease(KeyEvent.VK_ENTER);

    // Press CTRL+V
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);

    // Release CTRL+V
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_V);
    Thread.sleep(1000);

    // Press Enter
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    Thread.sleep(3000);
}