How to upload a file without opening file explorer (headless)

Hi I am trying to upload a file using WebUI.uploadFile. Since it was not working I used Robot class. But when I am running this in Jenkins I’m getting error. I believe robot class cannot support this.
How will I upload a file from my T drive without opening file explorer?

I used WebUI.uploadFile as below
WebUI.uploadFile(findTestObject(‘Object Repository/TP/PolicyQuestions/UploadNow’),‘T:\Katalon - Automation\PDFs\Sample.pdf’)

Xpath:
image

I used robot class as:
** crated a file within the project directory and took from there.
UploadFile(findTestObject(‘Object Repository/TP/PolicyQuestions/UploadNow’), ‘Sample.pdf’)

def UploadFile(TestObject to,String strFilePath) {
WebUI.doubleClick(to)
WebUI.delay(2)
String userDir = System.getProperty(“user.dir”)+“”+“\PDFDocuments\”+strFilePath+“”
WebUI.delay(4)
StringSelection ss = new StringSelection(userDir);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
WebUI.delay(2)
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
WebUI.delay(2)
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
WebUI.delay(2)
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(1000)
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.delay(1000)
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
WebUI.delay(2)
}

Anyone please help?

Hey @smathews, you can check the following post, which also deals with uploading files in headless mode.

I hope this helps.

Hi Chen
I developed the upload funtion using this post itself. as I said using robot file i can upload file but the file explorer will open to select the file.
Also, as the reply says UI has only tag. there is no tag

From what I found, a Robot class isn’t ideal for headless execution because the browser isn’t visible anyway. Best for you to try another method.

@smathews Your solution is to use the WebUI.fileUpload API – but you need to use it correctly.

The problem you have is your XPath is locating a button element which does not support uploading a file. You need to target the <input type='file'> element – it’s there somewhere, you just need to find it.

Here:

image

I was trying to get through id, but yes TYSM @Russ_Thomas for pointing. it worked!

I used the below code too.

def FileName = ‘T:\Katalon \TP \MON.pdf’
//Wait for the ‘UploadButton’ to display
WebUI.waitForElementVisible(findTestObject(‘Object Repository/TP/PolicyQuestions/UploadNow’), 30, FailureHandling.OPTIONAL)
WebUI.sendKeys(findTestObject(‘Object Repository/TP/UploadNow’), FileName)

thanks for the quick support.

1 Like

Hi what if the input is in file explorer? @Russ_Thomas

I don’t understand your question. This is about web testing, not desktop.

But regardless, how can the input (element) be in file explorer? (by that I assume you mean Windows Explorer – explorer.exe)

Good Morning!

What happens when you click the button from the UI " Add document", It opens up windows file explorer from my local machine

then in file explorer it looks for the file then state its not there? Not sure why

Well, you seem to be choosing a folder, not a file.

And technically, that’s the File Open dialog, I believe, not File Explorer.

I think you should read the rest of this thread; find the <input type='file'...> element.