What is Response Specification
Response specification helps to remove the duplicate code while building the framework .
In a real project we will have lot of test cases which uses common steps to make a request or validate a response for example – Base_URI , Base_Path , Header , Status Code
All the Common response can be clubbed in a common entity and can used as Response specification
Lets Take a POST Request before applying response specification
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package blog; import static io.restassured.RestAssured.given; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.path.json.JsonPath; import io.restassured.response.Response; public class TwitterPostRequest { String consumerKey = "ZJJb3jOpA567O1kV52KrjHAeD"; String consumerSecret = "meFz7bYLEqv0xwUzZedg3sA8HYpEbxmSYnkEwF4C"; String accessToken = "9694139200490364748uetruKDH4q8d6K5xM4FkV7zOsBzT"; String accessSecret = "gudUTtk4chtm9jfhfjfjfjfQOu9Fl0MV6GwtXfMXlk9Vd"; String tweetId = ""; @BeforeClass public void setup() { RestAssured.baseURI = "https://api.twitter.com"; RestAssured.basePath = "/1.1/statuses"; } @Test public void updateTweet() { Response response = given() .auth() .oauth(consumerKey, consumerSecret, accessToken, accessSecret) .queryParam("status", "My First Tweet11") .when() .post("/update.json") .then() .statusCode(200) .extract() .response(); tweetId = response.path("id_str"); System.out.println("The response.path: " + tweetId); } } |
Identify the response parameters that needs to be validated and group them in a common entity and name it as response specification and reuse them for all the test cases .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package blog; import static io.restassured.RestAssured.given; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.builder.ResponseSpecBuilder; import io.restassured.specification.ResponseSpecification; import static org.hamcrest.Matchers.*; public class ResponseSpecificationDemo { String consumerKey = "ZJJb3jOpA567O1kdsfdsgV52KrjHAeD"; String consumerSecret = "meFz7bYLEqv0xwUzZedg3dsfgdssA8HYpEbxmSYnkEwF4C"; String accessToken = "9694139200490364748uetruKDHgdsgd4q8d6K5xM4FkV7zOsBzT"; String accessSecret = "gudUTtk4chtm9jfhfjfjfjfQOu9Fl0MV6GwtXfMXlk9Vd"; String tweetId = ""; ResponseSpecBuilder responseBuilder; static ResponseSpecification responseSpec; @BeforeClass public void setup() { RestAssured.baseURI = "https://api.twitter.com"; RestAssured.basePath = "/1.1/statuses"; responseBuilder = new ResponseSpecBuilder(); responseBuilder.expectStatusCode(200); responseSpec = responseBuilder.build(); } @Test public void updateTweet() { given() .auth() .oauth(consumerKey, consumerSecret, accessToken, accessSecret) .queryParam("status", "TweetUpdate") .when() .get("/user_timeline.json") .then() .spec(responseSpec); } } |