How to Upload a file from windows explorer

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

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”)

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

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?

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?

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

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

How to use this keyword in the script of test case? insert into which place? Do I need to revise any words in the above script? please state clearly. it would be more helpful if you attach some files to illustrate it.
upload file key word is not working in my test case.

1 Like

Wei Luo said:

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

How to use this keyword in the script of test case? insert into which place? Do I need to revise any words in the above script? please state clearly. it would be more helpful if you attach some files to illustrate it.
upload file key word is not working in my test case.

Wei,

You have to create a new Keyword by right click on Keywords > New > Keyword

if there is Keyword already exists just double click on it and add @Keyword on the top of def uploadFile(TestObject to, String filepath)

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

1 Like