How to upload a file / photo using katalon studio?
I am trying to upload a file using katalon studio for automation testing (web testing. After clicking Browse, a popup opens, but I cannot select a photo or navigate to a specific path. I found the WebUI.UploadFile () command , but I think that I am not using it correctly.
If anyone has had something similar, please share your experience. How can i do this in katalon?
source to share
You can try this solution:
- 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);
}
}
- Replace the Upload File step with this custom keyword in your test case, for example:
CustomKeywords.'com.katalon.WebUICustomKeyword.uploadFile'(findTestObject('BrowseButton'), 'yourFileHere')
source to share
I have implemented the above and I am still getting the error:
java.lang.ClassNotFoundException: com.katalon.WebUICustomKeyword
Here's my scenario:
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.checkpoint.Checkpoint as 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 as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
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);
}
}
WebUI.openBrowser('')
WebUI.navigateToUrl(GlobalVariable.BASE_URL + '/client')
WebUI.click(findTestObject('Page_VendorProof Vendor Login/div_LoginUsernamePasswordForgot Password'))
WebUI.setText(findTestObject('Page_VendorProof Vendor Login/input_Username_j_username'), GlobalVariable.USERNAME)
WebUI.setText(findTestObject('Page_VendorProof Vendor Login/input_Password_j_password'), GlobalVariable.PASSWORD)
WebUI.click(findTestObject('Page_VendorProof Vendor Login/input_Password_Submit'))
//confirm you are logged in
WebUI.verifyElementPresent(findTestObject('Object Repository/Page_Compliance Dashboard/a_Logout'), 1)
WebUI.click(findTestObject('Object Repository/Page_Compliance Dashboard/span_Import'))
WebUI.click(findTestObject('Object Repository/Page_Vendor Import Management/button_Upload File'))
CustomKeywords.'com.katalon.WebUICustomKeyword.uploadFile'(findTestObject('Object Repository/Page_Vendor Import Management/input_Choose a file'), '/Users/mthees/Documents/katalon-test-import-vendor.csv')
source to share
-
First create a keyword for the file download button or link, something like: "Object Repository / Page_Add_Document_To_Current_Account / lnk_choose_a_file"
-
Then create a method call for which the upload will be handled, for example the uploadNewFile {class
/ ** This method will be used to upload the file * / @ Keyword def uploadFileToTest (TestObject to, String filePath) {WebUI.click (to) WebUI.delay (3) // id recommends adding this delay to give code time to run StringSelection ss = new StringSelection (filePath) Toolkit.getDefaultToolkit (). getSystemClipboard (). setContents (ss, null) WebUI.delay (2) // same reason as above
Robot robot = 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);
-
Then call the method where it should be in your code and it will look something like this: * CustomKeywords.'methodCalls.uploadNewFile.uploadFileToTest (findTestObject (** null ), ') ***
-
Then replace the null in 3 above with the keyword you created in 1 above, then paste the file path to upload in 'in 3 above.
The final statement will look something like this: CustomKeywords.'methodCalls.uploadNewFile.uploadFileToTest (findTestObject ('Object Repository / Page_Add_Document_To_Current_Account / lnk_choose_a_file),' C: \ Users \ ... \ Documents \ ... \ tt)
All the best
source to share