Example usage for java.awt Robot waitForIdle

List of usage examples for java.awt Robot waitForIdle

Introduction

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

Prototype

public synchronized void waitForIdle() 

Source Link

Document

Waits until all events currently on the event queue have been processed.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Robot robot = new Robot();

    robot.keyPress(KeyEvent.VK_A);
    robot.waitForIdle();
    robot.keyRelease(KeyEvent.VK_A);

}

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   ww  w  .ja  v  a2  s.c  om*/
    // 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;

}