Download and verify files
Requirements
- This keyword is applicable for desktop browser testing on Windows and macOS.
- You have installed the Katalon TestCloud Keywords plugin from Katalon Store. If you have not, visit this site: Katalon TestCloud Keywords.
- Follow this guide to install the plugins: Install plugins online from Katalon Store.
When executing WebUI tests with TestCloud, in some scenarios, you may want to download files and verify them. The following is a ready-to-use FileExecutor class that contains custom keywords for downloading and verify files. The keyword file should be placed under the com.katalon.testcloud package.
- In Katalon Studio, click the Profile drop-down and select Reload Plugins to make sure the plugin is installed.
- Add the
FileExecutor.pushFiletoDevicekeyword to your test case.
- Configure your TestCloud mobile environment and run the test.
Read and verify file content​
Since the FileExecutor.getFileContent keyword returns the file content in Base64-encoded format, you need to decode it before use. Add the following code snippets to your script:
Read and verify file as String​
String fileName = 'sample-1.csv'
String encodedContent = CustomKeywords.'com.katalon.testcloud.FileExecutor.getFileContent'(fileName)
byte[] decodedBytes = Base64.getDecoder().decode(encodedContent);
WebUI.verifyEqual(new String(decodedBytes).contains('Enter stock symbol in cell B4",,,,,POWERED BY'), true)
Read and verify CSV file as CSV structure​
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.testdata.CSVData
import com.kms.katalon.core.testdata.reader.CSVSeparator
'Get file Content from TestCloud'
String fileName = 'sample-1.csv'
String encodedContent = CustomKeywords.'com.katalon.testcloud.FileExecutor.getFileContent'(fileName)
byte[] decodedBytes = Base64.getDecoder().decode(encodedContent);
'Write the content to a new file'
String newFileLocation = new File(RunConfiguration.getProjectDir() + '/' + fileName).getCanonicalPath()
FileOutputStream outputStream = new FileOutputStream(newFileLocation);
outputStream.write(decodedBytes);
outputStream.close();
'Read the CSV file content (Excluding the headers)'
CSVData csvData = new CSVData(newFileLocation, true, CSVSeparator.COMMA)
List allData = csvData.getAllData()
'Get row 27th, column 2nd'
println allData.get(26).get(1)
Read and verify CSV file with a dynamic file name in CSV structure​
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.testdata.CSVData
import com.kms.katalon.core.testdata.reader.CSVSeparator
import com.kms.katalon.core.webui.driver.DriverFactory
import org.openqa.selenium.WebDriver
import org.openqa.selenium.JavascriptExecutor
'Get name of the latest file downloaded on TestCloud'
WebDriver driver = DriverFactory.getWebDriver()
driver.get('chrome://downloads/')
String fileName = ((JavascriptExecutor) driver).executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList').items.filter(e => e.state === 2).map(e => e.fileName)[0];")
WebUI.back()
'Get file Content from TestCloud'
String encodedContent = CustomKeywords.'com.katalon.testcloud.FileExecutor.getFileContent'(fileName)
byte[] decodedBytes = Base64.getDecoder().decode(encodedContent);
'Write the content to a new file'
String newFileLocation = new File(RunConfiguration.getProjectDir() + '/' + fileName).getCanonicalPath()
FileOutputStream outputStream = new FileOutputStream(newFileLocation);
outputStream.write(decodedBytes);
outputStream.close();
'Read the CSV file content (Excluding the headers)'
CSVData csvData = new CSVData(newFileLocation, true, CSVSeparator.COMMA)
List<List> allData = csvData.getAllData()
'Get row 27th, column 2nd'
println allData.get(26).get(1)
Read Excel files​
import com.kms.katalon.keyword.excel.ExcelKeywords
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.webui.driver.DriverFactory
import org.openqa.selenium.WebDriver
import org.openqa.selenium.JavascriptExecutor
import java.util.Base64
import java.io.FileOutputStream
WebUI.openBrowser('https://go.microsoft.com/fwlink/?LinkID=521962')
WebUI.delay(5)
WebDriver driver = DriverFactory.getWebDriver()
driver.get('chrome://downloads/')
// Get the latest downloaded file name
String fileName = ((JavascriptExecutor) driver).executeScript(
"return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList').items.filter(e => e.state === 2).map(e => e.fileName)[0];"
)
WebUI.back()
// Get file content from TestCloud
String encodedContent = CustomKeywords.'com.katalon.testcloud.FileExecutor.getFileContent'(fileName)
byte[] decodedBytes = Base64.getDecoder().decode(encodedContent)
// Write decoded file locally
String newFileLocation = new File(RunConfiguration.getProjectDir() + '/' + fileName).getCanonicalPath()
FileOutputStream outputStream = new FileOutputStream(newFileLocation)
outputStream.write(decodedBytes)
outputStream.close()
// Read Excel sheet content
def sheet01 = ExcelKeywords.getExcelSheet(newFileLocation)
Integer firstRowIndex = 1 // Row index starts at 0 for headers
Integer firstColIndex = 0
println 'First cell value: ' + ExcelKeywords.getCellByIndex(sheet01, firstRowIndex, firstColIndex)
Read PDF files​
With PDF files, you need to use the Apache PDFBox library to extract text from the decoded bytes. This approach allows you to read and verify PDF contents directly from your TestCloud session.
import org.openqa.selenium.WebDriver
import org.openqa.selenium.JavascriptExecutor
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.text.PDFTextStripper
import org.apache.pdfbox.Loader
import java.util.Base64
WebUI.openBrowser('https://smallpdf.com/blog/sample-pdf')
TestObject btnDownloadSamplePDF = findTestObject("Object Repository/Download Files/btnDownloadSamplePDF")
WebUI.scrollToElement(btnDownloadSamplePDF, 3)
WebUI.click(btnDownloadSamplePDF)
TestObject btnDownload = findTestObject("Object Repository/Download Files/btnDownload")
WebUI.click(btnDownload)
WebDriver driver = DriverFactory.getWebDriver()
driver.get('chrome://downloads/')
// Get downloaded file name
String fileName = ((JavascriptExecutor) driver).executeScript(
"return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList').items.filter(e => e.state === 2).map(e => e.fileName)[0];"
)
WebUI.back()
// Retrieve Base64-encoded content
String encodedContent = CustomKeywords.'com.katalon.testcloud.FileExecutor.getFileContent'(fileName)
byte[] decodedBytes = Base64.getDecoder().decode(encodedContent)
// Use PDFBox to read the PDF content
PDDocument document = Loader.loadPDF(decodedBytes)
PDFTextStripper pdfStripper = new PDFTextStripper()
String text = pdfStripper.getText(document)
println "Extracted PDF Content:\n" + text
document.close()