UI自动化:导出元素树
Posted
技术标签:
【中文标题】UI自动化:导出元素树【英文标题】:UI automation: export element tree 【发布时间】:2014-02-27 02:21:30 【问题描述】:在 UI 自动化中,有没有办法以不同于默认格式(例如 XML)的格式导出 logElementTree()
生成的元素树。我想在 UI 自动化之外使用元素层次结构。
我在测试脚本中使用logElementTree()
记录了树并且它成功了,在编辑器日志中显示为“通过”。我曾尝试“导出跟踪结果”,但这似乎并没有奏效。
【问题讨论】:
【参考方案1】:您可以使用 Appium 检查器获取 XML 结果:Appium
Appium 是一个开源自动化框架。它还提供了获取应用程序 UI 层次结构的工具。
按照说明启动 Appium 检查器。填写所需的信息,例如设备类型和 UDID。截图:Appium Inspector
使用 Appium GUI,右下角有一个“复制 XML”按钮。按刷新按钮获取当前屏幕的 UI 层次结构。
这是计算器应用程序的结果。
<?xml version="1.0" encoding="UTF-8"?>
<AppiumAUT>
<UIAApplication name="Calculator" label="Calculator" value="" dom="" enabled="true" valid="true" visible="true" hint="" path="/0" x="0" y="20" >
<UIAWindow name="" label="" value="" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0" x="0" y="0" >
<UIAStaticText name="Result" label="Result" value="0" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/0" x="0" y="0" >
</UIAStaticText>
<UIAButton name="all clear" label="all clear" value="all clear" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/1" x="1.5" y="169.5" >
</UIAButton>
<UIAButton name="plus, minus" label="plus, minus" value="plus, minus" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/2" x="80.5" y="169.5" >
</UIAButton>
<UIAButton name="percent" label="percent" value="percent" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/3" x="160" y="169.5" >
</UIAButton>
<UIAButton name="divide" label="divide" value="divide" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/4" x="239.5" y="169.5" >
</UIAButton>
<UIAButton name="7" label="7" value="7" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/5" x="1.5" y="249" >
</UIAButton>
<UIAButton name="8" label="8" value="8" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/6" x="80.5" y="249" >
</UIAButton>
<UIAButton name="9" label="9" value="9" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/7" x="160" y="249" >
</UIAButton>
<UIAButton name="multiply" label="multiply" value="multiply" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/8" x="239.5" y="249" >
</UIAButton>
<UIAButton name="4" label="4" value="4" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/9" x="1.5" y="328.5" >
</UIAButton>
<UIAButton name="5" label="5" value="5" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/10" x="80.5" y="328.5" >
</UIAButton>
<UIAButton name="6" label="6" value="6" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/11" x="160" y="328.5" >
</UIAButton>
<UIAButton name="subtract" label="subtract" value="subtract" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/12" x="239.5" y="328.5" >
</UIAButton>
<UIAButton name="1" label="1" value="1" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/13" x="1.5" y="408" >
</UIAButton>
<UIAButton name="2" label="2" value="2" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/14" x="80.5" y="408" >
</UIAButton>
<UIAButton name="3" label="3" value="3" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/15" x="160" y="408" >
</UIAButton>
<UIAButton name="add" label="add" value="add" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/16" x="239.5" y="408" >
</UIAButton>
<UIAButton name="0" label="0" value="0" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/17" x="1.5" y="487.5" >
</UIAButton>
<UIAButton name="decimal" label="decimal" value="decimal" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/18" x="160" y="487.5" >
</UIAButton>
<UIAButton name="equals" label="equals" value="equals" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/0/19" x="239.5" y="487.5" >
</UIAButton>
</UIAWindow>
<UIAWindow name="" label="" value="" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/1" x="0" y="0" >
<UIAStatusBar name="" label="" value="" dom="" enabled="true" valid="true" visible="true" hint="" path="/0/1/0" x="0" y="0" >
<UIAElement name="2 of 5 bars, signal strength" label="2 of 5 bars, signal strength" value="" dom="" enabled="true" valid="true" visible="true" hint="Swipe down with three fingers to reveal the notification center., Swipe up with three fingers to reveal the control center, Double-tap to scroll to top" path="/0/1/0/0" x="6" y="0" >
</UIAElement>
<UIAElement name="T-Mobile network" label="T-Mobile network" value="" dom="" enabled="true" valid="true" visible="true" hint="Swipe down with three fingers to reveal the notification center., Swipe up with three fingers to reveal the control center, Double-tap to scroll to top" path="/0/1/0/1" x="44" y="0" >
</UIAElement>
<UIAElement name="3 of 3 Wi-Fi bars" label="3 of 3 Wi-Fi bars" value="FireEye" dom="" enabled="true" valid="true" visible="true" hint="Swipe down with three fingers to reveal the notification center., Swipe up with three fingers to reveal the control center, Double-tap to scroll to top" path="/0/1/0/2" x="97" y="0" >
</UIAElement>
<UIAElement name="10:48 AM" label="10:48 AM" value="" dom="" enabled="true" valid="true" visible="true" hint="Swipe down with three fingers to reveal the notification center., Swipe up with three fingers to reveal the control center, Double-tap to scroll to top" path="/0/1/0/3" x="134" y="0" >
</UIAElement>
<UIAElement name="95% battery power, Charging" label="95% battery power, Charging" value="" dom="" enabled="true" valid="true" visible="true" hint="Swipe down with three fingers to reveal the notification center., Swipe up with three fingers to reveal the control center, Double-tap to scroll to top" path="/0/1/0/4" x="282" y="0" >
</UIAElement>
</UIAStatusBar>
</UIAWindow>
</UIAApplication>
</AppiumAUT>
【讨论】:
虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。 感谢您的评论。我编辑了我的答案。如果有什么我可以改进的,请提出建议。【参考方案2】:http://www.w3schools.com/xsl/xsl_transformation.asp
上面的方法会起作用吗?
例如,这样的事情会将测试结果粗略地解析成 html 表格:
<table border="1" style="width:800px">
<tr>
<th>Timestammp</th>
<th>Sequence</th>
<th>Message</th>
<th>Type</th>
<th>Screenshot</th>
</tr>
<xsl:for-each select="plist/dict/array/dict">
<tr>
<td>
<xsl:value-of select="translate(translate(date, 'T',' '), 'Z','')"/>
</td>
<td><xsl:value-of select="position()"/></td>
<td><xsl:value-of select="string"/></td>
<td>
<xsl:choose>
<xsl:when test="integer = 8"><span style="color:blue">Screenshot</span></xsl:when>
<xsl:when test="integer = 5"><span style="color:green">Pass</span></xsl:when>
<xsl:when test="integer = 4"><span style="color:orange">Message</span></xsl:when>
<xsl:otherwise><xsl:value-of select="integer"/></xsl:otherwise>
</xsl:choose>
</td>
<td>
<xsl:if test="integer = 8">
<xsl:element name="img">
<xsl:attribute name="src"><xsl:value-of select="string"/>.png</xsl:attribute>
<xsl:attribute name="style">height:250px;width:167px;</xsl:attribute>
</xsl:element>
</xsl:if>
</td>
</tr>
</xsl:for-each>
</table>
作为一个注释,我已经有一段时间没有使用它了,因为我的项目还没有到那个时候,我相信它大部分来自某人的博客。如果我能再次找到该网站,我会发布它的功劳。
【讨论】:
以上是关于UI自动化:导出元素树的主要内容,如果未能解决你的问题,请参考以下文章