How To Pass Data From One Api To Another
Introduction
Every bit a part of theFinish to End REST Assured Tutorial, in this postal service, nosotros will learn to pass data from 1 API to another API.
Information is fundamental in API testing and we may require to laissez passer the output of ane API equally an input to another API. For example:- We get a booking id from Create Booking API. If we demand to recollect, update, partial update, or delete booking and so we must need to pass the booking id to these APIs.
We will employ TestNG equally a testing framework so that we can easily share data amidst test methods.
I have already covered Sharing Data Among Test Methods In TestNG Using ITestContext. In this mail service, I will explain the same concept with respect to Residuum Assured.
Required Dependencies
I have used Rest Assured and TestNG of the below versions:-
< ! -- https : //mvnrepository.com/antiquity/org.testng/testng --> < dependency > < groupId > org . testng < / groupId > < artifactId > testng < / artifactId > < version > 7.iii.0 < / version > < scope > test < / telescopic > < / dependency > |
< ! -- https : //mvnrepository.com/artifact/io.rest-bodacious/rest-assured --> < dependency > < groupId > io . rest - assured < / groupId > < artifactId > rest - assured < / artifactId > < version > iv.3.three < / version > < scope > examination < / telescopic > < / dependency > |
Interface ITestContext
Every bit per TestNG Javadoc, ITestContext is an interface that defines a exam context that contains all the information for a given examination run. An instance of this context is passed to the test listeners so they can query information near their environment.
ITestContext is a powerful interface that provides many useful methods. In this post, we will run across two of import methods setAttribute(java.lang.Cord name, java.lang.Object value) and getAttribute(java.lang.String name) provided past ITestContext interface.
setAttribute()
setAttribute(atttributeName, attributeValue) – Set a custom attribute. Information technology is similar to calculation an element in a Map equally primal-value pair. Kindly pay attending here that aspect value tin be of any type. This is the reason this method accepts Object type as a value.
getAttribute()
getAttribute(attributeName) – Go the value of given attribute name. Call back render type is an Object.
How to use ITestContext?
ITestContext interface extends IAttributes interface. Instead of going theoretically, let'southward learn it using examples. Information technology volition make more than sense to you.
ITestContext reference is created once and can be used with the @Test annotated method past just passing it as a parameter. Whatsoever information y'all would like to use in other tests you just need to shop them in Exam Context using setAttribute() method. To remember stored information apply getAttribute() method.
We volition utilise Restful – Booker APIs for demo purposes. I have already covered many examples of these APIs in my RestAssured series.
Example Program
one ii 3 4 5 6 7 8 ix x 11 12 thirteen fourteen 15 16 17 18 19 twenty 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 twoscore 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 sixty 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | bundle SharingData ; import org . testng . ITestContext ; import org . testng . annotations . Exam ; import io . restassured . RestAssured ; import io . restassured . http . ContentType ; public class ShareDataUsingITestContext { @ Test public void createBooking ( ITestContext context ) { int bookingId = RestAssured . given ( ) . log ( ) . all ( ) . baseUri ( "https://restful-booker.herokuapp.com/" ) . basePath ( "booking" ) . contentType ( ContentType . JSON ) . body ( "{\r\northward" + " \"firstname\" : \"Jim\",\r\northward" + " \"lastname\" : \"Brown\",\r\n" + " \"totalprice\" : 111,\r\n" + " \"depositpaid\" : true,\r\n" + " \"bookingdates\" : {\r\n" + " \"checkin\" : \"2018-01-01\",\r\northward" + " \"checkout\" : \"2019-01-01\"\r\n" + " },\r\due north" + " \"additionalneeds\" : \"Breakfast\"\r\north" + "}" ) . when ( ) . post ( ) . then ( ) . log ( ) . all ( ) . extract ( ) . jsonPath ( ) . go ( "bookingid" ) ; // Storing data in a context to use for other tests context . setAttribute ( "bookingId" , bookingId ) ; } @ Test public void updateBooking ( ITestContext context ) { // Retrieving required data from context int bookingId = ( int ) context . getAttribute ( "bookingId" ) ; RestAssured . given ( ) . log ( ) . all ( ) . baseUri ( "https://restful-booker.herokuapp.com/" ) . basePath ( "booking/" + bookingId ) . header ( "Authorization" , "Basic YWRtaW46cGFzc3dvcmQxMjM=" ) . contentType ( ContentType . JSON ) . body ( "{\r\north" + " \"firstname\" : \"Amod\",\r\n" + " \"lastname\" : \"Mahajan\",\r\due north" + " \"totalprice\" : 222,\r\northward" + " \"depositpaid\" : true,\r\northward" + " \"bookingdates\" : {\r\northward" + " \"checkin\" : \"2022-01-01\",\r\n" + " \"checkout\" : \"2022-01-01\"\r\n" + " },\r\n" + " \"additionalneeds\" : \"Breakfast\"\r\northward" + "}" ) . when ( ) . put ( ) . and so ( ) . log ( ) . all ( ) ; } } |
Output
1 2 iii four 5 6 seven 8 nine 10 11 12 thirteen 14 15 16 17 xviii nineteen 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 xl 41 42 43 44 45 46 47 48 49 fifty 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | [ RemoteTestNG ] detected TestNG version 7.0.1 Request method : POST Request URI : https : //restful-booker.herokuapp.com/booking Proxy : < none > Request params : < none > Query params : < none > Grade params : < none > Path params : < none > Headers : Accept = * /* Content-Type=awarding/json; charset=UTF-viii Cookies: <none> Multiparts: <none> Torso: { "firstname": "Jim", "lastname": "Dark-brown", "totalprice": 111, "depositpaid": true, "bookingdates": { "checkin": "2018-01-01", "checkout": "2019-01-01" }, "additionalneeds": "Breakfast" } HTTP/1.i 200 OK Server: Cowboy Connection: go on-alive Ten-Powered-Past: Express Content-Type: application/json; charset=utf-8 Content-Length: 195 Etag: W/"c3-mr+ToVU3dK7bNqxVpsnhD1SC7cM" Date: Wed, 27 Jan 2021 06:23:23 GMT Via: i.1 vegur { "bookingid": 11, "booking": { "firstname": "Jim", "lastname": "Brown", "totalprice": 111, "depositpaid": true, "bookingdates": { "checkin": "2018-01-01", "checkout": "2019-01-01" }, "additionalneeds": "Breakfast" } } Request method: PUT Request URI: https://restful-booker.herokuapp.com/booking/xi Proxy: <none> Request params: <none> Query params: <none> Class params: <none> Path params: <none> Headers: Authorization=Basic YWRtaW46cGFzc3dvcmQxMjM= Accept=*/ * Content - Type = application / json ; charset = UTF - 8 Cookies : < none > Multiparts : < none > Torso : { "firstname" : "Amod" , "lastname" : "Mahajan" , "totalprice" : 222 , "depositpaid" : true , "bookingdates" : { "checkin" : "2022-01-01" , "checkout" : "2022-01-01" } , "additionalneeds" : "Breakfast" } HTTP / i.1 200 OK Server : Cowboy Connection : keep - live X - Powered - Past : Express Content - Type : application / json ; charset = utf - 8 Content - Length : 171 Etag : W / "ab-+iqLT0fOvVL3GfV0ed6NlH849m8" Appointment : Wed , 27 Jan 2021 06 : 23 : 25 GMT Via : ane.1 vegur { "firstname" : "Amod" , "lastname" : "Mahajan" , "totalprice" : 222 , "depositpaid" : true , "bookingdates" : { "checkin" : "2022-01-01" , "checkout" : "2022-01-01" } , "additionalneeds" : "Breakfast" } PASSED : createBooking ( org . testng . TestRunner @ 1990a65e ) PASSED : updateBooking ( org . testng . TestRunner @ 1990a65e ) === === === === === === === === === === === === === === === == Default test Tests run : 2 , Failures : 0 , Skips : 0 === === === === === === === === === === === === === === === == === === === === === === === === === === === === === === === == Default suite Total tests run : 2 , Passes : 2 , Failures : 0 , Skips : 0 === === === === === === === === === === === === === === === == |
You tin can download/clone the in a higher place sample projection from here.
You lot tin can subscribe to my YouTube channelRetargetCommon to learn from video tutorials.
If you have any uncertainty, feel free to comment beneath.
If you lot similar my posts, delight like, comment, share and subscribe.
#ThanksForReading
#HappyLearning
Find all Selenium related posts here, all API manual and automation related posts here, and detect ofttimes asked Java Programs hither.
Many other topics you tin navigate through the menu.
Author: Amod Mahajan
A software Tester who is paid to approximate products developed past others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.
Source: http://makeseleniumeasy.com/2021/01/27/rest-assured-tutorial-64-how-to-pass-value-from-one-api-to-another-api-using-testng-itestcontext/
0 Response to "How To Pass Data From One Api To Another"
Post a Comment