如何验证项目是否存在于SoapUI的列表响应中?

时间:2014-06-10 19:42:57

标签: rest groovy soapui

简短问题:在SoapUI / SoapUI Pro测试步骤中,如何验证作为列表的REST响应是否包含特定项目?

长版本:我有一个测试套件,它将两个REST方法调用链接在一起。首先,我致电addCustomer,然后致电getCustomerByPhoneNumber。但是,电话号码不是唯一的,所以我可能会找回几个客户的列表。如何确定该列表是否包含我刚刚添加的客户?

示例:假设我致电addCustomer创建customer2,响应返回customerId = 222。然后我致电getCustomerByPhoneNumber并收到以下回复。如何验证列表中是否存在customerId = 222?理想情况下,我还要验证有关customer2的所有信息是否正确(电话号码,姓名等)

<customers>
    <customer>
        <id>111</id>
        <name>customer1</name>
        <phone>555-5555</phone>
    </customer>
    <customer>
        <id>222</id>
        <name>customer2</name>
        <phone>555-5555</phone>
    </customer>
    <customer>
        <id>333</id>
        <name>customer3</name>
        <phone>555-5555</phone>
    </customer>
</customers>

如果答案需要Groovy脚本,我会感谢一些示例代码或伪代码,因为我之前没有使用过Groovy。

2 个答案:

答案 0 :(得分:0)

使用XPath,类似exists(//*:customer[name[text()='customer2']]),以查看是否&#34;他&#34;存在。

接下来,像//*:customer[name[text()='customer2']]/id这样的内容应该会给你&#34; 222&#34;。

答案 1 :(得分:0)

您还可以在测试步骤中使用XQuery断言,如下所示:

for $customer in //*:customer
where ($customer/id = '222')
return ($customer/name,
        $customer/phone)

这会产生如下输出:

<name>customer2</name>
<phone>555-5555</phone> 

然后,在断言预期结果面板中,您可以替换预期值:

<name>${customerName}</name>
<phone>${custmerPhone}</phone>