Jacobvu84 / serenity-pageobject-junit-webdriver

4 stars 1 forks source link

Bài #38: Upload file #38

Closed Jacobvu84 closed 5 years ago

Jacobvu84 commented 5 years ago

Video hướng dẫn

Jacobvu84 commented 5 years ago
    /**
     * This method will set any parameter string to the system's clipboard.
     */
    private static void setClipboardData(String pathFile) {
        // StringSelection is a class that can be used for copy and paste operations.
        StringSelection stringSelection = new StringSelection(pathFile);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
    }

    public static void uploadFile(String fileLocation) {
        try {
            // Setting clip board with file location
            setClipboardData(fileLocation);
            // native key strokes for CTRL, V and ENTER keys
            Robot robot = new Robot();

            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            Thread.sleep(3000);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }