Java – how to pass parameters to rest assured

Someone can help me in this situation:

When I call this service, http://restaccounts.com EU / rest / V1 /, I got information from several countries

But when I want to get information about a specific country like Finland, I call the web service http://restcountries.eu/rest/v1/name/Finland To get information about the country

To automate the above scenario, how do I parameterize the country name in rest assured? I had an interview under, but it didn't help me

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

Solution

As described in the document:

But in your case, it seems that you need path parameters instead of query parameters Also note that the common URL for getting countries is http://restcountries.eu/rest/v1/name/ {country} where {country} is the country name

Then, there are many ways to transmit path parameters

Here are a few examples

Example of using 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"));

Examples of using variables:

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 parameterize 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}",containsString("Bern"));

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

RestAssured.given()
        .when()
            .get("http://restcountries.eu/rest/v1/{service}/{value}",containsString("Sofia"));

Then you can easily use JUnit @ runwith (parameterized. Class) to provide parameters' SVC 'and' value 'for unit tests

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>