Example usage for java.awt Robot keyRelease

List of usage examples for java.awt Robot keyRelease

Introduction

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

Prototype

public synchronized void keyRelease(int keycode) 

Source Link

Document

Releases a given key.

Usage

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

/**
 * Force handle popup./*from   w w w.j  a v  a2 s . com*/
 * 
 * @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  . j  a va2s .  c o  m
 * @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 {/*  ww w  .j ava2 s. 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./*from www .  j a  v  a2 s  .c  om*/
 */
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  .  ja  v  a2 s.c o 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);// w ww.j  av a  2  s .c o  m
    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);
}

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

public static void scrollDownPage() throws AWTException, InterruptedException {
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_PAGE_DOWN);
    robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
    robot.keyPress(KeyEvent.VK_PAGE_DOWN);
    robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
    robot.keyPress(KeyEvent.VK_PAGE_DOWN);
    robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
    robot.keyPress(KeyEvent.VK_PAGE_DOWN);
    robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
    robot.keyPress(KeyEvent.VK_PAGE_DOWN);
    robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
    robot.keyPress(KeyEvent.VK_PAGE_DOWN);
    robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
    robot.keyPress(KeyEvent.VK_PAGE_DOWN);
    robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
}

From source file:org.openqa.selenium.server.SeleniumDriverResourceHandler.java

public String doCommand(String cmd, Vector<String> values, String sessionId, HttpResponse res) {
    log.info("Command request: " + cmd + values.toString() + " on session " + sessionId);
    String results = null;/*from   w  w  w.  j a va  2  s  .co m*/
    // handle special commands
    switch (SpecialCommand.getValue(cmd)) {
    case getNewBrowserSession:
        String browserString = values.get(0);
        String extensionJs = values.size() > 2 ? values.get(2) : "";
        String browserConfigurations = values.size() > 3 ? values.get(3) : "";
        try {
            sessionId = getNewBrowserSession(browserString, values.get(1), extensionJs,
                    BrowserOptions.newBrowserOptions(browserConfigurations));
            LoggingManager.perSessionLogHandler().attachToCurrentThread(new SessionId(sessionId));

            setDomain(sessionId, values.get(1));
            results = "OK," + sessionId;
        } catch (RemoteCommandException rce) {
            results = "Failed to start new browser session: " + rce;
        } catch (InvalidBrowserExecutableException ibex) {
            results = "Failed to start new browser session: " + ibex;
        } catch (IllegalArgumentException iaex) {
            results = "Failed to start new browser session: " + iaex;
        } catch (RuntimeException rte) {
            results = "Failed to start new browser session: " + rte;
        }
        // clear out any network traffic captured but never pulled back by the last client (this
        // feature only works with one concurrent browser, similar to PI mode)
        CaptureNetworkTrafficCommand.clear();

        break;
    case testComplete:
        browserSessionFactory.endBrowserSession(sessionId, remoteControl.getConfiguration());
        results = "OK";
        break;
    case getLog:
        try {
            results = "OK," + LoggingManager.perSessionLogHandler().getLog(new SessionId(sessionId));
        } catch (IOException ioex) {
            results = "Failed to get RC logs for the session: " + sessionId + " exception message: "
                    + ioex.getMessage();
        }
        break;
    case shutDownSeleniumServer:
        results = "OK";
        shutDown(res);
        break;
    case getLogMessages:
        results = "OK," + logMessagesBuffer.toString();
        logMessagesBuffer.setLength(0);
        break;
    case retrieveLastRemoteControlLogs:
        results = new RetrieveLastRemoteControlLogsCommand().execute();
        break;
    case captureEntirePageScreenshotToString:
        results = new CaptureEntirePageScreenshotToStringCommand(values.get(0), sessionId).execute();
        break;
    case captureScreenshot:
        results = new CaptureScreenshotCommand(values.get(0)).execute();
        break;
    case captureScreenshotToString:
        results = new CaptureScreenshotToStringCommand().execute();
        break;
    case captureNetworkTraffic:
        results = new CaptureNetworkTrafficCommand(values.get(0)).execute();
        break;
    case addCustomRequestHeader:
        results = new AddCustomRequestHeaderCommand(values.get(0), values.get(1)).execute();
        break;
    case keyDownNative:
        try {
            RobotRetriever.getRobot().keyPress(Integer.parseInt(values.get(0)));
            results = "OK";
        } catch (Exception e) {
            log.log(Level.SEVERE, "Problem during keyDown: ", e);
            results = "ERROR: Problem during keyDown: " + e.getMessage();
        }
        break;
    case keyUpNative:
        try {
            RobotRetriever.getRobot().keyRelease(Integer.parseInt(values.get(0)));
            results = "OK";
        } catch (Exception e) {
            log.log(Level.SEVERE, "Problem during keyUp: ", e);
            results = "ERROR: Problem during keyUp: " + e.getMessage();
        }
        break;
    case keyPressNative:
        try {
            Robot r = RobotRetriever.getRobot();
            int keycode = Integer.parseInt(values.get(0));
            r.keyPress(keycode);
            r.waitForIdle();
            r.keyRelease(keycode);
            results = "OK";
        } catch (Exception e) {
            log.log(Level.SEVERE, "Problem during keyDown: ", e);
            results = "ERROR: Problem during keyDown: " + e.getMessage();
        }
        // TODO typeKeysNative. Requires converting String to array of keycodes.
        break;
    case isPostSupported:
        results = "OK,true";
        break;
    case setSpeed:
        try {
            int speed = Integer.parseInt(values.get(0));
            setSpeedForSession(sessionId, speed);
        } catch (NumberFormatException e) {
            return "ERROR: setSlowMode expects a string containing an integer, but saw '" + values.get(0) + "'";
        }
        results = "OK";
        break;
    case getSpeed:
        results = getSpeedForSession(sessionId);
        break;
    case addStaticContent:
        File dir = new File(values.get(0));
        if (dir.exists()) {
            remoteControl.addNewStaticContent(dir);
            results = "OK";
        } else {
            results = "ERROR: dir does not exist - " + dir.getAbsolutePath();
        }
        break;
    case runHTMLSuite:
        HTMLLauncher launcher = new HTMLLauncher(remoteControl);
        File output = null;
        if (values.size() < 4) {
            results = "ERROR: Not enough arguments (browser, browserURL, suiteURL, multiWindow, [outputFile])";
        } else {
            if (values.size() > 4) {
                output = new File(values.get(4));
            }

            try {
                results = launcher.runHTMLSuite(values.get(0), values.get(1), values.get(2), output,
                        remoteControl.getConfiguration().getTimeoutInSeconds(), "true".equals(values.get(3)));
            } catch (IOException e) {
                e.printStackTrace();
                results = e.toString();
            }
        }
        break;
    case launchOnly:
        if (values.size() < 1) {
            results = "ERROR: You must specify a browser";
        } else {
            String browser = values.get(0);
            String newSessionId = generateNewSessionId();
            BrowserLauncher simpleLauncher = browserLauncherFactory.getBrowserLauncher(browser, newSessionId,
                    remoteControl.getConfiguration(), BrowserOptions.newBrowserOptions());
            String baseUrl = "http://localhost:" + remoteControl.getPort();
            remoteControl.registerBrowserSession(
                    new BrowserSessionInfo(newSessionId, browser, baseUrl, simpleLauncher, null));
            simpleLauncher.launchHTMLSuite("TestPrompt.html?thisIsSeleniumServer=true", baseUrl);
            results = "OK";
        }
        break;
    case slowResources:
        String arg = values.get(0);
        boolean setting = true;
        if ("off".equals(arg) || "false".equals(arg)) {
            setting = false;
        }
        StaticContentHandler.setSlowResources(setting);
        results = "OK";
        break;
    case attachFile:
        FrameGroupCommandQueueSet queue = getQueueSet(sessionId);
        try {
            File downloadedFile = downloadFile(values.get(1));
            queue.addTemporaryFile(downloadedFile);
            results = queue.doCommand("type", values.get(0), downloadedFile.getAbsolutePath());
        } catch (Exception e) {
            results = e.toString();
        }
        break;
    case open:
        warnIfApparentDomainChange(sessionId, values.get(0));
    case nonSpecial:
        results = new SeleniumCoreCommand(cmd, values, sessionId).execute();
    }

    log.info(commandResultsLogMessage(cmd, sessionId, results));
    return results;

}