What is Request Specification
Request 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 request can be clubbed in a common entity and can be used as Request Specification
Lets Take an example, considering a POST request before applying Request 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 TwitterGETRequest { 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 common request parameters and group them in a common entity and name it as request 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.authentication.AuthenticationScheme; import io.restassured.builder.RequestSpecBuilder; import io.restassured.specification.RequestSpecification; public class RequestSpecificationDemo { String consumerKey = "ZJJb3jOpA567O1kV52KrjHAeD"; String consumerSecret = "meFz7bYLEqv0xwUzZedg3sA8HYpEbxmSYnkEwF4C"; String accessToken = "9694139200490364748uetruKDH4q8d6K5xM4FkV7zOsBzT"; String accessSecret = "gudUTtk4chtm9jfhfjfjfjfQOu9Fl0MV6GwtXfMXlk9Vd"; String tweetId = ""; RequestSpecBuilder requestBuilder; static RequestSpecification requestSpec; @BeforeClass public void setup() { AuthenticationScheme authScheme = RestAssured.oauth(consumerKey, consumerSecret, accessToken, accessSecret); requestBuilder = new RequestSpecBuilder(); requestBuilder.setBaseUri("https://api.twitter.com"); requestBuilder.setBasePath("/1.1/statuses"); requestBuilder.setAuth(authScheme); requestSpec = requestBuilder.build(); } @Test public void updateTweet() { given() .spec(requestSpec) .queryParam("status", "Test Tweet23") .when() .post("/update.json") .then() .statusCode(200); } } |