[WebUI] Upload File


#1

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

#2

this is not working.
image

My script:

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


#3

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


#4

having the same issue


#5

yeah that code is not working


#6

Hello
You can look below link , please…
Regards


#7

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.


#8

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”


#9

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?


#10

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);
}


#11

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


#12

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.


#13

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.


#14

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?