简体   繁体   English

RestAssured-如何检查ID <a id=“some number”></a> ?

[英]RestAssured - how to check id in <a id=“some number”></a>?

I am testing a web service, which returns an xml like in the example below 我正在测试一个Web服务,它返回一个xml,如下面的示例所示

<test xmlns="some url">
   <a id="482b6f66-0543-49b2-9a22-d70dc1a87032" href="some url">some text</a>
</test>

I looked through RestAssured documentation but I could not find how to check individual xml properties. 我浏览了RestAssured文档,但找不到如何检查单个xml属性。 I want to test "id" property, returned in the response, but my test fails, telling me that actual vs expected is empty, though I can clearly see that the tag is there and id value is present, if I output xml response as a String. 我想测试响应中返回的“ id”属性,但是我的测试失败,告诉我实际vs预期为空,但是如果我输出xml响应,我可以清楚地看到标记在那里并且id值存在一个字符串。

Here's my test: 这是我的测试:

expect().body("test.a.id", equalTo(expectedId)).when().post(myRequest);

您错过了@应该是:

expect().body("test.a.@id", equalTo(expectedId)).when().post(myRequest);

Since your response is XML you can use XPath: 由于您的响应是XML,因此可以使用XPath:

expect().body(hasXPath("/test/a/@id"), equalTo(expectedId)).when().post(myRequest);

@ is the way you should address attributes in xpath. @是您应该在xpath中处理属性的方式。 Note that this might also help you with a solution using XMLPath. 请注意,这也可能对使用XMLPath的解决方案有所帮助。 (see the examples here ) (请参见此处的示例)

Perhaps your usage of namespaces might give some troubles though, you can enable that using: 也许您使用命名空间可能会带来一些麻烦,但是您可以使用以下命令启用它:

given().
    config(newConfig().xmlConfig(xmlConfig().with().namespaceAware(true))).
expect().
    body(hasXPath("/yourns:test/yourns:a/@id"), equalTo(expectedId)).when().post(myRequest);

see also: http://code.google.com/p/rest-assured/wiki/Usage#XPath 另请参见: http : //code.google.com/p/rest-assured/wiki/Usage#XPath

The solution turned out to be a bit ugly, but I could not find any relevant examples of this usage, so it's all a guess work. 解决方案结果看起来有点丑陋,但是我找不到这种用法的任何相关示例,因此这全都是猜测。 Maybe there's a better way, but here's what I came up with: 也许有更好的方法,但是这是我想出的:

Here's the code to find and print my id: 这是查找并打印我的ID的代码:

String xml = get(myRequest).asString();
XmlPath xmlPath = new XmlPath(xml).setRoot("test");
String id = xmlPath.get("a.@id");
Assert.assertTrue(id.equals(expectedId));

This worked for me with xml: 这适用于我的xml:

<example id="12345">
    <someStuff></someStuff>
</example>

I could verify the attribute using: 我可以使用以下方法验证属性:

given().
when().
    post("/example/get/").
then().
    body(
        "example.@id", equalTo("12345")
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM