4

Im trying to upload a file from my projects root folder using Robot Framework and Java. When the popup window opens, the root file path is sent to popup but the popup never closes to submit the file. If i change the file path to the desktop the popup opens the desktop file path is submitted and popup closes submitting the file.

Heres my Robot class

package utils;

import static java.awt.event.KeyEvent.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class RobotWrite {

    private Robot robot;

    public RobotWrite() throws AWTException {
        this.robot = new Robot();
    }



    public void type(CharSequence characters) throws AWTException {
        int length = characters.length();
        for (int i = 0; i < length; i++) {
            char character = characters.charAt(i);
            type(character);
        }
    }

    public void type(char character) throws AWTException {
        switch (character) {
        case 'a': doType(VK_A); break;
        case 'b': doType(VK_B); break;
        .........
        .........
        case '?': doType(VK_SHIFT, VK_SLASH); break;
        case ' ': doType(VK_SPACE); break;
        default:
            throw new IllegalArgumentException("Cannot type character " + character);
        }
    }

    private void doType(int... keyCodes) {
        doType(keyCodes, 0, keyCodes.length);
    }

    private void doType(int[] keyCodes, int offset, int length) {
        if (length == 0) {
            return;
        }

        robot.keyPress(keyCodes[offset]);
        doType(keyCodes, offset + 1, length - 1);
        robot.keyRelease(keyCodes[offset]);
    }

}

And my calling method where i set the path..

public static void Robotwrite() throws Exception{
            try{
                 RobotWrite rw = new RobotWrite();            
                   rw.type("C:\\workspace\\project\\src\\main\\resources\\data\\ExampleCV.docx"); //doesnt work
                   //rw.type("C:\\Users\\Desktop\\ExampleCV.docx"); //works

                   Robot r = new Robot();
                   r.keyPress(VK_ENTER);
                   r.keyRelease(VK_ENTER);

            }catch (Exception e){
                 Log.error("Could not write");
                throw(e);
                }
            }
user1798578
  • 221
  • 3
  • 12
  • What does "I am unable to do so" mean? What is stopping you? Do you get an error? What error? Does the wrong file get uploaded? Also, please reduce the code down. See http://www.stackoverflow.com/help/mcve – Bryan Oakley Jan 16 '16 at 16:46
  • I dont get any error. The popup window opens, the file path is sent to popup but the popup never closes to submit the file – user1798578 Jan 16 '16 at 18:44

1 Answers1

-1

Normally you can solve this problem by sending the path of the file to the input element of the website (e.g. the upload button).

Storm
  • 728
  • 2
  • 7
  • 17