Skip to main content

Download and verify files

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.

Note:
The keyword is also accessible through the Katalon TestCloud Keywords plugin on Katalon Store. You can install the plugin to automatically load all TestCloud keywords into your Katalon project, without having to manually define them. For more details, visit:
package com.katalon.testcloud
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 static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.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
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import com.kms.katalon.core.webui.driver.DriverFactory
import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.remote.RemoteWebDriver
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import internal.GlobalVariable
import java.io.File;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class FileExecutor {
/**
* Download file content using base64 encoding.
*
* @param the given file name
* @return the file content in base64
*/
@Keyword
def getFileContent(String fileName) {
try {
Path userHome = Paths.get(System.getProperty("user.home"));
Path downloadsDirectory = userHome.resolve("Downloads");
Path filePath = downloadsDirectory.resolve(fileName);
File file = filePath.toFile();
byte[] fileBytes = Files.readAllBytes(file.toPath());
return Base64.getEncoder().encodeToString(fileBytes);
} catch (Exception e) {
throw new Exception('Failed to execute TestCloud Keyword: FileExecutor.getFileContent - Error Code: TCKW301');
}
}
/**
* Retrieve file metadata.
*
* @param the given file name
* @return file metadata
*/
@Keyword
def getFileDescriptor(String fileName) {
try {
Path userHome = Paths.get(System.getProperty("user.home"));
Path downloadsDirectory = userHome.resolve("Downloads");
Path filePath = downloadsDirectory.resolve(fileName);
File file = filePath.toFile();
long fileSize = file.length();
Map< String, Object > fileDescriptor = new HashMap<>();
long lastModifiedTimestamp = file.lastModified();
FileTime fileTime = FileTime.fromMillis(lastModifiedTimestamp);
Instant instant = fileTime.toInstant();
ZonedDateTime utcDateTime = instant.atZone(ZoneId.of("UTC"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
String utcDateString = utcDateTime.format(formatter);
fileDescriptor.put("modified_time", utcDateString);
fileDescriptor.put("name", file.getName());
fileDescriptor.put("size", fileSize);
return fileDescriptor;
} catch (Exception e) {
throw new Exception('Failed to execute TestCloud Keyword: FileExecutor.getFileDescriptor - Error Code: TCKW302');
}
}
/**
* Check if file name exists.
*
* @param the given file name
* @return true if the file exists
*/
@Keyword
def exist(String fileName) {
try {
Path userHome = Paths.get(System.getProperty("user.home"));
Path downloadsDirectory = userHome.resolve("Downloads");
Path filePath = downloadsDirectory.resolve(fileName);
File file = filePath.toFile();
return file.exists();
} catch (Exception e) {
throw new Exception('Failed to execute TestCloud Keyword: FileExecutor.exist - Error Code: TCKW303');
}
}
}