How to upload file in headless mode


I’m trying to run file upload in headless mode, I have tried different ways, but not work. How can I get file upload in headless mode?

  1. The import button in DOM is like below:

    It is not input tag, I’m not sure if this will impact on the result ?
  2. I need it to run in headless mode.

Following are all the ways I have tried:

  1. Using WebUI.uploadFile keyword
    Not work in default mode and headless mode.

  2. Define custom Keywords like below:
    public class MyTools {
    def uploadFile (TestObject to, String filePath) {
    StringSelection ss = new StringSelection(filePath);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

     Robot robot = new Robot();
     robot.delay(1000) //NOTE THE DELAY (500, 1000, 1500 MIGHT WORK FOR YOU)

    This way can run in default Chrome mode, but will always fail in headless mode.

  3. Use following code:
    WebDriver driver = DriverFactory.getWebDriver()
    String path = ‘D:\Daily task\New PW User.csv’
    Not work both in default mode and headless mode.

Can someone help me on this?


1.) There should be no difference in the effectiveness of headless testing versus your normal browser testing. Is the upload working when you try any of your methods with a non-headless run?

2.) Are you sure that the only element involved in the upload is the <button> element you’ve shown us? This is highly unlikely (usually it would be an <input> element that would recieve the path to the file you are trying to upload). Can you share a screenshot of the GUI when you are uploading the file? My guess is that you hit the button, then you should get another field to actually receive the upload path.