无法使用groovy解析xml

时间:2018-07-17 07:58:30

标签: jenkins groovy xmlslurper

我无法读取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
     }
  }
}

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>

我如何解析列表?

1 个答案:

答案 0 :(得分:5)

您需要在属性中用引号将-括起来

xmlText.suite.test.class.'test-method'.each{ 

您的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()