如何在空手道框架中获取 XML 响应值

How to get XML response values in karate framework

这是我的 XML 响应,我想使用空手道框架从响应中检索 Name 和 LastModifiedTime

<ADELdirectory:Directory
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ADELdirectory="http://www.com/XMLSchema/MT/Generic/v1.3" xsi:schemaLocation="http://www.com/XMLSchema/MT/Generic/v1.3 ADELdirectory.xsd">
    <Header>
        <Title>Directory listing</Title>
        <MachineID>TS01</MachineID>
        <MachineType></MachineType>
        <SoftwareRelease>Unknown</SoftwareRelease>
        <CreatedBy></CreatedBy>
        <CreateTime>2021-10-19T14:31:17.442Z</CreateTime>
        <Comment>
            <elt>url: http://localhost:8080/EDI/AD?cmd=list</elt>
        </Comment>
        <DocumentId>c0ceb67f-6498-4b5e-8c86-4f482121fb0c</DocumentId>
        <DocumentType>ADELdirectory</DocumentType>
        <DocumentTypeVersion>v1.3</DocumentTypeVersion>
    </Header>
    <Input>
        <Username>Framework</Username>
        <DocumentType>ADE</DocumentType>
        <DocumentPath>/</DocumentPath>
        <RequestParameterList>
            <elt>
                <Name>cmd</Name>
                <Value>list</Value>
            </elt>
        </RequestParameterList>
    </Input>
    <Results>
        <MainAttributeList/>
        <EntryList>
            <elt>
                <Type>Document</Type>
                <Name>TS01/LOT_0001/2021-10-12T14:35:28.835Z</Name>
                <LastModifiedTime>2021-10-12T14:50:28.836Z</LastModifiedTime>
                <Size>15235986</Size>
            </elt>
            <elt>
                <Type>Document</Type>
                <Name>TS01/LOT_AP_0001/2021-10-12T14:35:35.853Z</Name>
                <LastModifiedTime>2021-10-12T14:50:35.854Z</LastModifiedTime>
                <Size>15235986</Size>
            </elt>
        </EntryList>
    </Results>
</ADELdirectory:Directory>

当我 运行 .feature 文件时出现错误信息 'no variable found with name' 我需要定义正确的 XML 路径。

    * xml entryLists = $/Directory/Results/EntryList/elt

您可以查看报错信息:

这会起作用:

* def times = $response//Results//elt/LastModifiedTime
* print times
* def names = $response//Results//elt/Name
* print names

所以你得到 JSON 字符串数组。现在你可以轻松得到你想要的东西例如

* def firstName = names[0]
* match firstName == 'TS01/LOT_AP_0001/2021-10-12T14:35:28.835Z'