bash的用法,zephyrscale获取case,解析数据,执行case

发布于:2025-03-21 ⋅ 阅读:(27) ⋅ 点赞:(0)
#!/bin/bash
zstoken="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb250ZXh0Ijp7ImJhc2VVcmwiOiJodHRwczovL3dlbGFiYmFuay5hdGxhc3NpYW4ubmV0IiwidXNlciI6eyJhY2NvdW50SWQiOiI2MzM1NGUzMWY1Njg2MTViZGM3ZWJlNjYiLCJ0b2tlbklkIjoiMjY5ZWU0M2EtNWYzNy00YzVjLWI4NGUtMTZmMTAyZTU5YWY4In19LCJpc3MiOiJjb20ua2Fub2FoLnRlc3QtbWFuYWdlciIsInN1YiI6ImZmMzViODM3LTUyMjktM2ZhYi04MDNlLTdhMjdhMTBhZWRlYyIsImV4cCI6MTc2NTYxODQ1NSwiaWF0IjoxNzM0MDgyNDU1fQ.r-9M_ugoSufvXUTxr5HUcs7Fn5SFemCi_KPKYlggtv4"
SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)
echo "${SHELL_FOLDER}"

env="stage"
platform="mobile"
mobile_temp="ios"
mobile=`echo ${mobile_temp} | tr '[A-Z]' '[a-z]'`

# get test cases from test execution
#echo "Enter test cycle key:"
#read manualTestCycleKey_temp # in uppercase, e.g. "VBL", "ACBN"
#manualTestCycleKey=`echo ${manualTestCycleKey_temp} | tr '[a-z]' '[A-Z]'`
manualTestCycleKey="ACBN-R76"
manualProjectKey=${manualTestCycleKey%%-*}
echo "manualTestCycleKey: ${manualTestCycleKey}"
echo "manualProjectKey: ${manualProjectKey}"

##################################################################
# get test case
manualTestCycleName=$(curl -k -H "Authorization: Bearer ${zstoken}" -X GET "https://api.zephyrscale.smartbear.com/v2/testcycles/${manualTestCycleKey}" -H "Content-Type: application/json" | jq -r -c '.name')
manualTestCycle=$(curl -k -H "Authorization: Bearer ${zstoken}" -X GET "https://api.zephyrscale.smartbear.com/v2/testexecutions?testCycle=${manualTestCycleKey}&startAt=0&maxResults=1000" -H "Content-Type: application/json")
manualTestCycleArr=(${manualTestCycle//,/ })
echo "check get testcase costs time if there are many test executions"

##################################################################
# read the case and save in list
manualTestCaseKeysFull=()
manualTestCaseKeysFullTemp=()
for ((i = 0 ; i < ${#manualTestCycleArr[@]} ; i++ ))
do
  if [[ ${manualTestCycleArr[$i]} = *"testcases"* ]]; then
    tempStr=${manualTestCycleArr[$i]#\"testCase\":{\"self\":\"https://api.zephyrscale.smartbear.com/v2/testcases/}}
    tempStr=${tempStr%%/*}
    echo "tempStr: ${tempStr}"
    manualTestCaseKeysFullTemp+=($tempStr)
  fi
done
manualTestCaseKeysFull+=($(echo "${manualTestCaseKeysFullTemp[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
echo "manualTestCaseKeysFull: ${manualTestCaseKeysFull[@]}"
echo "manualTestCaseKeysFullTemp: ${manualTestCaseKeysFullTemp[@]}"


#####################
## update runner file
xmlLocations="${SHELL_FOLDER}/src/main/resources/suite/channel/mobile_ios.xml"
runnerFolder="${SHELL_FOLDER}/src/main/java/com/welab/automation/projects/channel/runners"
runnerFile="/MobileTestRunner_iOS.java"
fullRunnerName=${runnerFolder}${runnerFile} #字符串拼接不需要加号
echo "fullRunnerName: ${fullRunnerName}"

tags_string=$(IFS=,; echo "${manualTestCaseKeysFull[*]}")  # 把list的元素用逗号隔开生成字符串
echo "tags_string: ${tags_string}"
i_tag=@$(sed 's/,/ or @/g' <<< ${tags_string}) # 用"or " 替换逗号
runner=
i_runner=$(sed "s/tags = \".*\",/tags = \"$i_tag\",/" ${fullRunnerName})
echo "$i_runner" > ${fullRunnerName}
#####################

#########################################  上传所有的case 的测试结果到这test cycle中

upperManualProjectKey=`echo ${manualProjectKey} | tr '[a-z]' '[A-Z]'`
if [[ $upperManualProjectKey == "ACBN" ]]; then
manualFolderId="9250320"
elif [[ $upperManualProjectKey == "VBL" ]]; then
manualFolderId="8410995"
elif [[ $upperManualProjectKey == "GOS" ]]; then
manualFolderId="10206779"
elif [[ $upperManualProjectKey == "VBWMIS" ]]; then
manualFolderId="9250352"
fi

currentDateTime=`date +"%Y-%m-%d %H:%M:%S"`
createManualTestCycleKey=$(curl -k -H "Authorization: Bearer ${zstoken}" -X POST "https://api.zephyrscale.smartbear.com/v2/testcycles" -H "Content-Type: application/json" -d "{\"projectKey\":\"${manualProjectKey}\",\"name\":\"${manualTestCycleKey}-${manualTestCycleName}-${currentDateTime}\",\"folderId\":\"${manualFolderId}\"}")
newManualTestCycleKey=$(jq -r -c '.key' <<< "$createManualTestCycleKey")
echo "createManualTestCycleKey: ${createManualTestCycleKey}"
echo "newManualTestCycleKey: ${newManualTestCycleKey}"
###########################################

echo mvn clean test -Denv=${env} -Dplatform=${platform} -Dmobile=${mobile} -DtestngXmlFile="${xmlLocations}"
echo "**************************************************************************************************"
mvn clean
mvn test -Denv=${env} -Dplatform=${platform} -Dmobile=${mobile} -DtestngXmlFile="${xmlLocations}"


##################################################################
for ((i = 0 ; i < ${#manualTestCaseKeysFull[@]} ; i++ ))
do

  testResultCsvFile=${SHELL_FOLDER}/TestReport/zephyrscale/suites.csv
    waitTime=10
    while : ; do
      [[ -f ${testResultCsvFile} || $waitTime == 0 ]] && break
      echo "Pausing until suites.csv exists."
      let waitTime--
      echo $waitTime
      sleep 1
    done
  appVersionTxtFile=${SHELL_FOLDER}/TestReport/zephyrscale/appversion.txt
  while : ; do
    [[ -f ${appVersionTxtFile} || $waitTime == 0 ]] && break
    echo "Pausing until appversion.txt exists."
    let waitTime--
    echo $waitTime
    sleep 1
  done
  appVersionTxt=`cat ${appVersionTxtFile} | tr '\r\n' "#"`
  appVersionTxt=$(sed 's/#/\<br\>/g' <<< $appVersionTxt)

  testResultCsv=`cat ${testResultCsvFile}`
  echo $testResultCsv

  i_comment=${appVersionTxt}

  # 根据txt文件判断是否pass
  i_testExecutionStatus=${appVersionTxt}
  if [[ $i_comment = *"${manualTestCaseKeysFull[$i]}=false"* ]]; then
    i_testExecutionStatus="Fail"
  elif [[ $i_comment = *"${manualTestCaseKeysFull[$i]}=true"* ]]; then
    i_testExecutionStatus="Pass"
  else
    i_testExecutionStatus="In Progress"
  fi

  i_executionTime="800"

  echo "manualProjectKey: ${manualProjectKey}"
  echo "manualTestCaseKeys[$i]: ${manualTestCaseKeysFull[$i]}"  # 所有feature 文件中的tag都存方法在这个list里面
  echo "newManualTestCycleKey: ${newManualTestCycleKey}"  #新建的test cycle
  echo "i_testExecutionStatus: ${i_testExecutionStatus}"
  echo "i_comment: ${i_comment}"
  echo "i_executionTime: ${i_executionTime}"
  # manualProjectKey 是ACBN
  # manualTestCaseKeys 是ACBN
  echo curl -k -H "Authorization: Bearer ${zstoken}" -X POST "https://api.zephyrscale.smartbear.com/v2/testexecutions" -H "Content-Type: application/json" -d "{\"projectKey\":\"${manualProjectKey}\",\"testCaseKey\":\"${manualTestCaseKeysFull[$i]}\",\"testCycleKey\":\"${newManualTestCycleKey}\",\"statusName\":\"${i_testExecutionStatus}\",\"comment\":\"${i_comment}\",\"executionTime\":\"${i_executionTime}\"}"
  manualTestExecution=$(curl -k -H "Authorization: Bearer ${zstoken}" -X POST "https://api.zephyrscale.smartbear.com/v2/testexecutions" -H "Content-Type: application/json" -d "{\"projectKey\":\"${manualProjectKey}\",\"testCaseKey\":\"${manualTestCaseKeysFull[$i]}\",\"testCycleKey\":\"${newManualTestCycleKey}\",\"statusName\":\"${i_testExecutionStatus}\",\"comment\":\"${i_comment}\",\"executionTime\":\"${i_executionTime}\"}") # Not Executed / Pass / Fail / WIP / Blocked

#  rm -f temppayload.json # delete json
#  rm -f payload.json # delete json
#  rm -f ${SHELL_FOLDER}/TestReport/zephyrscale/suites.csv # delete test result
#  rm -f ${SHELL_FOLDER}/TestReport/zephyrscale/API.txt # delete API.txt
done

网站公告

今日签到

点亮在社区的每一天
去签到