Java 如何将参数传递给Rest Assured
有人能在这种情况下帮助我吗: 当我调用此服务时,Java 如何将参数传递给Rest Assured,java,rest-assured,Java,Rest Assured,有人能在这种情况下帮助我吗: 当我调用此服务时,http://restcountries.eu/rest/v1/,我得到了几个国家的信息 但是,当我想要获得任何特定的国家信息(比如芬兰)时,我调用web服务作为http://restcountries.eu/rest/v1/name/Finland获取国家/地区相关信息 要自动化上述场景,如何在Rest Assured中参数化国家名称?我在下面试过了,但没用 RestAssured.given(). par
http://restcountries.eu/rest/v1/
,我得到了几个国家的信息
但是,当我想要获得任何特定的国家信息(比如芬兰)时,我调用web服务作为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"));
您正在错误地调用GET调用
参数(“name”、“Finland”)
将分别转换为获取和发布/发布的查询参数或表单参数
RestAssured.when().
get("http://restcountries.eu/rest/v1/name/Finland").
then().
body("capital", containsString("Helsinki"));
这是唯一的办法。因为它是java DSL,所以您可以自己构造URL,并在需要时将其传递给get()
如果带有GET请求的URL必须获取相同的详细信息,则如下所示:
,
您的DSL将类似于:
RestAssured.given().parameters("name","Finland").
when().
get("http://restcountries.eu/rest/v1")
当您有GET请求时,您的参数将转换为queryParameters
有关此链接的更多信息:
如文档所述: REST Assured将自动尝试确定哪种参数类型 (即查询或表单参数)基于HTTP方法。万一 GET查询参数将自动使用,如果是POST 将使用表单参数 但在您的情况下,似乎需要路径参数而不是查询参数。 还要注意,获取国家/地区的通用URL是
http://restcountries.eu/rest/v1/name/{country}
其中,{country}
是国家名称
然后,还有多种传输路径参数的方法
这里有几个例子
使用pathParam()的示例:
使用变量的示例:
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"));
现在,如果您需要调用不同的服务,您还可以将“服务”参数化,如下所示:
// 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"));
之后,您还可以轻松地使用JUnit
@RunWith(Parameterized.class)
为单元测试提供参数“svc”和“value” parameters方法已弃用,请开始使用params
例如:
RestAssured.given().params(“name”,“Finland”)
您可以先定义一个请求指定(比如在@之前的>):
然后重复使用多次,如下所示:
given()
.spec(requestSpecification)
.get("someResource/{pathParamName}/{anotherPathParamName}")
.then()
.statusCode(200);
您可以在任何需要的地方重新生成请求规范。如果发生一些变化,您可以在一个地方轻松地更改您的参数名称
我希望它能有所帮助。您完全可以使用pathParam(字符串arg0,对象arg1)来实现参数化。如果使用@DataProvider提供数据,请使用以下示例
这样,您可以使用DataProvider提供多个数据,还可以使用ApachePOI从Excel工作表获取数据
@DataProvider(name="countryDataProvider")
public String[][] getCountryData(){
String countryData[][] = {{"Finland"}, {"India"}, {"Greenland"}};
return (countryData);
}
@Test(dataProvider="countryDataProvider")
public void getCountryDetails(String countryName){
given().
pathParam("country", countryName).
when().
get("http://restcountries.eu/rest/v1/name/{country}").
then().
assertThat().
.body("capital", containsString("Helsinki"));
}
似乎,您的Web服务调用是不正确的,当我调用时,我会获取所有国家的信息,而如果您尝试“”则会获取特定的国家信息。如果我们按值传递参数,那么在这里使用参数有什么好处?字符串strCountryName=“芬兰”;RestAssured.given()//参数(“名称”、“芬兰”)。when()。get(“).then().body(“大写”,hasItem(“赫尔辛基”);为什么我需要在上面注释的参数?在这种情况下,您不需要参数()完全可以。这是你可以做的另一个选择。参数仅用于GET的查询参数和POST/put表单参数。如果你明白我的意思,就不用于路径参数。Karthik,你能详细说明一下吗。它可以很好地用于示例。这是一种非常优雅的路径值参数化方法!正如RequestSpecification API所述,这种方法可以es确实提高了可读性并允许路径重用!是的,但这仍然是将参数转换为查询参数,在这种情况下,这不是API想要的。对于路径参数,您必须使用pathParam(…)
相反。请注意,正确的名称是param
,而不是params
在测试的第二次迭代中将发生什么?我认为您还应该在数据提供程序中添加期望值。
given()
.spec(requestSpecification)
.get("someResource/{pathParamName}/{anotherPathParamName}")
.then()
.statusCode(200);
@DataProvider(name="countryDataProvider")
public String[][] getCountryData(){
String countryData[][] = {{"Finland"}, {"India"}, {"Greenland"}};
return (countryData);
}
@Test(dataProvider="countryDataProvider")
public void getCountryDetails(String countryName){
given().
pathParam("country", countryName).
when().
get("http://restcountries.eu/rest/v1/name/{country}").
then().
assertThat().
.body("capital", containsString("Helsinki"));
}
HttpUrl url1 = HttpUrl.parse("https:google.com").newBuilder()
.addQueryParameter("search","parameter")
.build();