Example usage for java.awt Robot Robot

List of usage examples for java.awt Robot Robot

Introduction

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

Prototype

public Robot() throws AWTException 

Source Link

Document

Constructs a Robot object in the coordinate system of the primary screen.

Usage

From source file:com.thoughtworks.selenium.SeleneseTestNgHelperVir.java

/**
 * Inits the robot./*from  w w  w.j a v  a 2s . c  o m*/
 */
public static synchronized void initRobot() {
    try {
        robot = new Robot();
    } 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 a va2s .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:com.cisco.dbds.utils.selenium.SeleniumUtilities.java

/**
 * This function captures the Screenshot of the screen and converts it into
 * a byte array to embed in cucumber reports.
 * /*from   w ww . jav a 2 s  .co  m*/
 * @return the byte[]
 * @throws Exception
 *             the exception
 */
public static byte[] captureScreen() throws Exception {

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle screenRectangle = new Rectangle(screenSize);
    Robot robot = new Robot();
    BufferedImage image = robot.createScreenCapture(screenRectangle);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", baos);
    baos.flush();
    byte[] bytes = baos.toByteArray();
    baos.close();
    return bytes;

}

From source file:com.netease.dagger.BrowserEmulator.java

/**
 * Hover on the page element//  www .  j a va  2  s. c o m
 * 
 * @param xpath
 *            the element's xpath
 */
public void mouseOver(String xpath) {
    pause(stepInterval);
    expectElementExistOrNot(true, xpath, timeout);
    // First make mouse out of browser
    Robot rb = null;
    try {
        rb = new Robot();
    } catch (AWTException e) {
        e.printStackTrace();
    }
    rb.mouseMove(0, 0);

    // Then hover
    WebElement we = browserCore.findElement(By.xpath(xpath));

    if (GlobalSettings.browserCoreType == 2) {
        try {
            Actions builder = new Actions(browserCore);
            builder.moveToElement(we).build().perform();
        } catch (Exception e) {
            e.printStackTrace();
            handleFailure("Failed to mouseover " + xpath);
        }

        logger.info("Mouseover " + xpath);
        return;
    }

    // Firefox and IE require multiple cycles, more than twice, to cause a
    // hovering effect
    if (GlobalSettings.browserCoreType == 1 || GlobalSettings.browserCoreType == 3) {
        for (int i = 0; i < 5; i++) {
            Actions builder = new Actions(browserCore);
            builder.moveToElement(we).build().perform();
        }
        logger.info("Mouseover " + xpath);
        return;
    }

    // Selenium doesn't support the Safari browser
    if (GlobalSettings.browserCoreType == 4) {
        Assert.fail("Mouseover is not supported for Safari now");
    }
    Assert.fail("Incorrect browser type");
}

From source file:com.seleniumtests.driver.CustomEventFiringWebDriver.java

/**
 * Use copy to clipboard and copy-paste keyboard shortcut to write something on upload window
 *///from  ww w  .  j av a2 s  .  c  o  m
public static void uploadFileUsingClipboard(File tempFile) {

    // Copy to clipboard
    Toolkit.getDefaultToolkit().getSystemClipboard()
            .setContents(new StringSelection(tempFile.getAbsolutePath()), null);
    Robot robot;
    try {
        robot = new Robot();

        WaitHelper.waitForSeconds(1);

        //         // Press Enter
        //         robot.keyPress(KeyEvent.VK_ENTER);
        //   
        //         // 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);
        WaitHelper.waitForSeconds(1);

        // Press Enter
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    } catch (AWTException e) {
        throw new ScenarioException("could not initialize robot to upload file: " + e.getMessage());
    }
}

From source file:com.tascape.qa.th.Utils.java

/**
 *
 * @param png picture file name/*www  .  j  av a  2 s  .com*/
 *
 * @throws AWTException UI related exception
 * @throws IOException  file IO issue
 */
public static void captureScreen(File png) throws AWTException, IOException {
    Robot robot = new Robot();
    BufferedImage image = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    ImageIO.write(image, "png", png);
    LOG.debug("Save screenshot to {}", png.getAbsolutePath());
}

From source file:com.netease.dagger.BrowserEmulator.java

/**
 * Mimic system-level keyboard event//from  w  w w  .  j  a va  2 s .c o m
 * 
 * @param keyCode
 *            http://www.cnblogs.com/hsapphire/archive/2009/12/16/1625642.html
 */
public void pressKeyboard(int keyCode) {
    pause(stepInterval);
    Robot rb = null;
    try {
        rb = new Robot();
    } catch (AWTException e) {
        e.printStackTrace();
    }
    rb.keyPress(keyCode); // press key
    rb.delay(100); // delay 100ms
    rb.keyRelease(keyCode); // release key
    logger.info("Pressed key with code " + keyCode);
}

From source file:com.seleniumtests.driver.CustomEventFiringWebDriver.java

/**
 * Left clic at coordinates on desktop. Coordinates are from screen point of view
 * @param x//w  w  w .ja  v a  2 s . c  o m
 * @param y
 */
public static void leftClicOnDesktopAt(int x, int y, DriverMode driverMode,
        SeleniumGridConnector gridConnector) {

    if (driverMode == DriverMode.LOCAL) {
        try {
            Robot robot = new Robot();
            moveMouse(robot, x, y);
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        } catch (AWTException e) {
            throw new ScenarioException("leftClicOnDesktopAt: problem using Robot: " + e.getMessage());
        }
    } else if (driverMode == DriverMode.GRID && gridConnector != null) {
        gridConnector.leftClic(x, y);
    } else {
        throw new ScenarioException("driver supports leftClicOnDesktopAt only in local and grid mode");
    }
}

From source file:com.seleniumtests.driver.CustomEventFiringWebDriver.java

/**
 * Left clic at coordinates on desktop. Coordinates are from screen point of view
 * @param x/*  w ww  .j ava  2s.c o  m*/
 * @param y
 */
public static void doubleClickOnDesktopAt(int x, int y, DriverMode driverMode,
        SeleniumGridConnector gridConnector) {

    if (driverMode == DriverMode.LOCAL) {
        try {
            Robot robot = new Robot();
            moveMouse(robot, x, y);
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            robot.delay(10);
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        } catch (AWTException e) {
            throw new ScenarioException("doubleClickOnDesktopAt: problem using Robot: " + e.getMessage());
        }
    } else if (driverMode == DriverMode.GRID && gridConnector != null) {
        gridConnector.doubleClick(x, y);
    } else {
        throw new ScenarioException("driver supports doubleClickOnDesktopAt only in local and grid mode");
    }
}

From source file:com.seleniumtests.driver.CustomEventFiringWebDriver.java

/**
 * right clic at coordinates on desktop. Coordinates are from screen point of view
 * @param x//from   w w w  .j  a  v  a 2s  .  c om
 * @param y
 */
public static void rightClicOnDesktopAt(int x, int y, DriverMode driverMode,
        SeleniumGridConnector gridConnector) {

    if (driverMode == DriverMode.LOCAL) {
        try {
            Robot robot = new Robot();
            moveMouse(robot, x, y);
            robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
        } catch (AWTException e) {
            throw new ScenarioException("rightClicOnDesktopAt: problem using Robot: " + e.getMessage());
        }
    } else if (driverMode == DriverMode.GRID && gridConnector != null) {
        gridConnector.rightClic(x, y);
    } else {
        throw new ScenarioException("driver supports sendKeysToDesktop only in local and grid mode");
    }
}