Katalon Studio Native Popup Window and Upload File

In this article, I would like to mention the solution I found regarding uploading any local file in the test step during the test with Native Popup Window.

All the objects we record with the record in the test case are the descriptions in the relevant web page.

However, if you select a local file from anywhere in the project and have a step to install, the test step will be blocked in this section and you will not be able to select the file from the local and select it in the test step.

For this case, it will be more functional to create a custom keyword that performs this process step and to perform file upload with this keyword in the related steps of the test cases.

Then let’s start.

I have a file upload area in my test project below. When I click on this field while saving the project steps with record, Native Popup Window opens and I have to select a file from local. I’m selecting and installing the file, but Catalon Studio can’t save my processing steps in the popup.

res1 res23

As a solution to this problem, we should first select a local file and create a custom keyword that will perform the installation process.

To add a custom keyword, you can get help from the link below.
https://docs.katalon.com/katalon-studio/tutorials/create_custom_keyword.html

** STEP 1: ** The content of the following code is added to the keyword content we create.

Here are the following:

***** package ** test ** must be the same as the package name you defined when creating a custom keyword.

  • public class ** testclass ** must be the same as the keyword name you defined when creating a custom keyword.

  • The following libraries in the code should be imported because they are not defined by default when the keyword is defined.

** 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 **

  • Finally, ** def uploadFile (TestObject to, String filePath) ** The keyword function is created. The first parameter consists of the input field in which the file is to be loaded, the other parameter will be used to refer to the file path of the file to be loaded.

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

** STEP 2: ** The defined custom keyword is added to the test step. Then, the content of the related step in the script field should be expressed as follows.

CustomKeywords. testtest.testclass.uploadFile '(findTestObject, ile C: \ Users \ Desktop \ test data file )

The file related to these steps can be uploaded on the web page.

In your projects, you can select files from local and use them easily by creating custom keyword mentioned above.

I hope it has been a useful post.

12 Likes

Hi @emine

Great post. Just a small tip: You should style your code with Preformatted Text because it will be much more easy to read. Another one is to take advantage of the styling our editor provides to emphasize the test steps and the structure of your post.

Regards !

1 Like

Hi @ThanhTo
Thank you for small tip .
Regards …

1 Like

Hi Emine, in step #2 above, how do you find the “TestObject” used in the customkeyword’s uploadFile command? I can’t figure-out how to find a windows explorer object using object spy. Please help so I can give move forward with trying to implement this example. Thank you very very much.

1 Like

Hello @tracy_ellis
To download the file on the web page first click on the relevant field. If you do not detect it, right click on the mouse with the mouse to detect this area and add it to the object repostory part.

example field picture


After this step, the test case steps are opened as shown in the following image and custom keywords are selected. You should follow the steps in the image below.

The next step assigns the keyword e.
The first parameter is the path of the file upload area, the part I mentioned above, which can be detected by the right click if it is not detected by the recorder. Please can you look above example field picture

the second parameter refers to the path of the file in the local.
For example: C: \ TEST \ test.txt

regards …

2 Likes

Thank you, this works great when I’m running a single test, but when I’m running a suite(or collection) of tests the native fileUpload window get lost behind all of the other open windows on my desktop and I find Robot trying to write text in places I don’t want it written. Is there anyway to ensure the FileUpload Windows desktop window is in the front before Robot tries to write to it? I know how to ensure a browser window is on top, but I cannot figure out how to ensure a native Windows window(fileUpload) is on top(or in front).

1 Like

This is a known issue with Robot. You need to cover all the circumstances it is likely to encounter; but if you’re executing suites, that might prove extremely difficult (if not impossible).

My advice: To keep yourself sane, restrict these kinds of tests to single-purpose testing desktops

1 Like

Hi, I also have the issue with robot. After opening new window, it paste the path to the file in the right place, but it doesn’t click enter. I have tried to set the delay but it also didn’t help. Any idea how to fix that?

Regards,
Maria

1 Like

You can use the command WebUI.delay ()

1 Like

This may help too: How to use the 'Robot Framework' to load files using a custom keyword

1 Like

In general two code doing the same thing.

1 Like

True, but the delays allowed my code to work for me:
The delays can be adjusted by the coder where needed.
robot.delay(1000); //Millisecond 1 second delay only if needed
Thread.sleep(2500) //Millisecond 2.5 second delay only if needed

2 Likes

Yes, but it doesn’t have to do it in the keyword, it can provide the waiting time for WebUI.delay () in the Test case.
Thus, the keyword will not need a waiting time.

1 Like

That might have been true for you but I tested with and without…
In my case it was needed.

1 Like

You’re right, our way of doing things is different.
Thank you for your contribution.

1 Like

Hi @emine.,

Currently Katlon provides "uploadFile " keyword as default. IS there any difference with that or same?

1 Like

I actually need to see the same function but I couldn’t get a solution with the uploadfile keyword.
Therefore, I was able to create a custom keyword by using the robot class and get a definite solution.

1 Like

Hi @emine…

I tried as you said…i added custom keyword as you mentioned above.(snapshot attached here). But when i type ‘dot’ mark after type ‘CustomKeywords’ word ,it is not suggesting anything. can you please help me to find the case?

1 Like

Hello Again @rushantha

For Custom Keyword, please add the libraries below to the top of the code.

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

1 Like

Hi… now added above mention libraries and i think now keyword is ok.

But when i type ‘CustomKeywords’ word in test script it is showing error as below and it is not suggesting anything

1 Like