[WebUI] Upload File


This is a companion discussion topic for the original entry at https://docs.katalon.com/katalon-studio/docs/webui-upload-file.html

this is not working.
image

My script:

Any other alternative?? Im not a coder Im pretty new in katalon

1 Like

hello. i have a problem the same. do you have any solutions ?

having the same issue

yeah that code is not working

Hello
You can look below link , please…
Regards

package uploadfile

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import java.awt.Robot
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
import java.awt.event.KeyEvent
import internal.GlobalVariable

public class cn {

@Keyword
def uploadFile (TestObject to, String filePath) {
	WebUI.click(to)
	StringSelection ss = new StringSelection(filePath);
	Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
	Robot robot = new Robot();
	robot.keyPress(KeyEvent.VK_ENTER);
	robot.keyRelease(KeyEvent.VK_ENTER);
	robot.keyPress(KeyEvent.VK_CONTROL);
	robot.keyPress(KeyEvent.VK_V);
	robot.keyRelease(KeyEvent.VK_V);
	robot.keyRelease(KeyEvent.VK_CONTROL);
	robot.keyPress(KeyEvent.VK_ENTER);
	robot.keyRelease(KeyEvent.VK_ENTER);
}

}

Encapsulate such a keyword and call it at execution time,you can try it.

Both solutions don’t work for me
it is opening upload windows dialog box, but then it is not inserting path of the file. When I make CTRL+click on upload file like here:

then some errors are shown:

“unable to resolve the class WebUICustomKeyword”

Looks like it is only working for Windows file system? I tried on Mac but I cannot make the path to work.

If I type on terminal open ///Users/oswaldo/Documents/file_name.png it opens the file in preview so the path is correct.

I provide the path WebUI.uploadFile(findTestObject('Upload drop/Page/input_Upload File_file'), '///Users/oswaldo/Documents/file_n ame.png')

BUT, when I get the file path with this FilePath = WebUI.getAttribute(findTestObject('Upload drop/Page_CloudApp/input_Upload File_file'), 'value')

I am getting C:\fakepath\file_name.png as FilePath, is it a bug? Is there a place to submit bugs?

At first this solution did not work for me, however I managed to make it work putting 2 Delays, after click on Browser Button and after paste the text and it worked perfect:

@Keyword
def uploadFile(TestObject to, String filePath) {
WebUI.click(to)
WebUI.delay(3) //Delay after click on Browser Button
StringSelection ss = new StringSelection(filePath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
WebUI.delay(1) //Delay after paste the text
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}

5 Likes

Dears how could upload file please i need your support/?

Even I need steps. Now File Upload is a plug-in. I have generated an API key for that plugin. But instillation steps are missing. Need help ASAP.

You can try to get the right userdir, i have it like that:

String userDir = System.getProperty('user.dir')
String filePath = userDir + '/Data Files/test.pdf'
WebUI.uploadFile(findTestObject('Object Repository/Administration/Gefahrstoffe/AD_GFS_LNK-SDB_TF-DokumentUpload'), filePath)

That way Katalon will get the right user path to it’s katalon directory on it’s own and you can add the rest.
But this way you need to have the files in your katalon directory (Data Files is a folder in there).

Edit:
You can use String userDir = System.getProperty('user.home')
to get to your home directory.

You can also use this to print out how katalon recognizes your directories.

Hello
I was able to manage run the file upload locally , and its running fine.
But I have issue to run it in gitlab. The gitlab runner like won´t search for the file even the path should be OK.

I know that file upload won’t work in headless browsers, therefore on git I’m running normal chrome
Does somebody managed solve similar issue?

hello ,I have tried this,but it didn’t run, even with no errors. Has anyone could help me?
WebUI.uploadFile(findTestObject('Object Repository/checkCar/Page_ -/image0'), 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563980425551&di=c171d1c03bdf3f3a1d03308dcbf4ac4d&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20170816%2Fede70cf3a497447fa423391b154fe7b0_th.jpg')
I’m sure the object is correct.

Thanks for your support

Thanks for contributing!

Should it work for Firefox? I’ve got a problem and noticed that the same TestCase that passed in Chrome, fails in Firefox. I searched of course for an answer but just found some old conversations about problems in Firefox.

Please note that using this method requires the Xpath of input_browse to be locked, otherwise it has no effect


HELP, I can’t upload the file. what’s wrong?