使用XPath从XML读取多个值

时间:2018-07-12 17:36:35

标签: xml xpath logstash-configuration

我正在尝试从Testng-result.xml文件读取一个属性,该文件包含套件结果,并且我想读取结果状态并将其传递给kibana,以仅获取结果XPath查询

xpath =>
   [
   "/testng-results/suite/test/class/test-method/text()", "test-method",
   "/testng-results/suite/test/class/test-method[3]/@status/text()", "test-status",
   "/testng-results/suite/test/class/test-method[@name='test']", "test-result"
   ]        

这是XML文件:

<testng-results skipped="0" failed="0" total="10" passed="10">
  <suite name="TestSuite" duration-ms="498075" started-at="2018-06-13T07:00:21Z" finished-at="2018-06-13T07:08:39Z">
    <groups>
    </groups>
    <test name="UIHTML5TC10SettingSupportUI" duration-ms="20867" started-at="2018-06-13T07:00:21Z" finished-at="2018-06-13T07:00:42Z">
      <class name="com.mtfilemedia.ifive.testscripts.UIHTML5TC10SettingSupportUI">
        <test-method status="PASS" signature="testSetup()[pri:0, instance:com.mtfilemedia.ifive.testscripts.UIHTML5TC10SettingSupportUI@7e0e6aa2]" name="testSetup" is-config="true" duration-ms="14" started-at="2018-06-13T12:30:21Z" finished-at="2018-06-13T12:30:21Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- testSetup -->
        <test-method status="PASS" signature="beforeMethod()[pri:0, instance:com.mtfilemedia.ifive.testscripts.UIHTML5TC10SettingSupportUI@7e0e6aa2]" name="beforeMethod" is-config="true" duration-ms="24" started-at="2018-06-13T12:30:21Z" finished-at="2018-06-13T12:30:21Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- beforeMethod -->
        <test-method status="PASS" signature="test()[pri:0, instance:com.mtfilemedia.ifive.testscripts.UIHTML5TC10SettingSupportUI@7e0e6aa2]" name="test" duration-ms="19520" started-at="2018-06-13T12:30:21Z" finished-at="2018-06-13T12:30:40Z">
        </test-method> <!-- test -->
        <test-method status="PASS" signature="afterMethod()[pri:0, instance:com.mtfilemedia.ifive.testscripts.UIHTML5TC10SettingSupportUI@7e0e6aa2]" name="afterMethod" is-config="true" duration-ms="172" started-at="2018-06-13T12:30:41Z" finished-at="2018-06-13T12:30:42Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- afterMethod -->
        <test-method status="PASS" signature="testTearDown()[pri:0, instance:com.mtfilemedia.ifive.testscripts.UIHTML5TC10SettingSupportUI@7e0e6aa2]" name="testTearDown" is-config="true" duration-ms="2" started-at="2018-06-13T12:30:42Z" finished-at="2018-06-13T12:30:42Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- testTearDown -->
      </class> <!-- com.mtfilemedia.ifive.testscripts.UIHTML5TC10SettingSupportUI -->
    </test>

对于此配置,我得到test_status,但没有得到测试结果。

1 个答案:

答案 0 :(得分:0)

如果您想获取某个测试方法的属性,例如if ($('#A').prop('checked')) { choosedString = "^A$" } if ($('#Aplus').prop('checked')) { choosedString += choosedString.length > 0 ? "|" : ""; choosedString += "^A\\+$" } 属性中,XPath表达式的开头必须找到所需的测试方法节点,假设您正在寻找一个名为“ test”的节点: name 要获取该元素的任何属性,请将属性轴附加到表达式中。因此,您将获得以下XPath表达式来访问属性:

  • 状态: /testng-results/suite/test/class/test-method[@name='test']
  • 签名: /testng-results/suite/test/class/test-method[@name='test']/@status
  • 名称: /testng-results/suite/test/class/test-method[@name='test']/@signature
  • 是配置: /testng-results/suite/test/class/test-method[@name='test']/@name
  • duration-ms: /testng-results/suite/test/class/test-method[@name='test']/@is-config
  • 开始于: /testng-results/suite/test/class/test-method[@name='test']/@duration-ms
  • 完成于: /testng-results/suite/test/class/test-method[@name='test']/@started-at

您的第一个XPath表达式提取所有测试方法的文本内容,仅产生空白节点。 您的第二个XPath表达式不正确-属性具有值,但是没有text()子节点(实际上根本没有子节点)。如果您需要将属性值提取为字符串,请使用/testng-results/suite/test/class/test-method[@name='test']/@finished-at函数,例如string()。 您的第三个XPath表达式获取整个test-method元素而不是其属性。

希望这会有所帮助。