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.hp.test.framework.jelly.ClickOKonWinDialogTag.java

@Override
public void doTag(XMLOutput arg0) throws MissingAttributeException {
    logger.info("Started Execution of ClickOkonWinDialogTag");
    WebDriver driver = getSelenium();//from  w  w  w .j  a  va  2  s .co m

    try {
        Robot robot = new Robot();

        //Pressky Enter/OK Button
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);

    } catch (AWTException e) {
        logger.error("Exception in the Click OK in Windows dialog" + "\n" + e.getMessage());
    }

    logger.info("Completed Execution of ClickOkonWinDialogTag");
}

From source file:io.github.bonigarcia.wdm.test.OpenNewTabChromeTest.java

@Test
public void test() throws Exception {
    // Open URL in default tab
    driver.get("https://wikipedia.org/");

    // If Mac OS X, the key combination is CMD+t, otherwise is CONTROL+t
    int vkControl = IS_OS_MAC ? VK_META : VK_CONTROL;
    Robot robot = new Robot();
    robot.keyPress(vkControl);//from w  w w.  ja  v a2  s  . c  o m
    robot.keyPress(VK_T);
    robot.keyRelease(vkControl);
    robot.keyRelease(VK_T);

    // Wait up to 5 seconds to the second tab to be opened
    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(numberOfWindowsToBe(2));

    // Switch to new tab
    List<String> windowHandles = new ArrayList<>(driver.getWindowHandles());
    System.err.println(windowHandles);
    driver.switchTo().window(windowHandles.get(1));

    // Open other URL in second tab
    driver.get("https://google.com/");
}

From source file:jdroidremote.ServerFrame.java

public static void typeCharacter(String letter) {
    System.out.println(letter);/* www. j  av  a2s  .  c  o  m*/
    Robot robot = null;
    try {
        robot = new Robot();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if (Character.isLetter(letter.charAt(0))) {
        try {
            boolean upperCase = Character.isUpperCase(letter.charAt(0));
            String variableName = "VK_" + letter.toUpperCase();

            KeyEvent ke = new KeyEvent(new JTextField(), 0, 0, 0, 0, ' ');
            Class clazz = ke.getClass();
            Field field = clazz.getField(variableName);
            int keyCode = field.getInt(ke);

            //System.out.println(keyCode + " = keyCode");
            robot.delay(80);

            if (upperCase) {
                robot.keyPress(KeyEvent.VK_SHIFT);
            }

            robot.keyPress(keyCode);
            robot.keyRelease(keyCode);

            if (upperCase) {
                robot.keyRelease(KeyEvent.VK_SHIFT);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    } else if (letter.equals(".")) {
        robot.keyPress(KeyEvent.VK_PERIOD); //keyCode 46
        robot.keyRelease(KeyEvent.VK_PERIOD);
    } else if (letter.equals("!")) {
        robot.keyPress(KeyEvent.VK_SHIFT); //keyCode 16
        robot.keyPress(KeyEvent.VK_1); //keycode 49
        robot.keyRelease(KeyEvent.VK_1);
        robot.keyRelease(KeyEvent.VK_SHIFT);
    } else if (letter.equals(" ")) {
        robot.keyPress(KeyEvent.VK_SPACE);
        robot.keyRelease(KeyEvent.VK_SPACE);
    } else if (letter.equals("?")) {
        robot.keyPress(KeyEvent.VK_SHIFT); //keyCode 16
        robot.keyPress(KeyEvent.VK_SLASH); //keyCode 47
        robot.keyRelease(KeyEvent.VK_SLASH);
        robot.keyRelease(KeyEvent.VK_SHIFT);
    } else if (letter.equals(",")) {
        robot.keyPress(KeyEvent.VK_COMMA);
        robot.keyRelease(KeyEvent.VK_COMMA);
    } else if (letter.equals("@enter")) {
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    } else if (letter.equals("@backspace")) {
        robot.keyPress(KeyEvent.VK_BACK_SPACE);
        robot.keyRelease(KeyEvent.VK_BACK_SPACE);
    }
}

From source file:com.hp.test.framework.jelly.SelectSaveandOKonDownloadDialogTag.java

@Override
public void doTag(XMLOutput arg0) throws MissingAttributeException {
    logger.info("Started Execution of SelectSaveandOKonDownloadDialogTag");
    WebDriver driver = getSelenium();/*from  w w  w. j a  v a 2 s  .  co m*/

    try {
        Robot robot = new Robot();

        //Pressky ALT+S
        robot.keyPress(KeyEvent.VK_ALT);
        robot.keyPress(KeyEvent.VK_S);

        //Release ALT
        robot.keyRelease(KeyEvent.VK_ALT);

        //Press Enter
        robot.keyPress(KeyEvent.VK_ENTER);

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

    } catch (AWTException e) {
        logger.error("Exception in the Save and Ok on Download dialog" + "\n" + e.getMessage());
    }

    logger.info("Completed Execution of SelectSaveandOKonDownloadDialogTag");
}

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

/**
 * Upload file typing file path directly
 * @param tempFile//w ww  .java2  s . com
 */
public static void uploadFileUsingKeyboardTyping(File tempFile) {
    try {
        Keyboard keyboard = new Keyboard();
        Robot robot = keyboard.getRobot();

        WaitHelper.waitForSeconds(1);

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

        keyboard.typeKeys(tempFile.getAbsolutePath());

        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 typing keys: " + e.getMessage());
    }
}

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

/**
 * Use copy to clipboard and copy-paste keyboard shortcut to write something on upload window
 *///from  w ww  .  j  a va 2 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.pentaho.ctools.issues.cde.CDE417.java

/**
 * ############################### Test Case 1 ###############################
 *
 * Test Case Name: // www  .  j  a  va  2s  . c o m
 *   Popup Export issues
 *
 * Description: 
 *   CDE-417 issue, so when user clicks to export chart, popup shown has chart preview. 
 *   CDE-424 issue we'll delete the .js file prior to opening the dashboard and saving the dashboard should create 
 *   the .js file again and enable exporting.
 *   CDF-448 exporting chart with skip ticks (chart is too small to show all labels) is successful
 *   CDF-502 export chart with array parameters is successful
 *   CDE-514 all charts are available on charts to export property and all elements are available on the data to export property
 *   
 * Steps:
 * 424
 *    1. Open PUC and click Browse Files
 *    2. Go to dashboard folder, click countryChart.js file and click Move To Trash
 *    3. Open created sample and save Dashboard
 * 514
 *    4. Go to Export Component and assert all charts appear on the Chart to Export property
 *    5. Assert all charts and the table appear on data to export property
 *    
 * 417/448/502
 *    6. Click to export chart, click export, assert chart is shown and assert export works as expected (448 and 502)
 *    7. Click to export chart2, click export, assert chart is shown and assert export works as expected
 * 
 * @throws InterruptedException
 *
 */
@Test
public void tc01_PopupExportComponent_PreviewerRendersChart() throws InterruptedException {
    this.log.info("tc01_PopupExportComponent_PreviewerRendersChart");

    /*
     * ## Step 1
     */
    //Show Hidden Files
    BrowseFiles browser = new BrowseFiles(driver);
    if (!PUCSettings.SHOWHIDDENFILES) {
        browser.CheckShowHiddenFiles();
    }

    /*
     * ## Step 2
     */
    //String pathChart = "/public/Issues/CDF/CDF-548/chart.js";
    //String pathCdfChart = "/public/Issues/CDF/CDF-548/CDF-548_chart.js";
    String pathCountryChart = "/public/Issues/CDF/CDF-548/countryChart.js";
    String pathCdfCountryChart = "/public/Issues/CDF/CDF-548/CDF-548_countryChart.js";
    //boolean fileDeleteChart = browser.DeleteFile( pathChart );
    //boolean fileDeleteCdfChart = browser.DeleteFile( pathCdfChart );
    boolean fileDeleteCountryChart = browser.DeleteFile(pathCountryChart);
    boolean fileDeleteCdfCountryChart = browser.DeleteFile(pathCdfCountryChart);
    //assertTrue( fileDeleteChart );
    //assertTrue( fileDeleteCdfChart );
    assertTrue(fileDeleteCountryChart);
    assertTrue(fileDeleteCdfCountryChart);

    driver.switchTo().defaultContent();
    WebDriver frame = driver.switchTo().frame("browser.perspective");
    this.elemHelper.WaitForAttributeValue(frame, By.xpath("//div[@id='fileBrowserFiles']/div[2]/div[1]"),
            "title", "CDF-548.cda");
    String nameOfCdf548Wcdf = this.elemHelper.GetAttribute(frame,
            By.xpath("//div[@id='fileBrowserFiles']/div[2]/div[1]"), "title");
    assertEquals("CDF-548.cda", nameOfCdf548Wcdf);

    /*
     * ## Step 3
     */
    // Go to Export Popup Component sample in Edit mode
    driver.get(baseUrl + "api/repos/%3Apublic%3AIssues%3ACDF%3ACDF-548%3ACDF-548.wcdf/edit");

    //Save Dashboard
    WebElement saveButton = this.elemHelper.WaitForElementPresenceAndVisible(driver, By.id("Save"));
    assertNotNull(saveButton);
    this.elemHelper.Click(driver, By.id("Save"));
    WebElement saveNotify = this.elemHelper.WaitForElementPresenceAndVisible(driver,
            By.cssSelector("div.notify-bar-message"));
    assertNotNull(saveNotify);
    String saveMessage = this.elemHelper.WaitForElementPresentGetText(driver,
            By.cssSelector("div.notify-bar-message"));
    assertEquals("Dashboard saved successfully", saveMessage);

    /* 
     * ## Step 4 
      */
    //Go to components panel and expand "Others"
    WebElement componentsPanelButton = this.elemHelper.FindElement(driver,
            By.cssSelector("div.componentsPanelButton"));
    assertNotNull(componentsPanelButton);
    componentsPanelButton.click();
    WebElement componentsTable = this.elemHelper.FindElement(driver, By.id("cdfdd-components-components"));
    assertNotNull(componentsTable);
    WebElement othersExpander = this.elemHelper.FindElement(driver,
            By.xpath("//table[@id='table-cdfdd-components-components']//tr[@id='OTHERCOMPONENTS']/td/span"));
    assertNotNull(othersExpander);
    othersExpander.click();

    //Select Export Popup Component
    WebElement exportPopupElement = this.elemHelper.FindElement(driver,
            By.xpath("//table[@id='table-cdfdd-components-components']//td[contains(text(),'exportPopup')]"));
    assertNotNull(exportPopupElement);
    exportPopupElement.click();

    //Open options for Chart Component to Export and assert "chart" and "countryChart"
    WebElement chartExportProperty = this.elemHelper.FindElement(driver, By.xpath(
            "//table[@id='table-cdfdd-components-properties']//td[@title='Id for Chart Component to Export']/../td[2]"));
    assertNotNull(chartExportProperty);
    chartExportProperty.click();
    WebElement chartExportInput = this.elemHelper.FindElement(driver, By.xpath(
            "//table[@id='table-cdfdd-components-properties']//td[@title='Id for Chart Component to Export']/../td[2]/form/input"));
    assertNotNull(chartExportInput);
    chartExportInput.clear();
    Robot robot;
    try {
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_DOWN);
        robot.keyRelease(KeyEvent.VK_DOWN);
    } catch (AWTException e) {
        e.printStackTrace();
    }
    WebElement chartExportOptions = this.elemHelper.FindElement(driver, By.xpath("//body/ul"));
    assertNotNull(chartExportOptions);
    String chartOption1 = this.elemHelper.WaitForElementPresentGetText(driver, By.xpath("//body/ul/li/a"));
    String chartOption2 = this.elemHelper.WaitForElementPresentGetText(driver, By.xpath("//body/ul/li[2]/a"));
    assertEquals("chart", chartOption1);
    assertEquals("countryChart", chartOption2);
    this.elemHelper.Click(driver, By.xpath("//body/ul/li/a"));

    /* 
     * ## Step 5 
      */
    //Open options for Data Component to Export and assert "table", "chart" and "countryChart"
    WebElement dataExportProperty = this.elemHelper.FindElement(driver, By.xpath(
            "//table[@id='table-cdfdd-components-properties']//td[@title='Id for Data Component to Export']/../td[2]"));
    assertNotNull(dataExportProperty);
    dataExportProperty.click();
    WebElement dataExportInput = this.elemHelper.FindElement(driver, By.xpath(
            "//table[@id='table-cdfdd-components-properties']//td[@title='Id for Data Component to Export']/../td[2]/form/input"));
    assertNotNull(dataExportInput);
    dataExportInput.clear();
    Robot robot1;
    try {
        robot1 = new Robot();
        robot1.keyPress(KeyEvent.VK_DOWN);
        robot1.keyRelease(KeyEvent.VK_DOWN);
    } catch (AWTException e) {
        e.printStackTrace();
    }
    WebElement dataExportOptions = this.elemHelper.FindElement(driver, By.xpath("//body/ul"));
    assertNotNull(dataExportOptions);
    String dataOption1 = this.elemHelper.WaitForElementPresentGetText(driver, By.xpath("//body/ul/li/a"));
    String dataOption2 = this.elemHelper.WaitForElementPresentGetText(driver, By.xpath("//body/ul/li[2]/a"));
    String dataOption3 = this.elemHelper.WaitForElementPresentGetText(driver, By.xpath("//body/ul/li[3]/a"));
    assertEquals("table", dataOption1);
    assertEquals("chart", dataOption2);
    assertEquals("countryChart", dataOption3);

    /* 
     * ## Step 6 
     */
    driver.get(baseUrl + "api/repos/%3Apublic%3AIssues%3ACDF%3ACDF-548%3ACDF-548.wcdf/generatedContent");
    this.elemHelper.WaitForElementInvisibility(driver, By.cssSelector("div.blockUI.blockOverlay"));

    //Click export Button
    WebElement exportChartButton = this.elemHelper.FindElement(driver, By.xpath("//div[@id='export']/div"));
    assertNotNull(exportChartButton);
    exportChartButton.click();

    //Assert popup and click Export Chart link
    WebElement exportChartLink = this.elemHelper.FindElement(driver, By.cssSelector("div.exportElement"));
    assertNotNull(exportChartLink);
    exportChartLink.click();

    //Assert chart popup
    WebElement exportChartPopup = this.elemHelper.FindElement(driver, By.id("fancybox-content"));
    assertNotNull(exportChartPopup);

    // Check URL of displayed image
    String chartSRCUrl = this.elemHelper.GetAttribute(driver,
            By.xpath("//div[@id='fancybox-content']/div/div/div/div[2]/img"), "src");
    assertEquals(baseUrl
            + "plugin/cgg/api/services/draw?outputType=png&script=%2Fpublic%2FIssues%2FCDF%2FCDF-548%2Fchart.js&paramcountries=France&paramcountries=USA&paramwidth=400&paramheight=400",
            chartSRCUrl);
    assertEquals(200, HttpUtils.GetResponseCode(chartSRCUrl, "admin", "password"));

    // Export chart and assert export was successful
    WebElement chartExportButton = this.elemHelper.FindElement(driver,
            By.cssSelector("div.exportChartPopupButton.exportChartOkButton"));
    assertNotNull(chartExportButton);

    //Click export and assert file is correctly downloaded
    try {
        //Delete the existence if exist
        new File(this.exportFilePath).delete();

        //Click to export
        chartExportButton.click();

        //Wait for file to be created in the destination dir
        DirectoryWatcher dw = new DirectoryWatcher();
        dw.WatchForCreate(downloadDir);

        //Check if the file really exist
        File exportFile = new File(this.exportFilePath);
        // assertTrue(exportFile.exists());

        //Wait for the file to be downloaded totally
        for (int i = 0; i < 50; i++) { //we only try 50 times == 5000 ms
            long nSize = FileUtils.sizeOf(exportFile);
            //Since the file always contents the same data, we wait for the expected bytes
            if (nSize >= 10000) {
                break;
            }
            this.log.info("BeforeSleep " + nSize);
            Thread.sleep(100);
        }

        this.log.info("File size :" + FileUtils.sizeOf(exportFile));

        //Check if the file downloaded is the expected
        String md5 = DigestUtils.md5Hex(Files.readAllBytes(exportFile.toPath()));
        assertEquals(md5, "c8c3ad02461f7b5c8a36ee2aec0eca95");

        //The delete file
        DeleteFile();

    } catch (Exception e) {
        this.log.error(e.getMessage());
    }

    // Close dialog box
    this.elemHelper.Click(driver, By.id("fancybox-close"));
    assertTrue(this.elemHelper.WaitForElementNotPresent(driver,
            By.xpath("//div[@id='fancybox-content']/div/div/div/div/div[1]")));

    /* 
     * ## Step 7
     */
    this.elemHelper.WaitForElementInvisibility(driver, By.cssSelector("div.exportElement"));
    //Click export Button
    WebElement exportCountryChartButton = this.elemHelper.FindElement(driver,
            By.xpath("//div[@id='export2']/div"));
    assertNotNull(exportCountryChartButton);
    exportCountryChartButton.click();

    //Assert popup and click Export Chart link
    WebElement exportCountryChartLink = this.elemHelper.FindElement(driver,
            By.xpath("//div[contains(text(),'Export Chart2')]"));
    assertNotNull(exportCountryChartLink);
    exportCountryChartLink.click();

    //Assert chart popup
    WebElement exportCountryChartPopup = this.elemHelper.FindElement(driver, By.id("fancybox-content"));
    assertNotNull(exportCountryChartPopup);

    // Check URL of displayed image
    String countryChartSRCUrl = this.elemHelper.GetAttribute(driver,
            By.xpath("//div[@id='fancybox-content']/div/div/div/div[2]/img"), "src");
    assertEquals(baseUrl
            + "plugin/cgg/api/services/draw?outputType=svg&script=%2Fpublic%2FIssues%2FCDF%2FCDF-548%2FcountryChart.js&paramwidth=400&paramheight=300",
            countryChartSRCUrl);
    assertEquals(200, HttpUtils.GetResponseCode(countryChartSRCUrl, "admin", "password"));

    // Export chart and assert export was successful
    WebElement countryChartExportButton = this.elemHelper.FindElement(driver,
            By.cssSelector("div.exportChartPopupButton.exportChartOkButton"));
    assertNotNull(countryChartExportButton);

    //Click export and assert file is correctly downloaded
    try {
        //Delete the existence if exist
        new File(this.exportFilePath2).delete();

        //Click to export
        countryChartExportButton.click();

        //Wait for file to be created in the destination dir
        DirectoryWatcher dw = new DirectoryWatcher();
        dw.WatchForCreate(downloadDir);

        //Check if the file really exist
        File exportFile = new File(this.exportFilePath2);
        // assertTrue(exportFile.exists());

        //Wait for the file to be downloaded totally
        for (int i = 0; i < 50; i++) { //we only try 50 times == 5000 ms
            long nSize = FileUtils.sizeOf(exportFile);
            //Since the file always contents the same data, we wait for the expected bytes
            if (nSize >= 30000) {
                break;
            }
            this.log.info("BeforeSleep " + nSize);
            Thread.sleep(100);
        }

        this.log.info("File size :" + FileUtils.sizeOf(exportFile));

        //Check if the file downloaded is the expected
        String md5 = DigestUtils.md5Hex(Files.readAllBytes(exportFile.toPath()));
        assertEquals(md5, "c0d04625306dd1f32afed115c0bf94be");

        //The delete file
        DeleteFile();

    } catch (Exception e) {
        this.log.error(e.getMessage());
    }

    // Close dialog box
    this.elemHelper.Click(driver, By.id("fancybox-close"));
    assertTrue(this.elemHelper.WaitForElementNotPresent(driver,
            By.xpath("//div[@id='fancybox-content']/div/div/div/div/div[1]")));
}

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

/**
 * javascript executer which must contains one object to be affected
 * /*from   w  ww. ja va 2  s.c o  m*/
 * @param js
 * @param by
 * 
 */
public void uploadFile(String filepath) {
    try {
        //put file path in a clipboard
        StringSelection strSel = new StringSelection(filepath);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(strSel, null);
        //imitate mouse event ENTER/COPY/PASTE
        Robot robot = new Robot();
        pause(500);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        logger.info("Success to upload file: " + filepath);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

/**
 * Open new tab in browser/*from   w ww. j  a  v  a2  s.  com*/
 */
public void openNewTAB() {
    pause(stepInterval);
    try {
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_T);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_T);
    } catch (Exception e) {
        e.printStackTrace();
        handleFailure("Failed to open new TAB");
    }
    logger.info("Success to open new TAB");
}