有人可以在这种情况下帮助我:
当我调用此服务时http://restcountries.eu/rest/v1/,我会收到几个国家/地区的信息.
但是当我想获得像芬兰这样的特定国家信息时,我会调用网络服务http://restcountries.eu/rest/v1/name/Finland来获取与国家相关的信息.
要自动执行上述方案,如何在Rest-Assured中参数化国家/地区名称?我在下面试过,但没有帮助我.
RestAssured.given().
parameters("name","Finland").
when().
get("http://restcountries.eu/rest/v1/").
then().
body("capital", containsString("Helsinki"));
Run Code Online (Sandbox Code Playgroud)
рüф*_*ффп 16
正如文档所述:
REST Assured将自动尝试基于HTTP方法确定哪种参数类型(即查询或表单参数).在GET的情况下,将自动使用查询参数,并且在POST表格的情况下将使用参数.
但在你的情况下,似乎你需要路径参数而不是查询参数.还要注意,为获得一个国家的通用网址是http://restcountries.eu/rest/v1/name/{country}
这里{country}是国家名称.
然后,还有多种方式来传输路径参数.
这里有几个例子
使用pathParam()的示例:
// Here the key name 'country' must match the url parameter {country}
RestAssured.given()
.pathParam("country", "Finland")
.when()
.get("http://restcountries.eu/rest/v1/name/{country}")
.then()
.body("capital", containsString("Helsinki"));
Run Code Online (Sandbox Code Playgroud)
使用变量的示例:
String cty = "Finland";
// Here the name of the variable have no relation with the URL parameter {country}
RestAssured.given()
.when()
.get("http://restcountries.eu/rest/v1/name/{country}", cty)
.then()
.body("capital", containsString("Helsinki"));
Run Code Online (Sandbox Code Playgroud)
现在,如果您需要调用不同的服务,您还可以像这样参数化"服务":
// Search by name
String val = "Finland";
String svc = "name";
RestAssured.given()
.when()
.get("http://restcountries.eu/rest/v1/{service}/{value}", svc, val)
.then()
.body("capital", containsString("Helsinki"));
// Search by ISO code (alpha)
val = "CH"
svc = "alpha"
RestAssured.given()
.when()
.get("http://restcountries.eu/rest/v1/{service}/{value}", svc, val)
.then()
.body("capital", containsString("Bern"));
// Search by phone intl code (callingcode)
val = "359"
svc = "callingcode"
RestAssured.given()
.when()
.get("http://restcountries.eu/rest/v1/{service}/{value}", svc, val)
.then()
.body("capital", containsString("Sofia"));
Run Code Online (Sandbox Code Playgroud)
之后您还可以轻松使用JUnit @RunWith(Parameterized.class)为单元测试提供参数'svc'和'value'.
您错误地调用了 GET 调用。
将仅parameters("name","Finland")分别转换为GET和POST/PUT的查询参数或表单参数
RestAssured.when().
get("http://restcountries.eu/rest/v1/name/Finland").
then().
body("capital", containsString("Helsinki"));
Run Code Online (Sandbox Code Playgroud)
是唯一的办法。由于它是 java DSL,因此您可以自己构造 URL 并将其传递给 get()(如果需要)
如果带有 GET 请求的 URL 必须获取相同的详细信息,则如下所示:
http://restcountries.eu/rest/v1?name=芬兰,
你的 DSL 会是这样的:
RestAssured.given().parameters("name","Finland").
when().
get("http://restcountries.eu/rest/v1")
Run Code Online (Sandbox Code Playgroud)
当您有 GET 请求时,您的参数会转换为queryParameters.
此链接的更多信息: https ://code.google.com/p/rest-assured/wiki/Usage#Parameters
| 归档时间: |
|
| 查看次数: |
38081 次 |
| 最近记录: |