【问题标题】:Get sub-child attribute value traversing child nodes for first match第一次匹配获取遍历子节点的子子属性值
【发布时间】:2019-07-05 13:16:30
【问题描述】:

每个类都有 3 种或更多的测试方法。

每种测试方法都可以通过或失败。

零,一种或多种方法可能会失败。

要求是遍历test-method结果,当找到失败的方法(first match)时,获取'message'的属性值并打印出来。

如果没有一个方法失败,则打印空白。

我怎样才能做到这一点?


Input.xml
<testng-results>
<suite>
<test>
    <class name="activities.ActivitiesListTest">
    <test-method status="PASS" started-at="2019-02-07T18:24:47Z" name="initTest">
        <reporter-output>
        </reporter-output>
    </test-method>

    <test-method status="FAIL" started-at="2019-02-07T18:24:47Z" name="ActListsForContactShowsContactRelatedTasksTest">
        <exception class="org.openqa.selenium.NoSuchElementException">
            <message>
                <![CDATA[Element with locator 123 not present']]>
            </message>
        </exception>
        <reporter-output>
        </reporter-output>
    </test-method>

    <test-method status="FAIL" started-at="2019-02-07T18:24:47Z" name="afterClass">
        <exception class="org.openqa.selenium.NoSuchElementException">
        <message>
            <![CDATA[Message 3]]>
        </message>
        </exception>
        <reporter-output>
        </reporter-output>
    </test-method>
</class>
</test>
</suite>
</testng-results>

预期结果:

    <Suite>
    <test failed_reason=" <![CDATA[Element with locator 123 not 
    present']]>"/>
    </Suite>

尝试过 (XSL):


<Suite>
<xsl:for-each select="/class">
    <test>
        <xsl:attribute name="failed_reason">
            <xsl:choose>
                <xsl:when test="*[@status='FAIL']">test-method[position() = current()]/exception/@message</xsl:when>
            </xsl:choose>
        </xsl:attribute>
    </test>
</xsl:for-each>
</Suite>

异常消息的绝对路径示例:

suite/test/class[@name='ActivitiesListForContactShowsContactRelatedTasksTest']/test-method[@name='ActListsTest']/exception/message

但是,没用。

如何达到预期效果?

编辑: 尝试迈克尔的解决方案。

这是我的 XSL 当前的样子:


<xsl:template match="/">
<Suite>
    <xsl:for-each select="testng-results/suite/test/class">
        <test>
            <xsl:attribute name="start-time">
                <xsl:value-of select="test-method[1]/@started-at"/>
            </xsl:attribute>
            <xsl:attribute name="failed_reason">
                {normalize-space(test-method[@status='FAIL'][1]/exception/message)}
            </xsl:attribute>
        </test>
    </xsl:for-each>
</Suite>
</xsl:template>

【问题讨论】:

  • 可以有多个类吗?如果是,根元素是什么?
  • 是的。可以有不止一个类。 是根元素。失败消息的绝对 xpath 示例:/testng-results/suite/test/class[@name='ActivitiesListForContactShowsContactRelatedTasksTest']/test-method[@name='ActListsTest']/exception/message
  • 恐怕你在自相矛盾。如果suite 是根元素,则路径不能以/testng-results 开头。 -- 请不要在 cmets 中发布代码 - 改为编辑您的问题。
  • testng-results 是 xml 的名称。这就是 IDE 中显示的路径。请忽略它。让我们将路径视为从 /suite 开始
  • 已编辑问题

标签: xslt xslt-1.0


【解决方案1】:

我猜(!)你想做类似的事情:

<xsl:template match="/*">
    <Suite>
        <xsl:for-each select="class">
           <test failed_reason="{normalize-space(test-method[@status='FAIL'][1]/exception/message)}"/>
        </xsl:for-each>
    </Suite>
</xsl:template>

注意:

<xsl:for-each select="/class">

除非class 是根元素,否则不会选择任何内容。但是如果class 是根元素,那么只有一个类——所以使用xsl:for-each 是没有意义的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    相关资源
    最近更新 更多