Download and verify files
- You have installed the Katalon TestCloud Keywords plugin from Katalon Store. If you have not, visit: Katalon TestCloud Keywords.
FileExecutor
class that contains custom keywords for downloading and verify files. The keyword file should be placed under the com.katalon.testcloud
package.Read and verify file content
To read the file returned by the keyword FileExecutor.getFileContent
, 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)