How to upload file in headless mode


#1

Hello,
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 {
    @Keyword
    def uploadFile (TestObject to, String filePath) {
    WebUI.click(to)
    WebUI.delay(2)
    StringSelection ss = new StringSelection(filePath);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

     Robot robot = new Robot();
     robot.keyPress(KeyEvent.VK_CONTROL);
     robot.delay(1000)
     robot.keyPress(KeyEvent.VK_V);
     robot.keyRelease(KeyEvent.VK_V);
     robot.delay(1000) //NOTE THE DELAY (500, 1000, 1500 MIGHT WORK FOR YOU)
     robot.keyRelease(KeyEvent.VK_CONTROL);
     robot.keyPress(KeyEvent.VK_ENTER);
     robot.keyRelease(KeyEvent.VK_ENTER);
    

    }
    }
    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’
    driver.findElement(By.xpath("//*[@id=‘app’]/div/section/div/section[1]/div[1]/div/button")).sendKeys(path);
    Not work both in default mode and headless mode.

Can someone help me on this?


#2

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.