xml - 使用 XPath 从 XML 读取多个值
问题描述
我正在尝试从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
,但没有得到测试结果。
解决方案
如果您想获取某个测试方法的属性,例如name
属性,则 XPath 表达式的开头必须找到所需的测试方法节点,假设您正在寻找名为“test”的节点
/testng-results/suite/test/class/test-method[@name='test']
:获取该元素的任何属性,将属性轴附加到表达式。因此,您可以获得这些 XPath 表达式来访问属性:
- 地位:
/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
- 是配置:
/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
- 完成时间:
/testng-results/suite/test/class/test-method[@name='test']/@finished-at
您的第一个 XPath 表达式提取所有测试方法的文本内容,只产生空白节点。您的第二个 XPath 表达式不正确 - 属性具有值,但没有 text() 子节点(实际上,根本没有子节点)。如果您需要将属性的值提取为字符串,请使用string()
函数,例如string(/testng-results/suite/test/class/test-method[@name='test']/@status)
. 您的第三个 XPath 表达式获取整个 test-method 元素而不是它的属性。
希望这可以帮助。
推荐阅读
- javafx - 我正在尝试使用 SceneBuilder 加载 FXML 文件。它曾经可以工作,但现在我收到错误“无法打开'FXMLDocument.fxml'
- vba - 如何在整个文档中搜索选定的单词并突出显示?
- c++ - 迭代段树实现
- excel - 将充满对象的字典设置为空时,VBA 非常慢
- flutter - 在 Flutter 中使用橡皮擦覆盖图像
- javascript - 无法让 Greasemonkey 脚本专注于 VirusTotal.com 上的文本框
- python - Python - 更新多行
- c# - Visual Studio for Mac 不允许我创建新的表单项目
- reactjs - 我列出了我的关键道具,但仍然收到以下错误:列表中的每个孩子都应该有一个唯一的“关键”道具
- prestashop - WebServices 更新是否有任何默认触发器(挂钩)?