Showing posts with label RestAssured. Show all posts
Showing posts with label RestAssured. Show all posts

RestAssuredGET

 Rest Assured with GET, a complete code:

Feature File:

Feature: Rest Assured API test

 

Scenario Outline: Validate rest assured API

Given User enters an URL-End point<URL>

Then User validates the execution status<statusCode>

Then User validates other parameters<total>and<lastName>and<text>

Examples:

|URL | statusCode | total | lastName | text |

|https://reqres.in/api/users?page=2  |200       | 12   |Lawson  | To keep ReqRes free, contributions towards server costs are appreciated!  |

 

 

Test Runner Class:

package testRunner;

 

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;

import cucumber.api.junit.Cucumber;

 

 

@RunWith(Cucumber.class)

@CucumberOptions(

features = "C:\\JavaProject\\...\\restAssuredGET.feature",

 

glue = {"restAssuredTest1"},

format = {"pretty", "pretty:test-output/prettyFormat.txt","usage:test-output/usageFormat.json","rerun:test-output/rerun.txt", "html:test-output","json:test-output/jsonReport.json","junit:test-output/junitReport.xml"},

monochrome = true,

dryRun = false,

strict = true

)

 

public class TestRunner {

      

 

}

 

Step Definition:

 

 

package restAssuredTest1;

 

import cucumber.api.java.Before;

import cucumber.api.java.en.Given;

import cucumber.api.java.en.Then;

import io.restassured.response.Response;

import static io.restassured.RestAssured.*;

import static org.hamcrest.Matchers.*;

import java.util.HashMap;

 

public class RestAssuredCucumberBDD_May1st {

             

       Response response ;

       String responseBoydAsString;

       HashMap<Object, Object> map;

      

       @Before()

       public void beforeMethod() {

       given().

       header("Key","value").//to give header

       //header("Key","value").accept(null)//extra informaton in the header

       param("key","value");//to give parameter

       }

      

       @Given("^User enters an URL-End point(.*)$")

       public void apiURL(String url) {

 

             response = get(url);

             responseBoydAsString = response.asString();

             System.out.println("responseBoydAsString =\n"+responseBoydAsString);

            

       }

      

       @Then("^User validates the execution status(.*)$")

       public void apiStatusCodeValidation(int exptectedstatusCode) {

             int actualStatuscode = response.getStatusCode();

             System.out.println("statuscode =\n"+exptectedstatusCode);

             response.then().statusCode(exptectedstatusCode);

       }

      

       @Then("^User validates other parameters(.*)and(.*)and(.*)$")

       public void apiParametersValidations(int totalValue,String lastName,String text) {

             response.then()

             .body("total",equalTo(totalValue))

             .body("data.last_name[0]",equalTo(lastName))// here index position is 0

             .body("support.text",equalTo(text)) //To get the path of any particular element use     https://jsonpathfinder.com/

             .body("data.id",hasSize(6))

             .header("Server",equalTo("cloudflare"))//this is from response header

             .log().all()//this will print all the response in the console in the same format like in postman

             .body("data.first_name", hasItems("Byron","Tobias"));

            

       }

}

 

Response:

{

    "page": 2,

    "per_page": 6,

    "total": 12,

    "total_pages": 2,

    "data": [

        {

            "id": 7,

            "email": "michael.lawson@reqres.in",

            "first_name": "Michael",

            "last_name": "Lawson",

            "avatar": "https://reqres.in/img/faces/7-image.jpg"

        },

        {

            "id": 8,

            "email": "lindsay.ferguson@reqres.in",

            "first_name": "Lindsay",

            "last_name": "Ferguson",

            "avatar": "https://reqres.in/img/faces/8-image.jpg"

        },

        {

            "id": 9,

            "email": "tobias.funke@reqres.in",

            "first_name": "Tobias",

            "last_name": "Funke",

            "avatar": "https://reqres.in/img/faces/9-image.jpg"

        },

        {

            "id": 10,

            "email": "byron.fields@reqres.in",

            "first_name": "Byron",

            "last_name": "Fields",

            "avatar": "https://reqres.in/img/faces/10-image.jpg"

        },

        {

            "id": 11,

            "email": "george.edwards@reqres.in",

            "first_name": "George",

            "last_name": "Edwards",

            "avatar": "https://reqres.in/img/faces/11-image.jpg"

        },

        {

            "id": 12,

            "email": "rachel.howell@reqres.in",

            "first_name": "Rachel",

            "last_name": "Howell",

            "avatar": "https://reqres.in/img/faces/12-image.jpg"

        }

    ],

    "support": {

        "url": "https://reqres.in/#support-heading",

        "text": "To keep ReqRes free, contributions towards server costs are appreciated!"

    }

}

 

Output: