7

I'm trying to upload file using katalon studio for automation testing (web Testing. After clicking on 'Browse' button the windows popup is opened but i can't choose photo or go to specific path. I found a command WebUI.UploadFile() but I think I'm not using it correctly.

If anyone had something like this, please share your experience. How could i do this in katalon?

Tapas Bose
  • 25,780
  • 71
  • 202
  • 317
M.Mortada
  • 81
  • 1
  • 4

2 Answers2

9

You can give this solution a try:

  1. Create the following custom keyword (https://docs.katalon.com/display/KD/Define+custom+keywords):
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 WebUICustomKeyword { 
    @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. Replace 'Upload File' step with that custom keyword in your test case instead, e.g:
CustomKeywords.'com.katalon.WebUICustomKeyword.uploadFile'(findTestObject('BrowseButton'), 'yourFileHere')
0
  1. First, create a keyword to the file upload button or link, something like: “Object Repository/Page_Add_Document_To_Current_Account/lnk_choose_a_file”

  2. Next, create a method call for which will handle the upload, like class uploadNewFile {

    /** This method will be used to upload file */ @Keyword def uploadFileToTest(TestObject to, String filePath){ WebUI.click(to) WebUI.delay(3) //i’d recommend adding this delay to give the code time to run StringSelection ss = new StringSelection(filePath) Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null) WebUI.delay(2) //same reason as above

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

  3. Next, call the method where it needs to be in your code and it will appear something like: *CustomKeywords.‘methodCalls.uploadNewFile.uploadFileToTest’(findTestObject(**null), ‘’)***

  4. Next, replace null in 3 above with the keyword you created in 1 above, then insert the filepath of the file to be uploaded into the ‘’ in 3 above.

The final statement would be something like: CustomKeywords.‘methodCalls.uploadNewFile.uploadFileToTest’(findTestObject(‘Object Repository/Page_Add_Document_To_Current_Account/lnk_choose_a_file’), ‘C:\Users\…\Documents\…\…\TestFile.txt’)

All the best