Java 如何将参数传递给 Rest-Assured

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32475850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:37:09  来源:igfitidea点击:

How to pass parameters to Rest-Assured

javarest-assured

提问by Uday

Can someone help me in this scenario:

有人可以在这种情况下帮助我吗:

When I invoke this service, http://restcountries.eu/rest/v1/, I get couple of countries information.

当我调用此服务时http://restcountries.eu/rest/v1/,我会获得几个国家/地区信息。

But when I want to get any specific country information like say Finland, I invoke the web service as http://restcountries.eu/rest/v1/name/Finlandto get the country related info.

但是,当我想获取任何特定国家/地区信息(例如芬兰)时,我会调用 Web 服务http://restcountries.eu/rest/v1/name/Finland以获取与国家/地区相关的信息。

To automate the above scenario, how can I parameterize the country name in Rest-Assured? I tried below, but doesn't help me.

为了自动化上述场景,我如何在Rest-Assured 中参数化国家名称?我在下面尝试过,但对我没有帮助。

RestAssured.given().
                    parameters("name","Finland").
            when().
                    get("http://restcountries.eu/rest/v1/").
            then().
                body("capital", containsString("Helsinki"));

回答by Karthik R

You are invoking the GET call incorrectly.

您错误地调用了 GET 调用。

The parameters("name","Finland")will be converted only as queryparameter or formparameter for GETand POST/PUTrespectively

parameters("name","Finland")将被转换为仅查询参数或形式参数为GETPOST / PUT分别

RestAssured.when().
                    get("http://restcountries.eu/rest/v1/name/Finland").
            then().
                body("capital", containsString("Helsinki"));

is the only way to do. Since it's a java DSL, you can construct the URL all by yourself and pass it to get() if required

是唯一的办法。由于它是一个 Java DSL,您可以自己构建 URL 并在需要时将其传递给 get()

If the URL with a GET request had to fetch the same details been like :

如果带有 GET 请求的 URL 必须获取相同的详细信息,则如下所示:

http://restcountries.eu/rest/v1?name=Finland,

http://restcountries.eu/rest/v1?name=芬兰,

The your DSL would be something like:

您的 DSL 将类似于:

RestAssured.given().parameters("name","Finland").
                when().
                        get("http://restcountries.eu/rest/v1")

When you have a GET request, your parameters transform into queryParameters.

当您有 GET 请求时,您的参数将转换为queryParameters.

More info from this link: https://code.google.com/p/rest-assured/wiki/Usage#Parameters

来自此链接的更多信息:https: //code.google.com/p/rest-assured/wiki/Usage#Parameters

回答by рüффп

As the documentation explains:

正如文档解释的那样:

REST Assured will automatically try to determine which parameter type (i.e. query or form parameter) based on the HTTP method. In case of GET query parameters will automatically be used and in case of POST form parameters will be used.

REST Assured 将根据 HTTP 方法自动尝试确定哪种参数类型(即查询或表单参数)。在 GET 查询参数的情况下将自动使用,在 POST 表单参数的情况下将使用。

But in your case it seems you need path parameter instead query parameters. Note as well that the generic URL for getting a country is http://restcountries.eu/rest/v1/name/{country}where {country}is the country name.

但是在您的情况下,您似乎需要路径参数而不是查询参数。还要注意,为获得一个国家的通用网址是http://restcountries.eu/rest/v1/name/{country}这里{country}是国家名称。

Then, there are as well multiple ways to transfers path parameters.

然后,还有多种方式来传递路径参数。

Here are few examples

这里有几个例子

Example using pathParam():

使用 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"));

Example using variable:

使用变量的示例:

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"));

Now if you need to call different services, you can also parametrize the "service" like this:

现在,如果您需要调用不同的服务,您还可以像这样对“服务”进行参数化:

// 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"));

After you can also easily use the JUnit @RunWith(Parameterized.class)to feed the parameters 'svc' and 'value' for a unit test.

之后,您还可以轻松地使用 JUnit@RunWith(Parameterized.class)为单元测试提供参数“svc”和“value”。

回答by Aruna

parameters method is deprecated and start using params.

不推荐使用参数方法并开始使用参数。

Eg: RestAssured.given().params("name","Finland")

例如: RestAssured.given().params("name","Finland")

回答by Julian Kolodzey

You can define a request cpecification (say in a @Before) first:

您可以先定义一个请求 cpeification(在 a 中说@Before):

RequestSpecification requestSpecification = new RequestSpecBuilder()
    .setBaseUri (BASE_URL)
    .setBasePath (BASE_PATH)
    .addPathParam(
        "pathParamName", "pathParamValue",
        "anotherPathParamName", "anotherPathParamValue")
    .addQueryParam(
        "queryparamName", "queryParamValue",
        "anotherQueryParamName", "anotherQueryParamValue")
    .setAuth(basic(USERNAME, PASSWORD))
    .setContentType(ContentType.JSON)
    .setAccept(ContentType.JSON)
    .log(LogDetail.ALL)
    .build();

and then reuse it multiple times as follows:

然后多次重复使用,如下所示:

given()
    .spec(requestSpecification)
    .get("someResource/{pathParamName}/{anotherPathParamName}")
.then()
    .statusCode(200);

You can rebuild the request specification wherever you want. And if some changes will happen you can easily change your params names in one place.

您可以在任何地方重建请求规范。如果发生某些更改,您可以轻松地在一处更改您的参数名称。

I hope it helps.

我希望它有帮助。

回答by Atul

You can definitely user pathParam(String arg0, Object arg1) for achieving parameterization. Use the below example, if you using @DataProvider for providing the data.

您绝对可以使用 pathParam(String arg0, Object arg1) 来实现参数化。如果您使用 @DataProvider 提供数据,请使用以下示例。

By this you can provide multiple data using the DataProvider and also can also use APache POI for getting data from Excel Sheet.

通过这种方式,您可以使用 DataProvider 提供多个数据,也可以使用 APache POI 从 Excel Sheet 获取数据。

@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"));
    }