【Katalon Studio】失败用例重跑

1开启retry

1.1 命令模式启动-python

python中运行命令,retry次数为2
重点看ui_cmd,Katalon的命令行模式,最终都是为了运行Katalon的该cmd,-retry=2

import subprocess
import os


os.chdir(katalon_path)
# TODO : subprocess支持cmd参数用来指定当前运行的目录,可以不需要用os.chdir来切换。
ui_cmd ='katalon -noSplash -runMode=console -projectPath="{proj_path}" -testSuitePath="{test_suite}" -executionProfile="{test_profile}" -browserType="Chrome" -retry=2 -retryFailedTestCases=true -reportFolder="{report_path}" --reportFileName="uiauto" -apikey="851eeef5-xxxx-xxxx-xxxx-xxxxxxxxxxx"'.format(
            proj_path=proj_path,
            test_suite = test_suite,
            test_profile = test_profile,
            report_path = test_report_path
        )
print(ui_cmd, flush=True)

        #开始运行测试
        subprocess.call(ui_cmd, shell=True)

1.2 界面开启

同样也是设置2次重跑,界面模式中有failed Test Cases only,仅重跑失败用例;
在这里插入图片描述

2 创建监听

CICD的时候不可能用界面模式,只能用cmd模式,那就不能做到Passed用例不重跑,如何实现呢?
用PassedCaseinSuite.txt记录通过用例,测试用例运行时,查看该txt文件,如果该用例是Passed,则跳过运行;

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 java.lang.reflect.Array

import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as 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.mobile.keyword.MobileBuiltInKeywords as Mobile

import internal.GlobalVariable as GlobalVariable

import com.kms.katalon.core.annotation.BeforeTestCase
import com.kms.katalon.core.annotation.BeforeTestSuite
import com.kms.katalon.core.annotation.AfterTestCase
import com.kms.katalon.core.annotation.AfterTestSuite
import com.kms.katalon.core.context.TestCaseContext
import com.kms.katalon.core.context.TestSuiteContext
import com.kms.katalon.core.configuration.RunConfiguration

class TestListener {
	static String passedCaseinSuitePath = RunConfiguration.getProjectDir() + '/';
	static String passedCaseinSuiteFile = 'PassedCaseinSuite.txt';
	static String passedCaseTXTFilePath = passedCaseinSuitePath + passedCaseinSuiteFile
	
	public static boolean deleteTXTFile() {
		boolean flag = false
		String sPath = passedCaseinSuitePath + passedCaseinSuiteFile
		System.out.println('PassedCaseinSuiteFilePath:  ' + passedCaseTXTFilePath)
		
		File file = new File(passedCaseinSuitePath, passedCaseinSuiteFile)
		if (file.isFile() && file.exists()) {
			file.delete()
			System.out.println('Successd to delete the file:' + passedCaseTXTFilePath)
			flag = true
		}

		return flag
	}
	
	public static String readTXT() {
		File file = new File(passedCaseTXTFilePath);
		if(file.isFile() && file.exists()){
			try {
				FileInputStream fileInputStream = new FileInputStream(file);
				InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
				BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
				 
				StringBuffer sb = new StringBuffer();
				String text = null;
				while((text = bufferedReader.readLine()) != null){
					sb.append(text);
				}
				return sb.toString();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	

	/**使用FileOutputStream来写入txt文件
	 * @param txtPath txt文件路径
	 * @param content 需要写入的文本
	 */
	public static void writeTXT(String txtPath, String content){
	   FileOutputStream fileOutputStream = null;
	   File file = new File(txtPath);
	   try {
		   if(!file.exists()){
			   //判断文件是否存在,如果不存在就新建一个txt
			   file.createNewFile();
		   }
		   fileOutputStream = new FileOutputStream(file, true);
		   fileOutputStream.write(content.getBytes());
		   fileOutputStream.flush();
		   fileOutputStream.close();
	   } catch (Exception e) {
		   e.printStackTrace();
	   }
	}
	
	public static void writetoTXT(String content){
		writeTXT(passedCaseTXTFilePath, content)
	 }
	
	public String getMapToString(Map<String,String> map){
		
		Set<String> keySet = map.keySet();
		//将set集合转换为数组
		String[] keyArray = keySet.toArray(new String[keySet.size()]);
		//给数组排序(升序)
		Arrays.sort(keyArray);
		//因为String拼接效率会很低的,所以转用StringBuilder。博主会在这篇博文发后不久,会更新一篇String与StringBuilder开发时的抉择的博文。
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < keyArray.length; i++) {
			// 参数值为空,则不参与签名 这个方法trim()是去空格
			if (map.get(keyArray[i]).toString().trim().length() > 0) {
				sb.append(keyArray[i]).append("=").append(map.get(keyArray[i]).toString().trim());
			}
			if(i != keyArray.length-1){
				sb.append("&");
			}
		}
		return sb.toString();
	}
	
	def getPassedCaseListFromFile() {
		String fileContent = readTXT();
		if (fileContent == null) {
			return [];
		}
		else {
			String[] passedCase = fileContent.split('@@@@@');
			return Arrays.asList(passedCase);
		}
	}
	
	@BeforeTestSuite
	def PassedCaseTXTbeforeTestSuite(){
		// deleteTXTFile();
		
		File file = new File(passedCaseinSuitePath, passedCaseinSuiteFile);
		
		//判断文件是否存在,如果不存在就新建一个txt文件
		if(!file.exists()){			
			file.createNewFile();
		}
	}
	/**	
	@AfterTestSuite
	def PassedCaseTXTafterTestSuite(){
		deleteTXTFile();
	}
	
	/**
	 * Executes before every test case starts.
	 * @param testCaseContext related information of the executed test case.
	 */
	@BeforeTestCase
	def beforeTestcase(TestCaseContext testCaseContext) {
		
		String testCaseId = testCaseContext.getTestCaseId();
		Map<String, Object> vars = testCaseContext.getTestCaseVariables();
		String svars = getMapToString(vars);
		String caseWithVars = testCaseId + "&&&&" + svars;
		List<String> testCasePassed = getPassedCaseListFromFile();
		if (testCasePassed.contains(caseWithVars)) {
			testCaseContext.skipThisTestCase();
		}
	}
	
	
	/**
	 * Executes after every test case ends.
	 * @param testCaseContext related information of the executed test case.
	 */
	@AfterTestCase
	def afterTestcase(TestCaseContext testCaseContext) {		
		File file = new File(passedCaseinSuitePath, passedCaseinSuiteFile)
		if (file.isFile() && file.exists()) {
			if (testCaseContext.getTestCaseStatus() == "PASSED") {
				String testCaseId = testCaseContext.getTestCaseId();
				Map<String, Object> vars = testCaseContext.getTestCaseVariables();
				String svars = getMapToString(vars);
				String caseWithVars = "@@@@@" + testCaseId + "&&&&" + svars;
				writetoTXT(caseWithVars);
			}
		}

	}
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值