Transcript for:
JSON Validation and Postman Testing

okay so in our last class we have seen a Json right so Json a JavaScript object notation so how we can uh identify the nodes in the Json by writing Json path and how we can locate the data in the Json file right so the Json is mainly the part of the response so when you start validating the response and we need to understand the basic stuff about Json and then we are able to write the Json path that we are able to generate the Json path and accordingly we can validate the data so today we are going to see some other options which we can consider for validating the data foreign [Music] the response we need to consider a few important things and one is status code the next one is headers and then cookies response time and then response body so these are the main components which we are going to validate once you send the request and once you get a response from the request okay and before sending request and response we have to know what exactly what type of response we will get so if you don't know the type of response or if you don't know the components on response you cannot add validation points okay you don't know what exactly you have to validate in the response so before sending a request right before testing the apis we should have a Clarity on what kind of request we are sending and what kind of data we are sending as part of the request and what kind of data we are getting the response what kind of options or what kind of status headers cookies we are getting once get the response so these are the things which we have to know before sending the sending the request and getting the response and that is a prerequisite so if you don't know the response like if you don't know what are the things will be there in the response you cannot add proper validation points okay so before adding the validation point at least once you have to run the API at least once you have to send the request and accordingly you can analyze the data and as per your expectation where the response code is correct or not headers are correct or not cookies are correct or not and you can put some validation points for that particular request okay so when you put some validation points we need to consider mainly these five important things the status code we have to validate whenever you send some request and get the response then we need to check some headers and there are multiple headers are there and we can check one or two headers or we can check all headers and then cookies also and then response time and response body okay and these are the main things which we need to uh test so to test these items we need to add something called assert sensor okay so there is something called assertion assertion is nothing but what which is a validation point which is a validation point a session is called as a validation so to add these assertions in Postman there is one Library provided by Postman that is called pm what is a PM PM is a kind of a library and in which there are so many functions are available okay so many uh functions are available so by using these functions we can add validation point so instead of calling the functions or yeah so we can call them as a functions or we can say assertions also so in the postman Library so we have set a number of functions are available so by adding those functions we can validate these things status code headers cookies and so on and these functions are written by using JavaScript okay so Bossman internally use JavaScript so you don't need to learn JavaScript for that but you need to learn some basic functions are available assertion functions and if you know those functions you can easily put validation points okay so PM is a postman Library built-in Library which is available in Postman itself and which is providing set a number of functions to add validation points and those functions are developed by using JavaScript only so what we need to do now to put a session we have to just call the functions from the PM Library by passing certain number of parameters when I say a function which will take some parameter so we need to pass some parameters inside the functions so that it will perform the validations okay so how the functions we need to write them and even though we are calling the functions we need to write something called uh JavaScript function okay so we have to call all these functions from the VM Library by writing another JavaScript function so we need to write our own JavaScript function inside this function we need to call PM dot some function so whatever function is available in the PM Library so we will invoke those functions through our JavaScript functions we have to create one user defined function in which we have to call Postman function okay so I will show you the syntaxes how we can write all these things and then we will try to understand so here we have to create our own functions in two different ways so with one is a normal function the other one is Arrow function one is called normal function the other one is called Arrow function so which is not uh exactly the same as a Java syntax it is something different because which is following the JavaScript so the function user defined function we can write in two different ways one is a normal function the other one is Paro function I'll show you the syntax how to create and accordingly we can put all the validation points so let me share a small presentation so these are the items which we are going to test status code headers cookies and so on now if I just look at here so we have to use something called chai assertion Library so this is a kind of a JavaScript framework so normally when you're working with a Java we use junit test engine Frameworks right so why we'll use them because we want to organize our test cases we need to maintain the tests all these things so they will provide some certain number of features similarly when you're working with the JavaScript there are some Frameworks are available so like Moga framework chai framework like that so especially Postman will use chai framework chai is a name of the framework and which contains certain number of functions okay so for example in the chai assertion Library we have to create a function like this PM is what a PM is a postman DOT test what is the test here as I said Postman Library contains some functions predefined functions so test is what a test is a function now here we need to provide the test name in The Double quotations comma and function is a keyword function is a keyword so include the bracket and this function implementation start from the curly brace so this is a starting point and this is the ending point and inside this we need to implement the function Body by writing a assertion and this particular test function is closed here okay this one flows here so this is the one way of writing the user defined function in JavaScript okay the same way we have to write in Postman also so this is the one type of syntax the other way is by writing Arrow function so Arrow function is nothing but what we can say PM DOT test and here test name and here instead of writing function and bracket keyword we just say simply one bracket we don't specify the function keyword and after that we say one Arrow so this is basically equal to and greater than the combination we need to write so this is called Arrow and then starting point and this is the ending point and here we need to write the assertion and this test function is got closed here here it is got closed so this is a normal function and this is the arrow function we can call it as Arrow function so in JavaScript we can write functions in two different ways we can write user defined functions in two different ways one is a normal function by using function keyword the other one is by using Arrow which is called as Arrow function and we don't need to write the function keyword here instead of that we need to just write one Arrow Mark here okay so either we can use the first one or second one but second one is the most popular one many times we use Arrow functions so by writing this error function we can write validation points for every component suppose if you want to test a status code or header or cookies responsible whatever it is we need to write that assertion inside the function inside the function we have to write the session so here we need to include that validation pointer okay so this is a syntax now we'll see one by one how we can write this function to validate different components of response now the first let us start with the status code testing status codes to test the status code what we need to do is we need to write a assertion like this but where we have to write all these things the postman Tool uh if you just go to the request any request and here you can see when I open the request you can see some uh tabs here parameters authorization headers body pre-request skip test and settings there are multiple options are there right so here we need to go to test tab so inside the test tab we may go here you will see the window here so here we have to write all the validation functions so this is the area where exactly we have to put all the validation points so now later we will add multiple validation points first of all we will see how we can add validation points so now let us start with testing the status code so how we can validate the status code so the same function we have to write the syntax will not be changed but internally the assertion will be changed okay this is called assertion and uh these brackets you can put in the same line or multiple lines and the same syntax so this is syntax I have used okay let me just copy this okay so this is a syntax okay so this is a syntax which we have used and the same syntax you can write like this so this is a syntax which used okay now this function is started here and here we'll put a assertion and the function is closed and this is the closing of test this test is got closed here this function is got closed here so same thing we can write in the single line so how we can write in a single line so for example if you just look at the syntax okay so let's look at the syntax same thing so PM DOT test status code this is actually test name and uh bracket and arrow Mark and this curly brace you can keep in the same line and this is the assertion after completion of this assertion this curly brace is here and this closing bracket and semicolon is here so the same thing here I have split into multiple lines so that we can understand the syntax but we can also combine everything into single liner so here we can start this bracket you can start here and this bracket you can close here okay same syntax so now let us try to understand what exactly it is so PM dot tester test is a function from the PM library and here we need to put the test what kind of test it is so you can write some description about the test like I want to check the status code is 200 so I can just write some message here status code is 200 and arrow function and inside this we have to write the statement in the PM Library we have a method called response dot 2 dot have dot status so few keywords you have to remember it is in simple English language also also so PM dot response so what we need to test here 200 200 is a part of the response so PM dot response dot two dot have status and here what is the status we are expecting that we need to specify okay if you are expecting 200 is a status so we have to say 200 if you are expecting 2 0 1 is the status then you have to specify 2 0 1. and this description is a user defined you can write anything here in the double quotation that is just a name of the test or description of the test you can write but this is the actual important thing where we need to specify the expected value so now this will check the status is 200 or not once you send a request once you get a response and if the status is 200 you got the response then this particular function or this particular assertion will pass or else it will fail so this is a validation point which we need to add if you want to test the status code and there is one more approach is also there so for example uh suppose I will specify multiple status code let us say when I send my resp when I send my request I'll get some response sometimes the response may have 200 or 201 or two zero two zero two like if you want to test them so for that what you can do you can use one more operator called one off that means what suppose you are getting the response as a 201 status code and this assession will pass why because 201 is listed here and suppose you are getting the response as two zero two okay you are getting the response as two zero two and that also then also this will pass because you specify the two zero two so what are all possibilities are there get to get the status codes all possibilities we can specify in this list so what is the method here PM dot expect again so PM Dot dot code and this is the parameter we have to pass in the expect dot to be dot 1 of 2 0 1 2 0. so you no need to remember all these things guys so the postman is provided a documentation and in their documentation we can find all the syntaxes and all the methods they have given clear documentation just we need to follow the documentation so we no need to remember all the options okay so this is the way so PM DOT test this is a description okay Arrow function and this is the actual assertion and in this assertion will pass if the status code is 201 r202 anything but normally status code will be only one but if you don't know exact status code but if you know some status course and if you want to validate if the status code is part of that list or not then you can specify multiple things or else go with the first one okay so these are sessions are the same for all kinds of API requests according to your response we need to put different type of assertions but the same assertion same syntax we will follow across all the HTTP requests so this is a one another flavor of uh same thing here also we are testing the status quo but what is the difference between this one and this one here we are exactly testing the status code with one value one expected value but here we are expecting uh two different values either this one or this one of the thing will be matched and then this assertion will pass okay so this is a one thing and uh in the status code you can see something called some message whenever you are done some post request especially when you do the post request you can see something called uh created along with the status code you can also see something called created and if you want to validate this message we can also use the same thing like pm.response dot two dot have and Status okay so pm.response.2 dot have status and here we can specify that message and this is also part of your response this is also part of your response okay so this is a uh one more function which we have to test the status code okay to test the status code especially so now let us try to uh insert this functions in the response and check whether they are properly working or not okay so this is all about verifying the status course now let's go to postman and let me take this get request so currently uh in the test tab I don't have anything right so here we don't have anything so we need to add that let me take the student API test case because here we will value the proper response so let me start my student API so let's go to Json section CMD and whenever you start running your API we need to run this command Json iPhone server and then students dot Json okay now my API started running now I can just minimize it right so now let us go and add some validation points for get a single student so get single student and here already added something let me just remove this okay so now let me just uh run the request once okay get a single student when I send this request so you got some response here okay you got some response and here it is a status code 200 okay is my status code so now we need to evaluate the status code so whether 200 or not so what we can do we need to go to the test tab here so basically you are there in the parameters authorization and so many other tabs are there and you need to go to the test data so here we need to write that validation point so I want to check the status code is 200 or not so for that what you can do now we need to add the assertion validation pointer so PM DOT test and I'm checking status code is 200 this is my test description or test name Arrow function and pm.response.2 Dot have dot status is 200 we are expecting status is 200 this is a function and similarly I can also add something let's say suppose uh I want to check multiple status codes okay just like this so I am expecting this one or this one either one of them so in that calls also we can just add this function and we can add multiple things guys okay not only one we can add multiple assessions for one request go back and I'm also adding this so this is a get request right so I'll say get request and here we are expecting let's say let's keep 200 in the list and 201 also but most we will get only 200 so 200 will match to the 01 will not match okay so if you have multiple options multiple status code you can just specify in the list and if one of them should be matched and then it will execute and pass and this is also the right way of adding and uh suppose if you are sending any post request then you can see here created right so that also we can do it okay so let me just add these two now let me just run this and see how it is going to work now I'm just saving it as soon as you added these assessions just save the assertion save it and then let me run the one more time this is same EXA same assertion uh same request I am going to run one more time click on the send okay now observe this so you got the body response as usual and we got some cookies and headers but still we are not validating them now go to the test results we go to the test results currently you can see these are the two got passed status code is 200 success will get request both are got passed because we are expecting the same thing suppose let us change it so I'm expecting 201 for example now will it pass or fail definitely it will fail because actual status quo is 200 but we are we are expecting 2 0 1 here now again save and execute execute so now we can see my test is got failed so we are expecting two zero one but it is returning 200. so my test is got failed my validation point is got failed on status code so this is how we can just add assertions in this particular test Tab and then we can validate okay and this is for multiple requests and this is for single request now save it and execute one more time so now we got First Watch first one is got passed so this is for uh status code how we can verify the status code this is for post request especially when you send something so which will say created along with the 201 it will also show you some message called created if you want to verify you can just add this validation point so these are the three different type of functions which we can use to validate status codes okay so status course we can validate by adding these three functions so the next one is headers part so how we can verify the headers so if I just go to the postman so as soon as you send your request we got some headers also in the response you can see here so in the headers contains a lot of information and this is basically talking about so many things talking about data or network information server information so many things are there but we can't understand everything but our responsibilities we need to check every header is having proper value or not so mainly what we will check in the header part is called the content type this is the most important thing which we need to test content type or content length also we can check but content length is dynamic every time you will not get the same value so we don't need to test that but mainly content type you have to verify so what is the content type is representing what kind of response format sometimes the response will be in the Json format sometimes it will be XML sometimes it will be HTML different formats of response we will get so the content type is Json or not the response is in the Json format or not that we can verify so the content type is written in this one so this value we can validate this is a value we can validate and the connection type also we can validate and when it is expired whatever the things whatever headers we got here everything we can validate but there are some options which are dynamically changing which we cannot uh predict those values so we cannot add any validation points especially let's say catchy control okay and then data right so this will keep on changing and uh keep alive this is also keep on changing so there are certain things even content also keeps changing so these things we cannot validate but whatever the values which are static here so those things we can validate so we can say content type is there so content type is most important thing which we need to validate okay so this is a content type which we can validate if the content is in XML format then you will see applications as XML okay and if our content is in HTML format then you can see yeah application slash HTML something that is so this is basically called mine type okay which is basically called as a mime type m i m e mind type so the mime type is representing the type of response or type of data whether it's a Json or XML or HTML or text whatever it is so the content type should be application slash Json that we can validate so now let's go and see how we can put this validation on headers so check that response header is present or not so there are two valorations we can do it first thing is what when I send a request when you get the response first of all this header is present or not we got this content type header or not that's the first thing okay if you got this header the value of the header is correct or not so two things we can verify one is called the presence of header the second is what the value of header okay so the presence of header we can verify with one function the other function is what we can check the value of the particular header we can verify so for that we have a two functions so the first function is what content type header is present or not not only content type you can check any other header is present or not so the only thing is you need to change the value here you need to put the header name here so whatever we got the response in the header so like pragma expires content type content length e tag date so these are all different names of headers so we can put anything in this particular function so basically what this will check this will check this particular header is created for that response or not that's the main intention of putting this pointer the second thing is what okay so this Adder is present but what is the value of the setup that we need to verify so for that we need to write this function so content of header is application or Json we need to verify so this is Arrow function and the PM dot expect and pm.response.headers Dot get and here we have to specify content iPhone type this is a parameter this is the header we want to check content type and that should be equal to what application slash Json colon car set equal to UTF and this is our completely uh the value of content type so the complete value we have to specify so here we have to specify the what type of header we are verifying and here we have to specify the what is a value which should have we what is the value that header should have so that is the expected value we have to specify here so here expected header and here we have to specify the expected value for that particular header and this you can use for multiple headers also so if you want to verify other type of matters you need to write the same function multiple times suppose I want to check some other type of header so let's say I want to check content length so then what you need to do here we need to specify the content length and here you have to specify the value you are expecting for the content length right so that will verify the header part so these are the two functions are there to verify the headers content type and content type value okay now let's put these two assertions in the postman console and then we'll see so we already add a status quo now I'm going to add two more functions to verify the header presence and the value of the header two things let me put here okay yeah so this is for header presence and this is for value of the header now save it as soon as you add something here you have to save it and then send the request one more time click on set okay so now okay just look at here test results there are three validation points we kept here so if we just go to test results and you can see status code content type and content type header is correct or not okay so this is how we can validate the headers the header header presence and header uh value so you can change here what kind of error suppose here a Content length is there so I put constantly suppose if you want to Value something else suppose I want to validate content length so I can say content length and value of the content length is 126 that you can specify here in the single quotations and that will validate the another header and another header value okay so these are the two functions which are available for testing the Header information now let's move on to the next one cookies and how to test the cookies and sometimes the cookies values keep on changing and they are more dynamic because whenever you send request you may not expect the same values for the cookies but there are some cookies their values are same sometimes they will get the same value for the cookie and those type of cookies we can validate them so to validate the cookies if I just go to the postman here once you send your request you can see one more tab under this and the response called cookies So currently for this particular request there are two copies are created okay there are two cookies are created so each cookie is having some name some value domain path expires HTP and secure some details will be there for that particular cookie so what we need to verify here is we need to verify the cookie and their value so we need to verify the cookie and their value suppose the cookie name is what currency and what is that value of this is USD that you can verify and what is the cookie name here is language the value of this language cookie is what e and iPhone GB so this I can verify so if you want to verify them what you can do is we can just put the function like this so PM DOT test and currently this cookie is having some language right this is the cookie and this is the value now I'm going to validate this so for that what you can use we can just write some description here I'm just checking whether cookie language is present or not so I'm just writing some description here Arrow function and the assertion is what PM dot expect bm.cookies dot has language so what this function will do now it will check in the cookies the language cookie is present or not there are so many cookies but the language cookie is present or not so that it will verify it should be true so the PM dot cookies has a language to be true it should be true and if it is present then this validation will pass if it is not present then validation will fail so this is just checking for cookie present or not but if you want to very read the cookie then we have to specify the another function PM dot expect pm.cookies.get and first we need to get this cookie and the value should be compared with this so dot equal en iPhone GB so this is a session point cookies.get and this is a cookie name and this is a value so by using this syntax you can validate any other cookies just we need to pass the cookie name here and cookie value here and if you want to add it multiple cookies you have to write multiple functions for every cookie you have to write a different function okay so this is how we need to verify the cookie so these are the two different methods are there one is what presence of cookie the second one is what the value of the cookie same thing we have done for header also first we have verified header presence and then header value similarly we can verify cookie presence and cookie value now let us add these two points also to our request now go to the test Tab and now I'm adding the validations for cookies cookie present and cookie value let me add it here yeah so this is for language cookies present or not and the second is for language cookies having some value or not en iPhone GB or not we can verify so now save it and then send the request okay so now I can go to the results totally five validation points we kept so the last two are related to cookies they have got passed okay so for example I'm expecting something else let's say e n underscore GB and just put some only en for example and now save and execute so this should fail because we are expecting is different but actually is different so this validation Point works like that so we can verify the presence of cookie and then we can verify the value of the cookie and if you want to validate multiple cookies you have to keep multiple function points okay so the next one is what testing response time okay so testing the response time so how we can verify the testing response time so you can just look at this request so this is the response time you can see uh 44 milliseconds are there okay so 44 milliseconds are there but this time we cannot predict exact time okay it depends on the net wall speed depends on complexity of data this time keep on changing so we cannot exactly specify the same time so if suppose here this time it is returning the response within 44 milliseconds but next time when you run this it may take 50 seconds or sometimes it will take 60 seconds so that value is not constant but what we can verify here is we can put some time and if the response is getting below the time we can make our test passed okay we can put some Maximum timeout and if your response is getting let's say currently it is returning 44 milliseconds so I can put something validation Point approximately so if my got response within 50 seconds then I can make my test pass if the response is getting more than 50 milliseconds then I can make my test is faster so like this you can put some approximate time but you cannot specify exact time because this will keeps changing okay so how we can add the status time is like this suppose response time is less than 200 milliseconds for example I put maximum point is 200 milliseconds then how we can specify PM dot expect pm.response.responsetime dot to be below 200. okay below 200 so if the response time is below 200 then this will pass and if it is taking more than 200 milliseconds then it will fail so this is the time guys don't think this is a status code okay this is a Time that is up to you how much time you will put okay so let me just add this also response time so currently we are getting 44 milliseconds approximately let me run one more time and see now this time it's getting 42 milliseconds right so let me just add this point uh assertion Point here so 42 44 it is taken uh for now I'm just adding 50 here let us say 50. description also I'm just changing let's say 50 milliseconds now save and then execute yeah so now we got response in 37 milliseconds so it is got passed let me run one more time so this time we got a 51 see here this time you got 51 but what we specified only 50 so it's got failed here so expected is 51 is 50 is maximum time you specify but it is giving 51 milliseconds so it got failed up so approximately we can specify Max 100 is fine 100 milliseconds means uh one second so we can just say Max 100 or 200 whatever you can just keep it here so this is how we can verify the response time okay response time so here uh range is not possible so we can just specify below or I think we have one more function is also there uh we have something called above is also there okay either you can use below or above so these functions you can use below or above you can use okay so this is a one more function which we can use for testing response time response time so what are the things so far we covered so we have seen how to verify the status code and then headers cookies and then response now what is the next thing is response body this is a very very crucial thing and very important also so for rest of the items we can directly keep the functions directly we can copy paste this function so almost every APA request you have the same stuff okay so you can just copy paste this but the response we have to test so response will be different responsibility will be the different from one API request to another APA request so now we need to validate the response body that is the next item all right so now we'll see how we can validate the responsibility so this is most important item which we need to focus on okay so uh we will get the response based on the request so what kind of request we are sending accordingly we will get the proper response right so this keeps changing the response is completely depends on what kind of request we have centered right so now to validate the response body so we have different type of options available so we have a different type of options are available and especially Json path will play very important role while performing the while while testing the response Json path yesterday we have discussed right if you want to locate a specific value or specific node in the Json response we have to use the Json path so that will play very important role while verifying the response body now let us see how we can validate the response body in the response so now let's go to the section yeah so the first thing the first very thing asserting a value type asserting a value type this is a first validation on the response so what is that mean value type is nothing but water for example here this is my response Json response okay in this response I have a multiple Fields here this ID is one field name is another field location is another field phone courses these are the fields and every field is having corresponding value here so every field is having corresponding value now what we need to verify here the first validation is what the type of the value for example ID is what integer type name is what a string type okay and what is the type of the courses array type so if you want to validate the type of the field or type of data then we can add a session like this so whether the ID is number or not whether the name is a string or not are we getting the string value or not here and are you getting the array or not here so that we can verify faster so to verify that we have to write like this multiple assets we can write for multiple field field level validations so we can write like this so first of all we have to write the statement which will capture the response so whatever the response we got in the body we have to capture the entire response into some variable so here Json data is a variable and the type of the variable is constant or you can say where Let There are multiple keywords in JavaScript you can use one of the keyword and this is the kind of Declaration in Java we will specify integer float double string right similarly in the JavaScript we have a different types you can say constant or you can say where you can say later multiple keywords are there you can use one of them and this is just for variable declaration Json data is just a variable so now what this statement will do is PM dot response dot Json so what this will do is from the PM Library whatever response we got that response will be copied into this variable the Json response whatever you got the response the entire response will be response body will be copied into this Json data variable that's the first step you have to do it for body verification First Step you have to do this after that we can put any number of validation points the first and most important thing is we have to capture the entire response into the variable and then we have to write an actual function so PM DOT test and if you want you can write the statement inside the function also as a first statement inside the function as a first statement you can write this statement no problem but this will be used in multiple functions later so later we will create multiple functions like this in every functions we need Json data so that's the reason I have defined this outside of the function when I create this variable outside of the function that is called as a global variable so that I can access that variable in all other functions so that's the reason I have defined this variable outside of the function when you define this in the inside of the function we cannot access this in another functions that's the reason I make it to created this outside of the function okay now what is the validation you want to do is we have to verify the type of data so whether ID is number or not whether name is string or not location is string or not phone is string or not course this is array or not that we need to verify so for that we can put a validation Point like this PM DOT test and this is a description about the test test data type of the response and arrow function and inside this PM dot expect and whatever Json data we captured in this variable we have to specify as a parameter PM dot expert Json data dot to B we have to use 2B function 2B and object to be an object what is this validation point this entire Json is an object okay the entire Json is one object right the entire Json is one object so whether it is written proper Json object or not that we can verify by using this point PM dot expect a Json data dot to be an object the entire Json is an object yes if it is an object then this point will pass we are verifying the entire Json as a one single object there is a first validation point now the second validation point now let us take a name so the name value is a string or not we can verify so how we can put that PM dot expect and from the Json object we have to extract the name field right how can access this name field by writing Json path how we can add the Json path Json data whatever data we capture from that dot name dot name field we are accessing and that name to name field to be a string and these are the Json data types yesterday we have discussed string number array object null so these are all Json data types so this name should be string type we are expecting the next thing ID so PM dot expert Json data.id.2b a number this should be a number we are expecting this should be a number and the courses the courses is basically an array type which contains multiple values so I can put validation like this PM dot expert Json data.courses dot 2B and array so like this for every field whatever is there in your Json response or Json data we can verify the data type of that particular field is correctly giving or not and this is a validation of the type so test the type of any part of the response assetting the value type so what is the value type is we got in the response that we are going to verify okay so this is the one thing so there are so many other things are there so let me just finish that okay don't ask the questions till I completed this so just only first one I have completed response body still we have to do a lot of other validations on the response body this is just the first validation we have to do here okay so data type validation we have done in this every field or having some value so that value is correctly coming as per the data type is not that we have to verify so let us copy this and check or in the response so this is the response I got so in the response we can verify name location phone courses everything so let me put this validation point in the tested app and another validation point I have added so first we need to capture the Json data into the variable and then we added all these points okay now save it and then send it okay so now we got it so this is the way we can uh go go to the test results and we got the output like this so this is violated so for example uh ID is a number okay and the name is a string and Json data is an object the courses is array so let us say ID is a number by default here let me just uh expecting as a string then what happens we will see I'm expecting ID value is a string formatter so now save and execute so now the assertion has got failed so why this failures because we are expecting a string we are expecting a string but actually is what integer so that has got failed so according to our expectation it is working or not the type of data is correct or not we can verify this so this is the first validation point on response even though if you have a multiple recurs no problem at all you can write one of the field because if you have a multiple records how it will be the fields will not be changed right the fields will not be changed so for example uh let us say this is your response foreign okay so in the first record name is a Stringer even second record also obviously it should be string it will not change so you can verify any one field like that this one or this one anything is fine you can verify any one field no need to put no need to put validation for every name field just try to put one validation on one field that can be from first recorder that can be for another record it is still fine okay it is still fine okay so remember that so accordingly you can put so even though if you have a multiple records no problem because in every record ID value is one that is an integer in every record name is string we never record four number every record course this is array that will not change anyway whatever data type which we got in the first record same thing will be repeated for rest of the records also you will not see any difference okay so if you have a multiple records also you don't have any problem so you can happily specify the data type okay sorry guys so that is a number not an integer all right so now save it then execute so now my session will pass okay so this is a the first validation on type of data so whether we are getting the right type or not object or not string or not number or not array or not we can verify and if you have multiple fields we have to put multiple points in one single function no need to write multiple functions for multiple Fields here you can put all fields in one single function okay now the next one so the next thing is asserting era properties yes properties so what exactly it is asserting array properties is nothing but so for example let us say I have an array variable in the body so this array contains some value here okay now I want to verify this array contains these values or not okay this array contains these values exactly the same values or not I want to verify okay so for that what you can verify the test array properties and this validation is only for array type of data if you have an array inside the Json so we can verify the array elements or array values so how we can verify the first point PM dot expert Json data.courses what is this courses courses is a array courses is a array and if you want to validate the single value in Array which is present or not see simply you can say two dot include okay if you want to just check Java is a part of the courses or not all claim is a part of the courses or not if you want to verify this you can just put this function two dot include specify one item and here it will allow us to put only one item when I use include method or include function so PM dot expect Json data.courses what is this Json data again the same variable which we created here so this we have to create before the fund because I created this as a global variable I can use this Json data in every function so I here I have not created again so same Json data I'm using here Json data.courses dot include Java so this will pass if it contains a Java and similarly you can specify another item here if this item is part of this course arrays then it will pass if it is not here then it will fail there's a first validation Point include or not we can check and suppose we want to check multiple options suppose multiple values should be included or not and these are the part of this array or not then you can write another Point PM dot expert Json data.courses dot to have dot members and here we can Define the same array which contains the values [Music] okay and these values no need to be in order you can write in whichever order you want the order is not important here but the values the content is important so this array contains the Java and selenium and here also we are validating whether the array contains a Java or selenium okay so let us try to add this validation and then check it go to the postman [Music] and now just copying the same function so here test era contents condense in body so now save this and then execute okay so now it got passed the last one is got pass array properties so for example uh here I'm expecting something okay let's say C plus plus actually which is not part of your array so definitely it will fail yeah now it has got failed so this is how we can verify the era values in your response if you have any array if you want to verify the contents in Array then this is a way of validating this okay so why two times used this is for single value validation this is the multiple values validation that is option if you want to remove this you can just remove this no problem because this is already covered here right so if you want to validate a single value then you can go with this point or this assertion or if you want to validate multiple values then go with this one that is up to you which one you have to use okay so that's the reason I covered both the things depends on your requirement you can choose one of them right so if you have one value in Array you can just go with them and if you want to test multiple values presence in Array then you can go with the second so both the points I have put in the same assertion okay now this is a one more type of validation inside the body so we have verified uh in arrays data is correctly coming or not by putting these validation okay so the next one is Json Fields validating Json fields in response this is another important thing guys okay so this is another thing so validating Json fields in response so this is my same response body now this time I want to verify the values of every field okay so let's say name is John or not location is India or not courses are Java slim or not I want to verify and here even if you have a multiple records you can still verify name location phone right because if you have a multiple records the name value will be different location will be different phone will be different courses can be different right if you have a multiple records so in those cases what you can do is you have to specify the Json path so by properly writing the proper Json path you can verify it suppose currently I have only one record here so I can directly say Json data ID Json data name location 4 and so on but if you have another record then you have to write a proper Json path here and then that will locate the element and then check the value here these are all Json path ID dot name location and so on four number so let's see the how we can write this validation so value of location field is India not only location I have verified other things also here and just look at look at this so the name value is John location is India the sum four number and courses is there so how we can verify this data is we can first capture the entire response into Json data so again here I have defined this as a local variable okay this is the local where we can just put this in outside of the function also just I have done some time back here okay so here I have done right outside of the function I have different because the same Json data variable I can use in multiple functions so if you define within the function you can use that for only within a function but here I have created one more statement within the function I created but this is not required if you already Define this variable for other thing again you don't need to Define this okay so we got the Json data here and now we need to verify the data of every field so now we can say PM dot expect Json data dot ID to equal 1. that should be 1. and the Json data name should be equal to John Json data location should equal to India Json right data phone to equal to something and similarly courses now you can see there are two verses are there so in the courses we have to access the first element Java and I can say courses of zero courses of one two different elements we have to put two different validations okay and when you put multiple validations in this even one is got failed okay even one validation has got failed the entire function will fail the entire status will fail that status will show as a fail okay so remember that point if you have multiple points in the same function while executing this function if something is got failed even one is got failed the whole function will show as a failure case okay so let us see how we can validate the data in the response so that is the next thing just a moment okay so now let me just go to postman and then I'm adding this pointer foreign so values of fields in response all right so now save it and then execute so now it is got past all values are perfectly come so let's say the ID I'm giving different value let's say five ID equal to 5 so execute and then obviously it will fail so even if one statement is got failed rest of them also it will show you the entire function is one validation will be consider as one validation so it will definitely fail so I can say here one yes okay so it has got passed so if you have a multiple records again you have to write a proper Json path and yesterday I have told you right how to generate the Json path how to generate the Json path so accordingly you have to write a proper Json path whichever field you want to variate in that Json you can get the Json path of that field if even if you have a multiple fields or multiple records in the even if you have a very complex Json you don't need any problem there so you can just get the XR Json path of that element and specify the Json path here here because Json data is a variable which is already captured the entire response and from the Json data dot here you have to specify the Json path then it will point the exact element and then we can check the data of that particular field or element and here Json path is very very important so sometimes it will be difficult to write the Json path manually so we will take the help of the tools and we can generate a Json path and whatever field we required so accordingly we can specify the value of the variable so now save it and send this now again it is got passed so this is how we can verify the data of that particular field so two validations we have done so far so three validations one is what we have verified the type of data data type of data we verified second thing is what if we have an array in the Json response we verify the contents of an array and the third thing is what verified the values of the fields means basically we are verifying the data is correct or not this is called Data validation field level data validation and then the next one is what we have to finally verify the schema validation which is basically called as a Json schema validation that's the last one validating Json schema so what is Json schema that we need to understand first Json schema so what is Json schema so schema is nothing but water the structure okay for example if you want to understand very clearly let me just explain with the table okay let me explain with the table so what is the schema means in the database concept also we will use this term very frequently the schema table schema like that similarly here we have a Json schema so let me just Express something here let's say I have a table like this first let us try to understand with the table okay in the table we have some Fields let's say employee ID employee name and the salary is there so this is my table and here we have some records because second record and so on so different type of data we have so what is a schema means this is a table which contains the data also but what about the scheme mask what is the schema means schema says what kind of data we are storing into the table so schema means nothing nothing but this is about so for example how many columns we have Eid and E name and salary and Eid is what integer column e name is what a string column salary is what again integer column or number column so this is called as a schema we can simply say this is a schema or we can also call as a metadata means the data aboard the data means how what kind of data we are storing in the table who is deciding that schema is deciding that suppose if the E name is a string or Eid is an integer if Eid is defined as an integer can we store string here in the E minor Eid field we can we store string here no so what is the data type we specified in the schema accordingly we have to specify the data in the table right we have to follow the schema and if the schema is a different We cannot put different type of data type whatever the data type is specified in the schema and the same data type we can store in the table this is a concept of schema similarly Json is also having as kind of a schema concept so what is the Json schema means for example here we have this particular response okay this is our response so this is our response so for this partner's response what could be the schema the schema means in schema what it is having so let's say ID is there so ID should be number and the name should be a name should be John that is a string location should be string four numbers should be string courses should be array right that is the schema actually so if you just look at the Json scheme of this particular response this is your Json scheme I can just look at this okay so the entire Json response is an object so it is an object and what are the properties are there in this ID is one property name is one property location is one property phone is one property courses is another property so every property there is a value associated with this that value data type we will have here see here what is the type ID is there so ID is what is the type of ID integer what is the type of name String what is the type of location string what is the type of phone which is also string and what's the type of courses which is array but inside the array there are multiple values are there like type string the second one is also type string okay and ID is mandatory required mandatory field so which is required field again name location phone courses all of them are required fields we cannot uh skip any one of the field so this is the schema of particular response so this is called Json schema according to the schema we will able to store the data in the Json or else this Json will not accept any other type of data so it will internally follow the same schema just now I have told you table concept right so what are the data types which we have given to the table columns same data we can store in the table similarly and whatever the type is defined in the Json schema the same type of data we can store in the Json response or in the Json data but how we will get the Json schema again we have a different tools are available so we can easily generate the Json schema so if I just go to the Google and search for it I'll tell you how to generate the Json schema so I can just type generate Json schema okay just search for Generate jsons schema and you can find so many website you can just go with this Json schema tool foreign you can use any website no problem okay so here we can just paste your Json which will automatically create your Json schema I think this is something different let me just take another one okay free online Json to Json schema converter so let us use this option okay so now here you can just copy your Json data that will automatically give you the Json schema so let me just go to the body and give this Json data and search for it here just paste your response whatever you got and for this response so we get the Json schema so just select this and click on generate schema so once you click on this generate schema so then it will automatically generated Json schema so this is a schema according to this schema the data we can store in the Json file okay so this is a connection between Json file and Json a schema so guys I have understood now what is schema means which is very very important schema so what is schema means schema is describing the where or the type of data which we can store in the Json file and whatever the type we specified in the Json Json schema at the same type of data we can store in the Json file okay so now why it is important why we are discussing about Json schema because we have to validate whether our response is following our schema or not so whatever the data we have in the response which is there according to our Json schema or not that we need to verify so that is a final verification so the Json schema verification we have to do this is very very important so whatever the response we got and not only one record guys even if you have more number of Records schema will not be changed schema will not be changed okay same schema even if you have a multiple request it will not change okay so how how it is possible because same example again if you take the table okay so let's say my table schema is what I have the employee ID employee name and employee salary employee IDs integer which is number employee name is a string employee salary is again number or integer this is my schema so when you add multiple records in the table will it change the data type or not even first record ID is integer only first record e name is string first record salary is number same second record also follow the same thing third record is also follow the same thing so the data whatever we have that will not change the schema okay this will not impact anything on the schema so even if you have one record or two records or 100 records you can still verify schema of that particular response okay so remember this point so now we will verify the schema of the response so how we can verify the Json schema so to do this we have something called okay so we have something called here we already converted into Json schema right so that we required so this is the actual response by using this response we are able to generate your Json schema right now we need to validate whether this response is there according to the Json scheme or not so for that what you can do we need to write this uh method so PM DOT test and this is the test name description and function and here you can write Arrow function also I have written normal function you can also write Arrow function no problem and PM dot expect tv4 what is this db4 so TV is nothing but a tiny validator so this is again one more Library which is there in the postman okay Postman it is there TV is nothing but a tiny validator version number four validator version number four this is one of the library just like a PM tv4 is one of the library in the post so tv4 dot validate and here we will pass two parameters one is what Json data so whatever data we have got in the response right the data we have to pass this is a one this is a response data and the second one is what and again what is the Json data this variable we have to create so you already created that variable earlier right so the same variable you have to refer this one if you create this variable outside of the function we can just refer this Json data or else you can create that variable again right so that variable we have to specify and schema and whatever schema we capture we have to create one more variable called schema and the entire schema uh Json schema we have to copy it in the test window and according to this schema we have to validate the Json data so two parameters we have to pass DOT to be true means what this Json data should follow the schema that should be true we are expecting true here so this particular method or function will validate Json schema okay now we'll see practically how we can do this go back to the postman and now the first and most important thing is what Json data variable we already created here the same variable I'm going to use so we can just look at this okay uh yeah so here we created one time and the same variable we can refer multiple places so the same variable I referred here in this function and also uh here in this function also I have referred the same variable Json data and now here we don't need actually okay because we already create as a global variable here you don't need same Json data we are using it here and now also we do the same thing but before that we have to capture the Json schema right so here Json schema validation Json schema validation okay the first and most important thing is we have to uh capture the Json schema so how we will capture by using any tool so just copy this entire thing and keep that in a variable so let me just go back to the postman and here I'm creating a variable and in the JavaScript you can create variable by using via keyword where scheme equal to and here we need to paste the entire schema whatever you have captured so we need to put everything inside the postman via schema equal to tab so we can just look at this so where schema is available and the entire Json schema I captured here I put everything here and now we need to start the validation okay so we need to start the validation what is the validation we have to start is PM DOT test so that is a function which we need to specify okay so PM DOT test schema is valid or not we will test function we can write normal function or Arrow function if you are writing Arrow function how we can write remove this keyword function and just say error this is becomes the arrow function or if you want to write a normal function you can just write a function keyboard anything is corrected so PM dot expect tv4.vary date and Json data is a variable which is already created above now this schema variable we just now we created which contains all the Json schema now come down and here we are expecting true so this is a schema schema validation now save it and then execute okay so now all validations are got passed all valid schema is also valid okay so for example what I'll do is I'll say as per schema okay as per schema I am expecting right I am expecting type ID is a string okay let's say I'm expecting ID is a string ID should be in the string format as per my expectation as per the schema okay but once you get the response we got in the number formatter right so now let us save and execute definitely it should fail now see this the ninth the last one schema is got failed so why because the response is not as per the schema which is defined so the response should be uh same format according to the schema definition so it should not be different so then it will pass so this is how we need to validate the schema so again let me change it back and in Json we have a number and integer both the data types are there so we can use alternatively integer or number so when I execute this now this will validate the schema also so schema validation is one of the important thing which we need to validate as part of the response okay so this is the last one so Json schema validation so whatever type of response you got okay how much response you got how much complex Json it is it doesn't matter so the same validation points you need to apply and moreover you should be more familiar with the Json path so if you know the Json path properly you can exactly verify the fields properly okay so these are all different things which we need to validate as part of the response okay so for every type of request we have to add these validation points so now what I will do is I will add all this validation points for the rest of the request so currently I have added all these validations for single request get single student so let me add all of them for other requests also so I already added let me show you so let's go to the next one get all students data so now go to the test Tab and here I have added the same thing almost status code status type status code header type cookies response and so on and same thing for create a new student also for creating student also new student also I have added test here 201 I'm expecting here and then update student and then delete student so the validation points which we need to add for every HTTP request and especially for get request you will have more number of validations because in the get request we will get the response body also so we need to verify the response body and along with the data schema and everything so when you send a get request we have to add more number of validation points but rest of the request mainly we will test the status code and uh then we will test the headers cookies time and so on so they are enough for rest of the things but especially when you send get request we will also do the data validation and response data validation and we need to add more number of assertions okay so now I have added all the validations for every request then what I will do is I just want to run this entire collection in one shot instead of running each individual HTTP request I just want to run the entire collection so how we can run it yesterday I have shown you so click on these three starts okay so click on the three dots and here you can see something called run collection so click on the Run collection now it will show you all the collections and whichever collection you want to run you can select or unselected okay it depends on your choice all the collection HTTP request will be shown in this collection and then iteration only one round you want execute and delay time you can make it a zero or if you want to put some delay time between these Collections and the time of execution you can increase this RS leave it and then click on Run students API so all requests will be executed one after another and almost all validation points are got passed okay so everything is got passed so this is how we need to add validations to the request and then we will able to execute okay I will share this document guys just keep it with you and the same process will be done for any kind of apis all kinds of apis the same kind of validations we do status quo headers cookies response time and response body and especially get request we will focus more on responsibility and we need to do schema validation data validation these are the two important things we need to validate okay so this is all about validations response validations now in the tomorrow session we will discuss a few more topics now I'll stop for today here okay