如何在groovy Jenkins管道中读取xml标记的属性

rsl1atfo  于 2023-08-02  发布在  Jenkins
关注(0)|答案(1)|浏览(141)

我有以下XML文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<testsuites>
  <testsuite name="xray" tests="2" failure="0" timestamp="2023-07-07T05:25:33.339Z" skipped="0" failure-evaluating="0">
    <testcase name="SPT-4323" classname="testim.io.test" time="52.597" ownedBy="Staysure Automation" ownerEmail="automationteam@staysure.co.uk">
      <system-out>https://app.testim.io/#/project/9GScC/branch/master/test/ZKkR</system-out>
    </testcase>
    <testcase name="SPT-4331" classname="testim.io.test" time="20.823" ownedBy="Automation Team" ownerEmail="nipunam@intervest.lk">
      <system-out>https://app.testim.io/#/project/9GScCU/branch/master/test/7O9a</system-out>
    </testcase>
  </testsuite>
</testsuites>

字符串
有没有一种方法可以将每个testcase标签中的name属性的值获取到Jenkins管道groovy脚本中的一个数组中(此XML可以有不同数量的“testcase”标签)。
示例阵列:Test_IDS:[“SPT-4323”,“SPT-4331”]
我已经尝试了如下,它没有打印任何值

def File = readFile "${env.WORKSPACE}/testim-tests-report.xml"
echo "${File}"
def xml = new XmlSlurper().parseText(File)
def xrayID = xml.testsuites.testsuite.testcase[1].@name

echo "Xray ID is ${xrayID}"

watbbzwu

watbbzwu1#

下面是一个快速解决方案:

def xml = new XmlSlurper().parseText("""
<testsuites>
  <testsuite name="xray" tests="2" failure="0" timestamp="2023-07-07T05:25:33.339Z" skipped="0" failure-evaluating="0">
    <testcase name="SPT-4323" classname="testim.io.test" time="52.597" ownedBy="Staysure Automation" ownerEmail="automationteam@staysure.co.uk">
      <system-out>https://app.testim.io/#/project/9GScC/branch/master/test/ZKkR</system-out>
    </testcase>
    <testcase name="SPT-4331" classname="testim.io.test" time="20.823" ownedBy="Automation Team" ownerEmail="nipunam@intervest.lk">
      <system-out>https://app.testim.io/#/project/9GScCU/branch/master/test/7O9a</system-out>
    </testcase>
  </testsuite>
</testsuites>
""")

def xrayIDs = xml.testsuite.testcase.collect { it.@name }

println "Xray IDs are ${xrayIDs}"

字符串
下面是输出中的数组:

Xray IDs are [SPT-4323, SPT-4331]


你几乎做了正确的一切,你只是错过了xml解析器将根标记视为路径的根元素,所以你不应该在路径中提及它:您的xml.testsuites.testsuite应该看起来像xml.testsuite
希望能帮上忙。

相关问题