简体   繁体   中英

How to pass parameters to Rest-Assured

Can someone help me in this scenario:

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

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/Finland to get the country related info.

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

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

As the documentation explains:

REST Assured will automatically try to determine which parameter type (ie 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.

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 https://restcountries.com/v2/name/{country} where {country} is the country name.

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

Here are few examples

Example using pathParam():

// Here the key name 'country' must match the url parameter {country}
RestAssured.given()
        .pathParam("country", "Finland")
        .when()
            .get("https://restcountries.com/v2/name/{country}")
        .then()
            .body("capital", containsString("Helsinki"));

Example using variable:

String cty = "Finland";

// Here the name of the variable (`cty`) have no relation with the URL parameter {country}
RestAssured.given()
        .when()
            .get("https://restcountries.com/v2/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("https://restcountries.com/v2/{service}/{value}", svc, val)
        .then()
            .body("capital", containsString("Helsinki"));


// Search by ISO code (alpha)
val = "CH"
svc = "alpha"

RestAssured.given()
        .when()
            .get("https://restcountries.com/v2/{service}/{value}", svc, val)
        .then()
            .body("capital", containsString("Bern"));

// Search by phone intl code (callingcode)
val = "359"
svc = "callingcode"

RestAssured.given()
        .when()
            .get("https://restcountries.com/v2/{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.

You are invoking the GET call incorrectly.

The parameters("name","Finland") will be converted only as query parameter or form parameter for GET and POST/PUT respectively

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

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

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

The your DSL would be something like:

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

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

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

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

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.

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

By this you can provide multiple data using the DataProvider and also can also use APache POI for getting data from 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"));
    }
HttpUrl url1 = HttpUrl.parse("https:google.com").newBuilder()
                       .addQueryParameter("search","parameter")
                       .build();

parameters method is deprecated and start using params.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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