How to Upload a file from windows explorer

Thank you for the information

Hello @Dave_Weil1,

Thank you for the above script. I tried using it and it works perfectly fine. The only trouble i have is trying to set the filename to be parameterised. Can you please let me know if we can send the file name as a parameter, and if yes how to go about it?
Thanks a lot in advance.

Thanks,
Shyju John

How to give .exe path for Mac. Can u help me?

Helloo

Winium works fine too :slight_smile:

how I can capture the button of upload file ??
when I run the test case I can’t see the file uploaded why ??
please answer me

Hello Shadakhabour ,
You look link in below .

Hi I am completely new to this . I am not able to understand what to specify in auto It.exe and what in katalon script . Can you please explain little more. I have to upload a file from my system . Thanks in Advance.

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

Thank you for your clear step by step guide, it has helped me several steps further.

However there are some parts that I don’t understand.

At step 4 you mention that a variable should be replaced with the variable that I use, I am unsure which part of the provided code you mean by this and what variable I should replace it with.
Could you explain what this variable does and where I should use it?

The provided script runs without errors when I let it click the Browse button on the page that I try to automate. This opens a Windows explorer popup window on a different page than the included URL. Then nothing else happens. See below for the used code:

CustomKeywords.'package_uploadFile.testclass.uploadFile' (findTestObject('Student/Paginas/Upload werkstuk/button_Select file'), 
    'C:\\\\Users\\joep\\Downloads\\Toetsupload.txt')

When I use the input type test object: //input [@type=‘file’] (this is not a visible object) and run the script I receive the following error;
com.kms.katalon.core.exception.StepFailedException: Unable to click on object

In my custom keyword the following imports show are warning that they are never used (I am not sure if this is related)
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

Some further notes on you post, I think step 2 is an accidental copy of step 1. It should have shown the creation of a new Keyword right?

I should point out that I have just started using Katalon a couple of days ago and I am quite inexperienced with coding in general.

Thank you in advance

With respect to this situation, you should note the variable names in the above code, which I have specified as bold. For example, if you have used the package name Package Test, please write it after the package statement.

Kullanmak zaman giriş tipi deney numunesini: // girişi [@ tipi = ‘Dosya’] (bu görünür bir nesne değil) ve aşağıdaki hata iletisini komut çalıştırmak;
com.kms.katalon.core.exception.StepFailedException: Unable to click on object

you should make sure that the object is correctly defined in the Object Repository section.

you must first create a custom keyword and use the code mentioned above for the keyword you created.

Then you should include and use the custom keyword in your project.

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
public class Upload{

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

}

use this it will work.

Complete steps and information can be found here:

  • Priyank

Apart from Robot framework is there any other way to upload file.
Have tried using WebUI.uploadFile() method but does not work.
If it’s Katalon issue can we get this fixed in next release ?

1 Like

I’ve never used it myself, but I’ve seen mention of this:

@ rohel.dawoodani

You can use AutoIT (Works on Windows OS Only) and you can try Sikuli as well.
I have tried WebUI.uploadFile() and it works as expected. is that not working at your end?

In case , if you have missed any step, you can refer blog mentioned above to cross check once.

@priyank.it

I tried WebUI.uploadFile() but it still did not worked for me. Can you help me with this one ?
I want to upload file from desktop window.

rohel.dawoodani

WebUI.uploadFile() - Upload file to an input with type = “file” .

if you need to handle windows file uploader then you need to take hep of some other frameworks like robot, sikuli, autoIT ,etc. You can check my blog where steps are mentioned for file upload using Robot framework.

Hope this helps.

How could I upload more than one file simultaneously to an input field?

@plaidshirtakos hello
To upload a single file, you can add the test step to the next line with copy - paste and apply the same operation in the other file.

@emine : I see, but I would like to upload all the two files in one request, input field is multiple type.