无法使用groovy解析xml

sha*_*ash 0 groovy xmlslurper jenkins

我无法读取xml并解析项目,因为它总是返回错误

groovy.lang.MissingPropertyException:没有这样的属性:类的方法:Script1

def xmlText = new XmlSlurper().parse('myxml.xml')
def skipped = 0
def failed = 0
def total =  0
def passed = 0
xmlText.suite.test.class.test-method.each{ 
   if(it['@is-config'] == "true"){
   }
   else{
      if(it['@status']=="PASS"){
         passed  = passed + 1
         total = total + 1
      }
      if(it['@status']=="FAIL"){
         failed = failed + 1
         total = total + 1
     }
     if(it['@status']=="SKIP"){
         skipped = skipped + 1
         total = total + 1
     }
  }
}
Run Code Online (Sandbox Code Playgroud)

xml文件是

<?xml version="1.0" encoding="UTF-8"?>
<testng-results skipped="3" failed="0" total="3" passed="0">
  <reporter-output>
  </reporter-output>
  <suite name="DinamicTestSuite" duration-ms="24" started-at="2018-07-16T13:26:48Z" finished-at="2018-07-16T13:26:48Z">
    <groups>
    </groups>
    <test name="Device:null" duration-ms="24" started-at="2018-07-16T13:26:48Z" finished-at="2018-07-16T13:26:48Z">
      <class name="com.automation.venues.stg.VenuesSuite">
        <test-method status="PASS" signature="login(org.testng.ITestContext, java.lang.String)[pri:0, instance:com.automation.venues.stg.VenuesSuite@71454b9d]" name="login" is-config="true" duration-ms="11" started-at="2018-07-16T16:26:48Z" finished-at="2018-07-16T16:26:48Z">
</test-method>
             <test-method status="PASS" signature="beforeMethod(java.lang.reflect.Method)[pri:0, instance:com.automation.venues.stg.VenuesSuite@71454b9d]" duration-ms="0" started-at="2018-07-16T16:26:48Z" finished-at="2018-07-16T16:26:48Z">
</test-method>
      </class> <!-- com.automation.venues.stg.VenuesSuite -->
    </test> <!-- Device:null -->
  </suite> <!-- DinamicTestSuite -->
</testng-results>
Run Code Online (Sandbox Code Playgroud)

我该如何解析清单?

tim*_*tes 6

你需要把报价一轮属性与-

xmlText.suite.test.class.'test-method'.each{ 
Run Code Online (Sandbox Code Playgroud)

您的each方法的替代方案是:

def xmlText = new XmlSlurper().parse('myxml.xml')

def totals = xmlText.suite.test.class.'test-method'.countBy { it.@status.text() }

def skipped = totals.SKIP
def failed = totals.FAIL
def passed = totals.PASS
def total = totals.values().sum()
Run Code Online (Sandbox Code Playgroud)