How to get XML response values in karate framework

Viewed 936

This is my XML Response and I would like to retrieve Name and LastModifiedTime from response by using karate framework

<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>

When I run .feature file there is a error message 'no variable found with name' I need to define correct XML path.

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

You can check the error message: This is the error message

1 Answers

This will work:

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

So you get JSON string arrays back. Now you can easy get what you want e.g.

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