Unable to upload a file using Windows Explorer

Hi All,

I used http://forum.katalon.com/discussion/2201/how-to-upload-a-file-from-windows-explorer/p1 to create an “uploadFile” keyword for use in my script.

The problem is that “Windows Explorer” opens but the file is never loaded.

I have determined the page I am working with does not include so I am using the “Keyword” instead.

Not sure if I am calling the “Keyword” correctly. Any insight would be very much appreciated.

Keywords.Tools.uploadFiles

package tools

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.checkpoint.CheckpointFactory

import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords

import com.kms.katalon.core.model.FailureHandling

import com.kms.katalon.core.testcase.TestCase

import com.kms.katalon.core.testcase.TestCaseFactory

import com.kms.katalon.core.testdata.TestData

import com.kms.katalon.core.testdata.TestDataFactory

import com.kms.katalon.core.testobject.ObjectRepository

import com.kms.katalon.core.testobject.TestObject

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords

import internal.GlobalVariable

import MobileBuiltInKeywords as Mobile

import WSBuiltInKeywords as WS

import WebUiBuiltInKeywords as WebUI

//Added for uploadFiles

import java.awt.Robot

import java.awt.Toolkit

import java.awt.datatransfer.StringSelection

import java.awt.event.KeyEvent

public class uploadFiles {

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

} }

from Script:

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.checkpoint.Checkpoint as Checkpoint

import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory

import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords

import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile

import com.kms.katalon.core.model.FailureHandling as FailureHandling

import com.kms.katalon.core.testcase.TestCase as TestCase

import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory

import com.kms.katalon.core.testdata.TestData as TestData

import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory

import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords

import internal.GlobalVariable as GlobalVariable

import org.openqa.selenium.Keys as Keys

import tools.SINGenerator as SINGenerator

//used for uploadFiles

import com.kms.katalon.core.testobject.TestObject as TestObject

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

Test steps:

WebUI.openBrowser(’’)

WebUI.navigateToUrl(GlobalVariable.testURL)

WebUI.maximizeWindow()

WebUI.delay(GlobalVariable.shortDelay)

WebUI.click(findTestObject(‘08_NEW_VW_ACCT/NEW_VW_ACCT3/Page_VirtualWealth/label_upload fa fa-upload fa-l’))

WebUI.focus(findTestObject(‘08_NEW_VW_ACCT/UploadFiles/Page_VirtualWealth/label_Upload your file’))

CustomKeywords.‘tools.uploadFiles.uploadFile’(findTestObject(‘08_NEW_VW_ACCT/UploadFiles/Page_VirtualWealth/label_Upload your file’),

'C:\\\\\\Users\\\\\\eversda\\\\\\Downloads\\\\\\TestFiles\\\\\\BlankCheque.gif')

FileUploadNotWorking.png

All,

I changed the call to the Keyword as follows but there was no difference (I removed the extra \s).

CustomKeywords.‘tools.uploadFiles.uploadFile’(findTestObject(‘08_NEW_VW_ACCT/UploadFiles/Page_VirtualWealth/label_Upload your file’), ‘C:\\\Users\\\eversda\\\Downloads\\\TestFiles\\\BlankCheque.gif’)

Can someone please give me an example of how to call the Keyword as I am stumped.

Thank you,
Dave

Update: “uploadFile” Keyword is NOT working:

I created a custom object named “UploadFile0” (see attached image)

I used the following test step to call “uploadFile”:

CustomKeywords.‘tools.uploadFiles.uploadFile’(findTestObject(‘FILE_UPLOAD/Page_VirtualWealth/UploadFile0’), ‘C:\\\Users\\\eversda\\\Downloads\\\TestFiles\\\BlankCheque.png’)

When I run the script the “uploadFile” keyword finds the object and opens “Windows Explorer”
& sits there.

I think the issue is that the “uploadFile” keyword never passes the to “Windows Explorer” for loading.

Katalon passes the step because it has found the object.

Has anyone had this same issue & if yes what solution did you use?

Is there a better / or proven Keyword that could work?

FileUploadNotWorking2.png

Forgot to load the custom object file…

UploadFile0.png

How did you get the xpath for the uploadFile0 object?
I have the same scenario as you but I cant quite figure out how you solved this.

What I mean by same scenario is that the custom keyword command brings clicks on the browse button, but it does not place the path in the file name text box and click open.

It actually did work once, then when I reran it, it hasnt worked since.

I have had similar issue, where it would work sometimes but not all the time, with the upload using robot. I think issue, is the robot operation is performed before the dialog is opened. I have had higher success rate when adding some delay. Try with 500 ms, or 1000 ms.

Robot robot = new Robot();

robot.keyPress(KeyEvent.VK_ENTER);

robot.keyRelease(KeyEvent.VK_ENTER);

robot.delay(500)

robot.keyPress(KeyEvent.VK_CONTROL);

robot.keyPress(KeyEvent.VK_V);

robot.keyRelease(KeyEvent.VK_V);

robot.delay(500)

robot.keyRelease(KeyEvent.VK_CONTROL);

robot.keyPress(KeyEvent.VK_ENTER);

robot.keyRelease(KeyEvent.VK_ENTER);

"uploadFiles" now works for me, thanks everyone for your help!

Keywords.tools.uploadFiles.groovy

//Added for use with “uploadFiles”

import java.awt.Robot

import java.awt.Toolkit

import java.awt.datatransfer.StringSelection

import java.awt.event.KeyEvent

public class uploadFiles {

@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.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);

} }

Note: Make sure to use the correct Object locators, in my case the following was true for “label_upload fa fa-upload fa-l”

//[@class = ‘upload fa fa-upload fa-lg pull-right’][count(. | //[@for = ‘chooseFile0’]) = count(//*[@for = ‘chooseFile0’])]

Script snippet…

WebUI.delay(GlobalVariable.shortDelay)

WebUI.comment(‘DO NOT click into Katalon while script is running. Doing so will cause uploadFiles to NOT work.’)

CustomKeywords.‘tools.uploadFiles.uploadFile’(findTestObject(‘FILE_UPLOAD/Page_VirtualWealth/label_upload fa fa-upload fa-l’),

'C:\\\Users\\\jdoe\\\Downloads\\\TestFiles\\\TestLicense.jpg')

Thread.sleep(2000) //waits for “Open” dialogue

WebUI.switchToWindowIndex(‘0’)

CustomKeywords.‘tools.uploadFiles.uploadFile’(findTestObject(‘FILE_UPLOAD/Page_VirtualWealth/label_upload fa fa-upload fa-l2’),

'C:\\\Users\\\jdoe\\\Downloads\\\TestFiles\\\TestPassport.png')

Thread.sleep(2000) //waits for “Open” dialogue

WebUI.switchToWindowIndex(0)

CustomKeywords.‘tools.uploadFiles.uploadFile’(findTestObject(‘FILE_UPLOAD/Page_VirtualWealth/label_upload fa fa-upload fa-l_1’),

'C:\\\Users\\\jdoe\\\Downloads\\\TestFiles\\\Bank.pdf')

Thread.sleep(2000) //waits for “Open” dialogue

WebUI.switchToWindowIndex(0)

CustomKeywords.‘tools.uploadFiles.uploadFile’(findTestObject(‘FILE_UPLOAD/Page_VirtualWealth/label_upload fa fa-upload fa-l_12’),

'C:\\\Users\\\jdoe\\\Downloads\\\TestFiles\\\Bill.pdf')

Thread.sleep(2000) //waits for “Open” dialogue

WebUI.switchToWindowIndex(0)

Forgot to add the following:

//Add these lines to script’s import section header

import com.kms.katalon.core.annotation.Keyword as Keyword

import com.kms.katalon.core.testobject.TestObject as TestObject

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

Hi Dave,

I used he code as above and it is throwing error a given below.

Script:

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.checkpoint.Checkpoint as 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 as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.annotation.Keyword as Keyword
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
Keywords.tools.uploadFiles.groovy

//Added for use with “uploadFiles”

import java.awt.Robot

import java.awt.Toolkit

import java.awt.datatransfer.StringSelection

import java.awt.event.KeyEvent

public class uploadFiles {

@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.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.openBrowser(’’)

WebUI.navigateToUrl(GlobalVariable.URL)

WebUI.setText(findTestObject(‘Page_WEX Pay Login/input_Username_ctl00ContentPla’), GlobalVariable.A_Username)

WebUI.setText(findTestObject(‘Page_WEX Pay Login (55)/input_ctl00ContentPlaceHolder1_1’), GlobalVariable.A_Password)

WebUI.click(findTestObject(‘Page_WEX Pay Login/input_Remember me_ctl00Content’))

WebUI.click(findTestObject(‘Page_WEX Pay Home/a_ADMIN’))

WebUI.delay(10)

WebUI.comment(‘DO NOT click into Katalon while script is running. Doing so will cause uploadFiles to NOT work.’)

CustomKeywords.‘tools.uploadFiles.uploadFile’(findTestObject(‘Object Repository/Page_WEX Pay Administration/button_UPLOAD USERS’),

‘C:\Users\Harini Sekar\minion gun.jpg’)
Thread.sleep(2000) //waits for “Open” dialogue

WebUI.switchToWindowIndex(0)

WebUI.click(findTestObject(‘Object Repository/Page_WEX Pay Administration/li_Line 2 StoreId already exis’))

WebUI.click(findTestObject(‘Object Repository/Page_WEX Pay Administration/a_ADMIN’))

WebUI.closeBrowser()

Error:

01-07-2019 11:28:12 AM Test Cases/Admin Transactions/Upload file try1

Elapsed time: 1.325s

Test Cases/Admin Transactions/Upload file try1 FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: Keywords for class: Script1546819933500
at Upload file try1.run(Upload file try1:19)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:224)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1546820888938.run(TempTestCase1546820888938.groovy:22)

Can some one help on this!!! please

harini,

I had the same problem - fixed with adding a semi-colon after the delay statements:

robot.delay(1000);

Hope that works for you too.

Cheers,

Jason