In general, we need to automate scenarios like uploading a file into the application for attaching profile pictures or documents. Subsequently, this tutorial will focus How to Handle File Uploads using Katalon Studio demonstrates handling the file upload feature and verifying downloaded files.
How to Handle File Uploads using Katalon Studio
What is File Upload in testing?
The file upload widget is the input tag having attribute type equal to file. Moreover, it allows us to upload all file formats (.jpg, .png, .txt…) Subsequently, let’s work on the case in which we need to upload a file and validate whether the file is uploaded.
Since Katalon Studio 6.1.5, please install Upload File Keywords (https://store.katalon.com/product/69/UploadFile-Keywords) plugin to use this feature.
Steps:
- Firstly, launch the URL of the application.
- Secondly, maximize the window.
- Finally, use the file upload widget to upload a file.
Manual Mode:
We can also use the script mode. Consequently, the below script is the code to upload a file and validate the uploaded file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
'Open browser and navigate to given URL' WebUI.openBrowser('C:\\\\Users\\\\User\\\\Desktop\\\\Katalon Articles\\\\File Upload\\\\UploadFile.html') 'Maximize the window\r\n' WebUI.maximizeWindow() 'Passing the path of the file' WebUI.uploadFile(findTestObject('Upload File'), 'C:\\\\Users\\\\Public\\\\Pictures\\\\Sample Pictures\\\\Desert.jpg') 'Capturing the file name after upload and storing it in a variable' FilePath = WebUI.getAttribute(findTestObject('Upload File'), 'value') 'Verifying the Actual path and Expected path of file' WebUI.verifyMatch(FilePath, 'C:\\fakepath\\Desert.jpg', false) |
File upload using Send Keys
We can also upload files by using the Send Keys method. Send Keys works for the input tag, having type equal to file.
Steps:
- Firstly, launch the URL of the application.
- Secondly, maximize the window.
- Thirdly, use the Send Keys method to upload a file.
- Finally, send Keys accepts file URL as a string.
Manual Mode:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
'Open browser and navigate to given URL' WebUI.openBrowser('C:\\\\Users\\\\User\\\\Desktop\\\\Katalon Articles\\\\File Upload\\\\UploadFile.html') 'Maximize the window\r\n' WebUI.maximizeWindow() 'Uploading the File using Send Keys method by passing the File path' WebUI.sendKeys(findTestObject('Upload File'), 'C:\\\\Users\\\\Public\\\\Pictures\\\\Sample Pictures\\\\Desert.jpg') 'Capturing the file name after upload and storing it in a variable' FilePath = WebUI.getAttribute(findTestObject('Upload File'),'value') 'Verifying the Actual path and Expected path of file' WebUI.verifyMatch(FilePath, 'C:\\fakepath\\Desert.jpg',false) |
Verify a Downloaded File
After downloading a file from the application, we need to verify whether the file download was successful and saved in a folder.
For that, we need to set preferences for Firefox, as shown in the image below.
Script Mode:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import org.openqa.selenium.By as By import org.openqa.selenium.WebDriver as WebDriver import org.testng.Assert as Assert import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI import internal.GlobalVariable as GlobalVariable 'Define Custom Path where file needs to be downloaded' String downloadPath = 'D:\\FileDownloadChecking' 'Launch a browser and Navigate to URL' WebUI.openBrowser(GlobalVariable.FileDownloadCheckingURL) WebDriver driver = DriverFactory.getWebDriver() 'Clicking on a Link text to download a file' driver.findElement(By.linkText('smilechart.xls')).click() 'Wait for Some time so that file gets downloaded and Stored in user defined path' WebUI.delay(10) 'Verifying the file is download in the User defined Path' Assert.assertTrue(isFileDownloaded(downloadPath, 'smilechart.xls'), 'Failed to download Expected document') boolean isFileDownloaded(String downloadPath, String fileName) { long timeout = 5 * 60 * 1000 long start = new Date().getTime() boolean downloaded = false File file = new File(downloadPath, fileName) while (!downloaded) { KeywordUtil.logInfo("Checking file exists ${file.absolutePath}") downloaded = file.exists() if (downloaded) { file.delete() // remove this line if you want to keep the file } else { long now = new Date().getTime() if (now - start > timeout) { break } Thread.sleep(3000) } } return downloaded } |
We have just learned how to handle file uploads using Katalon. Moreover, we also learned to verify the downloaded files using Katalon Studio. Additionally, you can download the source code here.
Conclusively, for further instructions and help, please refer to the Upload File (https://docs.katalon.com/display/KD/%5BWebUI%5D+Upload+File) guideline.