How to Upload a file from windows explorer


#1

Hi,

in my project , i can able to click on a button to upload after that i cannot able to select a a file from the windows explorer path to upload. please help me with an example


#2

Use this below code:Simple just load the upload object with the file which you placed in the local system.
Like load the object with you local file path.
WebUI.uploadFile(findTestObject(‘ASACoverObjs/ChooseDocFileBtnObj’), “F:\\AutomationProjects\\BeautyBookingWebSite\\BeautyBookingWebsiteAutomationProject21042017\\Data Files\\TestDataFiles\\Test Data Doc file.docx”)


#3

That keyword will:

  1. Click on ‘Upload’ button on the web application

  2. Select the file based on the input path to that keyword.

That’s all for how of this keyword behaves.

Can you please tell me more about why it didn’t work from your side after you’ve tried it?


#4

Actually this didnt work. Moreover it does not have an image to understand how this works.

Once the attachment button is clicked it navigates to windows explorer to choose the file but we are not sure how to go after that.

whether we need to find any other way or any integartion is required like auto it to support this upload feature?


#5

Please use ‘Upload File’ keyword. Example is within that page.


#6

The UploadFile keyword only works if your field is . Please check again. If it isn’t, please use this keyword.

import java.awt.Robot
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
import java.awt.event.KeyEvent

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

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


#7

The solution is also not working, please suggest some other way to upload file from windows explorer.


#8

It works from my side. Please note that it will not work in all cases due to different Upload control from the current web application, some cases the Upload button is not visible or it is not shown completely until you do some actions


#9

WebUI.uploadFile(findTestObject(‘QA2_OBP_OR/SettingsInTransactionPage_OR/input_statement’), ‘C:\\files\\Images\\bmp-sample.bmp’)


#10

ok will check the above methods.


#11

None of this works for me.

WebUI.uploadFile(findTestObject(‘Page_SubmitJob/label_Choose a data file’), ‘C:\\Mail.json’)

I does not even click the button at all. It does click when I use:

WebUI.click(findTestObject(‘Page_SubmitJob/label_Choose a data file’))

but then I have NO way of inputting path\filename. It’s a native explorer window and I cannot capture anything about it. Is there no way to just send text to wherever the cursor is? Why does it need to have an object associated with it. Sigh.


#12

duyluong said:

The UploadFile keyword only works if your field is . Please check again. If it isn’t, please use this keyword.

import java.awt.Robot
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
import java.awt.event.KeyEvent

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

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

I set this up as a custom keyword and have it as:

CustomKeywords.‘kw.WebUICustomKeywords.uploadFileCustom’(findTestObject(‘Page_SubmitJob/label_Choose a data file’), ‘C:\\Mail.json’)

It will click the button now, but then the text does not get entered. Any ideas?


#13

See: https://forum.katalon.com/discussion/5231/when-upload-file-or-send-keys-will-not-upload-files-for-your-test-case


#14

I just used autoIT and it works perfectly for this situation. The window being a native Explorer window nothing would pass to it from Katalon. All good now. Thanks for reply @Dave Evers


#15

Using below code to upload file but its not working

WebUI.uploadFile(findTestObject(‘Object Repository/NewJobType/Page_GIGKYAdmin/label_Choose image’), '/Users/hassan/Desktop/Random Files/IK.png ')

When I use WebUI.Click(), it just opens system’s file uploading dialogue box. Can anybody guide how to go through it as we cannot spy objects on native dialogue box or tell any other convenient method to upload files(mostly images)


#16

@Dave Weil Following your code, i am getting this error

FAILED because (of) Variable ‘Toolkit’ is not defined for test case.


#17

@Hassan ShafiqI ended up just creating an autoIT file to pass this in. I could not get Katalon to recognize the native explorer window. It works flawlessly now. My little blurb looks like this:

//Click the Browse for file button…
WebUI.click(findTestObject(‘Page_SubmitJob/label_BrowseForFile’))
//Sleep to wait for window to open.
Thread.sleep(1000)
//Load up the autoIT script using the first variable specified.
autoit_prj = ‘C:\\insertVariableExplorerWindow.exe \“C:\\Filename.ext\”’
//Run the autoIT script.
Runtime.getRuntime().exec(autoit_prj)

The autoIT looks like:
ControlSetText("","","",$CmdLine[1])
Send("{ENTER}")

So it takes the first variable I am passing above which is the path and filename to upload and then it hits ENTER to get rid of the window.


#18

Dave Weil said:

I just used autoIT and it works perfectly for this situation. The window being a native Explorer window nothing would pass to it from Katalon. All good now. Thanks for reply @Dave Evers

Hi @Dave Weil, is it possible that in Katalon, we have this string that contains a filepath and then you pass this string to the AutoIT script. is this doable when using AutoIT? if so, can you share your sample script in Katalon and in AutoIT?


#19

Dave Weil said:

@Hassan ShafiqI ended up just creating an autoIT file to pass this in. I could not get Katalon to recognize the native explorer window. It works flawlessly now. My little blurb looks like this:

//Click the Browse for file button…
WebUI.click(findTestObject(‘Page_SubmitJob/label_BrowseForFile’))
//Sleep to wait for window to open.
Thread.sleep(1000)
//Load up the autoIT script using the first variable specified.
autoit_prj = ‘C:\\insertVariableExplorerWindow.exe \“C:\\Filename.ext\”’
//Run the autoIT script.
Runtime.getRuntime().exec(autoit_prj)

The autoIT looks like:
ControlSetText("","","",$CmdLine[1])
Send("{ENTER}")

So it takes the first variable I am passing above which is the path and filename to upload and then it hits ENTER to get rid of the window.

Can you please tell me if your solution works when you run Chrome in SCRIPT MODE??


#20

Thank you for the information