【Katalon Studio】关键字 - Keyword Util自定义验证关键字

文章目录

  • 介绍
  • 示例

介绍

在自定义关键字中,如果想要实现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;
		}
	}
}

你可能感兴趣的:(Katalon,Studio,Web自动化)