How to Upload a file from windows explorer

Hello I will try to tell you step by step:
You must first define a custom keyword to select a local file and upload it to the project.
To create Custom Keyword:
1-As you will see in the following image; First of all, after creating a new project, right click on the Keywords file in Tests Explorer and follow the steps in the following image and add a new package.


2-
3-Copy the following code to the drop-down page.

package test

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 com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable

public class testclass {

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

4-At this stage, you need to pay attention to your own package and function names. You must update the code above so that you have used the variable name.above I marked it as dark for you.

5-After this stage, you must include the custom keyword structure in the test step. Please review the screenshots below.

6-This keyword takes 2 parameters. The first parameter is the name of the field on the web page you want to upload, and the second parameter is the path of the file in the locale.

In Script mode you should see a code as follows.

CustomKeywords.’test.testclass.uploadFile’(findTestObject(‘ Input_File’), ‘C:\Users\Desktop\test data’)

I hope it is helpful…

3 Likes