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);
}
}
}
}