介紹
在自定義關鍵字中,如果想要實現Web UI中Verify *
關鍵字的效果,其中最重要的是,在關鍵字中可以判定運行結果;
可通過Keyword Util中的mark*
來實現;
方法 | 描述 |
---|---|
logInfo(String message) | 記錄消息 |
markError(String message) | 將關鍵字標記爲錯誤 |
markErrorAndStop(String message) | 將關鍵字標記爲錯誤並停止執行 |
markFailed(String message) | 將關鍵字標記爲失敗並繼續執行 |
markFailedAndStop(String message) | 將關鍵字標記爲失敗並停止執行 |
markPassed(String message) | 將關鍵字標記爲已通過 |
markWarning(String message) | 將關鍵字標記爲警告 |
【注意】在關鍵字中,markPassed運行後,會繼續運行,並不會標記通過後自動終止;
import com.kms.katalon.core.util.KeywordUtil
示例
看帶Verify的自定義關鍵字
自定義關鍵字作用,設置下載路徑爲D:\Downloads\後,可以驗證文件名是否正確,以及MD5是否正確
package com.uih
import java.io.IOException
import com.kms.katalon.core.annotation.Keyword
import java.io.FileInputStream
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.IOUtils
import org.testng.Assert as Assert
import internal.GlobalVariable
import java.util.concurrent.TimeUnit
import com.kms.katalon.core.util.KeywordUtil
import com.kms.katalon.core.configuration.RunConfiguration
public class FileModule {
static String DownloadPath = "D:\\Downloads\\"
static String ProjectPath = RunConfiguration.getProjectDir()
static def boolean deleteFile(String sPath) {
boolean flag = false
File file = new File(sPath)
if (file.isFile() && file.exists()) {
file.delete()
flag = true
}
return flag
}
@Keyword
static def boolean deleteDownloadFile(String sFile) {
boolean flag = false
String sPath = DownloadPath + sFile
System.out.println('FilePath: ' + sPath)
File file = new File(DownloadPath, sFile)
if (file.isFile() && file.exists()) {
file.delete()
System.out.println('Successd to delete the file:' + sPath)
flag = true
}
return flag
}
@Keyword
static def VerifyDownloadFileNameExist(String sFile, int timeout) {
String sPath = DownloadPath + sFile
System.out.println('FilePath: ' + sPath)
File file = new File(DownloadPath, sFile)
int count = 0
while(count < timeout){
TimeUnit.SECONDS.sleep(1)
if (file.isFile() && file.exists()) {
System.out.println('Verify the download document exist!')
KeywordUtil.markPassed('Verify the download document exist!')
break
}
System.out.println(String.valueOf(count+1) + ' times to verify the download document not exist!')
count++
}
if(count == timeout){
KeywordUtil.markFailedAndStop('Failed to find the file:' + sPath + ' , in ' + String.valueOf(timeout) + ' seconds.')
}
}
@Keyword
static def boolean deleteDirectory(String sPath) {
println(sPath);
if (!(sPath.endsWith(File.separator))) {
sPath=(sPath+File.separator)
}
File dirFile = new File(sPath)
if (!(dirFile.exists()) || !(dirFile.isDirectory())) {
return false
}
boolean flag = true
File[] files = dirFile.listFiles()
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath())
if (!(flag)) {
break
}
} else {
flag = deleteDirectory(files[i].getAbsolutePath())
if (!(flag)) {
break
}
}
}
if (!(flag)) {
return false
}
if (dirFile.delete()) {
return true
} else {
return false
}
}
@Keyword
static def boolean DeleteFolder(String sPath) {
boolean flag = false
File file = new File(sPath)
if (!(file.exists())) {
return true
} else {
if (file.isFile()) {
return deleteFile(sPath)
} else {
return deleteDirectory(sPath)
}
}
}
@Keyword
static def VerifyDownloadFileMD5(String downloadfile, String filePath) throws IOException {
File filedownload = new File(DownloadPath, downloadfile);
File filecompare = new File(ProjectPath, filePath);
if (filedownload.length() != filecompare.length()) {
System.out.println("+++++++++ unequal +++++++++++++");
} else {
System.out.println("+++++++++ equal +++++++++++++");
}
InputStream downloadfileStream = new FileInputStream(filedownload);
InputStream comparefileStream = new FileInputStream(filecompare);
// InputStream 轉 byte[]
byte[] FileByteArray = new byte[downloadfileStream.available()];
if (isSameMD5File(FileByteArray, new byte[comparefileStream.available()])){
KeywordUtil.markPassed('Same MD5')
}
else{
KeywordUtil.markFailedAndStop('Different MD5')
}
}
static def boolean isSameMD5File(byte[] fileByte1, byte[] fileByte2) {
String downloadFileMd5 = DigestUtils.md5Hex(fileByte1);
String compareFileMd5 = DigestUtils.md5Hex(fileByte2);
if (downloadFileMd5.equals(compareFileMd5)) {
System.out.println("---- equals ------ md5 " + downloadFileMd5);
return true;
} else {
System.out.println(downloadFileMd5 + " is downFileMd5 ++ unequal ++ compareFileMd5 = " + compareFileMd5);
return false;
}
}
}