#3: Cypress API Automation - POST Call [2021 latest]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is navini welcome back to veen automation labs and back to our cypress api automation series so in this video we are going to talk about how to use post call how to create a specific resource or let's see example create a user so there are multiple things we i'm going to teach you something very interesting but let's see first which api i'm going to use it same thing we will go to gores.com in the previous video we have already seen how to use get user api today we will see this api post user api it means create a new user with the post call if you really want to hit this api you can hit it in your postman and you can cross check before using it in cyprus so this is my post call and as a header i'm passing my authorization you can generate your authentication token as i already told you in the last video and then this is the json body that you have to pass it from here so these are the four attributes for example hcm passing i'm passing some random email id for example and then click on next so here you will see that response is coming over here status code is 201 and this is absolutely working fine right if you really want to generate your token as i already told you just simple logout and simple hit this api gohan's dot co dot n go to login and then you can log in with your google account facebook or getup i'm going to log in with getup or anything that's up to you and then it will give you this access token copy this access token and then when you pass as a header you have to pass along with the bearer keyword because this is a beer token bearer space and whatever the token is there see this is the new token and then you send it and then you will start getting the response although this email is already taken from the body if you change it let's see again cue and then send it again you will get the response now exactly same thing we have to do with the help of cyprus now in cyprus there are multiple ways we can do that first of all you can use the hard coded json payload in a script you can use a random email id because i don't want to change my email id again and again just like manually i'm doing it right now so i will create one random function which will generate one email string randomly and then i'll use in my payload second thing is that you can read the json payload from the cyprus fixture and use it in your request body and then there are two ways you can use as a require a constant also you can use it or you can use c by dot fixture also you can do that so with the help of callback function so we will see these three four options and very interesting very easy to know so let's open my visual studio code now let's write the script now so i'll do one thing this is under api test i'm going to create one file and let's see this time i'm going to create this is my post user dot js and then first of all for all the suggestions for all the by default suggestion from the cyprus i'm going to use this reference type equal to cyprus and then i'm going to use a describe here and then the describe let's see i'm going to create this is my post user request and then put a comma and then i'm going to write my callback function like this okay and then the bracket close then i'm going to write my first it block here and my first it block i simply create my first test function this is my let's see create user test function and then i'm going to write my cy dot my first request with the request method before that i'll do one thing i'm going to create one xs let's see x is a token a variable i'm going to create which is equal to what which is equal to whatever the token that we have generated so i'll do one thing let me regenerate one axis token it's supposed to be expired so let me just copy this and paste it over here after that cy.request method that i'm going to use under cy.request method i'll start writing my request in my first request i'm going to write what the first request is that what kind of method it is so i simply say this is my everything it is smaller method post but post should be in capital it could be get call post calls and we are using post here but this attribute you have to use a small letter so i'm going to create one request object with the url is what the url is in single quote whatever the url that we were using so copy the same url here and uh this is my url put a comma and then after that along with the url do you have any headers yes i have the headers also and headers colon and start your headers so we have only authorization so i can simply use authorization so be error whatever the access token that you are writing it x is token that i'm going to use it here perfect so this is my one header i have after that body so i simply write my body here and the body will be ended here now here the body is what the json payload so whatever json payload that you were using it here four attributes in the json object and then paste it here i i'll do one thing let me just this username is already taken so i simply write cy testing at the gmail.com i'm going to use it so my body is ready after that your body is uh your cy should be added somewhere so let me just end my cyu request object that is going to close it here and then i'm going to use a then then what i'm going to store in a particular variable let's say response variable and then callback function like that and this then object is getting ended over here now the response will be stored in this particular respon response variable and then from the response variable i can write multiple assertions so i'm going to use one expect method and an expect method i simply write from the response you go to the body and from the body you what you want you go to data right like this see so in the response payload i have to go to from data to id name email id right this is what i want so i'll go back here it means from the data dot there is one method here that is called has method and then dot property method here i'm going to use and then which property that you want to verify i want to verify that this email property first of all that email is correct or not which email the same email see by test gmail.com so i'm going to use the same email over here okay so this is my first assertion that i have written similarly you can write multiple assertions for example let's see uh what is a name the name is what okay so i'm going to uratus and status is active small letter you want to check the gender also let's check the gender as well perfect so this is what we have written and along with that i can write one assertion for the expect response dot i can check the status also should be equal to two zero one you can see in the postman also the status will be two zero one two zero one means the data got created so here you will be getting two zero one instead of two hundred so i'll be writing the status code should be two 201 along with these assertions i'm going to verify perfect so i think this is all we have written the request is ready now right now we are just doing the hard coded values and let's run it and let's see it is working or not so i'm going to do what i'm start my cyprus so i'll open my cypress npx cypress open and after that we will try to run it so the same script should be visible in your runner also you can see that just close this post user.js under apa test just click on it and let's see it is working or not and then open it again awesome see i'll show you once again okay just open your cypress stop it again and then run it just click on this post posture.js and this time is giving you error why because the email id is already captured so it's saying unprocessable entity or something like that we are getting it okay because the same email id it's saying user has already taken with the field email so we have to change the email id so i'll do one thing let's go to the script and uh simple right let's see one one here and the same thing you have to update over here as well now you check it save it and then automatically it will be updated and all your test cases are absolutely assertions are absolutely working fine and the create user test also got passed over here now here if you really want to print whatever the json response we are getting just like in postman right this is the response we are getting if you really want to print in cyprus how will you do that so you can simply write one method over here that cy dot log method okay and then if you try to print the cy.log method if you try to print this particular response so what will happen see this carefully let me just change it to one two otherwise it will give you error once again and then save it so skid will work fine no issues with that you just open this and then see it's printing the object value object 9 this is we want a json object but it's giving you object 9 here so i'll do one thing in this case what you have to do you have to slightly update in your string i mean in your script what you have to do you simply write json object dot there is a method that is called a string five method c string five method and uh this is string phi you pass the response object here and then it will convert the exact string into into a proper json string and then it will print it on the console so let me just update the email id once again to one three and then save it and then let's see see can you see that cy.log the consoles will be printed over here see the complete json payload is getting printed here this is so awesome like that right along with all the searches so this is a very basic script that we have written right now you must be thinking that every time i have to change my email id this is obviously not a good practice so what exactly i can do is i can generate one random string for the email id so how will you generate that so there are various ways of doing it for example let's say i'm going to create one simple variable here let's see let variable and my variable name is first of all let's see a text that or i simple write let's see some random text string that i'm going to create and then i'm going to create one test email also which is initially blank so these are the two a describe label sweet level variable that i have created now i'm going to use this variable in my for example let's see in my it block so i'll do one thing i'll simply come over here i'm going to create one random method so i simple side says okay fine a variable and one random string that i'm going to create so let's see one all let's see pattern string is equal to i want to generate from some random string with the help of this pattern i just going to paste it over here like that okay and then i'm going to create a for loop what i simply say that whatever the text that you have written this random text that i want to generate so i simply write random text plus is equal to what is equal to whatever the pattern that you are using it dot character at and then i'm going to use this function okay math dot floor math dot random pattern you want to generate up to what up to pattern dot so up to pattern.length i'll give you the string don't worry about that okay and then i'm going to use my test email so whatever the test email that you are writing it it means this is my test email is what whatever the random text that you are generating okay plus and then append with what append with at the rate gmail.com so my test email is ready and the same test email i can use it in my script now so instead of this hard coded value first of all i'm going to remove this hard coded value and reuse this test email here when you use this test email the same thing i'm going to verify over here as well so just simple remove this and then this is what i'm expecting and rest of things are still hard coded so now you don't need to change your email id again and again so let's see it is working or not so awesome see it is absolutely working fine and you can see the test body and this time test body see this is a random email it will generate can you see that every time it will generate a new random email every time the moment you pass it run it again so let's run it again and let's see let me just stop it and let's run it and next time it will generate a new email id and then the new email id will be sent now see this is a new email id got sent here perfect so this is the first thing we have we have to fix and we have already fixed now now what else still this is the hardwood value test automation and all such things so what we can do is now we can use the concept of fixtures i'll go to a fixture and you can see in this fixture example.json file is already there in this example.json you can update the same json or you can create a new json also for example let's say i'm going to create a new file here create user dot json file that i have used and then in this particular this this is the exact payload i'm going to use it here this is what we have to pass perfect so this is the format this is the payload format i have defined the template i have written name gender email id and status now value i'm not right now i'm not bother about it for email name gender status i'll be passing it from here so i'll do one thing i'll go to my post user and see there are two ways of doing it first way is that you simply come here and create one constant here constant let's see this is your data json which is equal to what which is equal to require and require what you have to give the path of your create user.json so how will you do that you simply write go to my project directory from the project directory go to where go to cypress so you can write dot dot format you can write a relative x path here and then go to fixtures folder so see this is my fixtures folder and then what is a json file name so json file name is create user you simply write create user no need to write dot json and now this data json you can use it in your okay over here to remove this in your body to remove the hard coded value simply write data json dot see i can access all the values now for name what do you want i want data json.name perfect mail is coming from where data json dot see this property that is actually gender property dot gender you can see gender is mail email id and status so i can simply write that okay fine email id i don't want to get the email id from data.json because email id i'm using from the from here from the random method and then instead of active i'll be using data json.status should be active and that's it and if you really want to verify you can verify with the same thing over here whatever you are passing the same thing i'm expecting so this name should be equal to what this name should be equal to data json.name and this is data json.gender very simple okay now that's it simple save it and let's see it is working or not post user let's run it again awesome see absolutely working fine once again and you can see that this time the random email id is coming but other data whatever the data that you are passing other data is actually coming from from payload.json c test automation i'm using it here and status gender is equal to mail status is equal to active now let's change the data just to cross check that okay it is actually picking the data from create user or not so i simply write test automation lapse and instead of mail i'll be writing a female and instead of active let it be active only and then simple save it and see the test will be picked automatically here you just come here and cross check the data you can see test automation laps this time gender is equal to female and the random email id superb right so this is absolutely working fine now there is one more way if you don't want to use this line here that require this is the best way of you using it you can use your fixtures directly with the help of cye object so what you can do is you can simply come over here before the c by dot request you simply write c by dot fixture there is a method this fixture method so what in this fixture method you can do it in the fixture method you can pass that okay yeah put a bracket here and then you tell what is your payload name so my payload file name is create user no need to write dot json will pick automatically and then what you do you create a callback function with the then and then store in a variable for example let's say this is a payload variable that you have taken perfect this is my payload variable that i have taken and then after that start your fixture body from here and then this entire request you have to write inside the fixture body like this so let me just close this fixture also here okay like this simple save it then what we can do is this payload what will happen whatever the data that you are using it from the create user.json it will transfer the data to this payload object now this payload object you can use it instead of data.json perfect so i simply write see this uh this is my payload data dot gender this is email id i'm not going to touch it because this is random and then dot status same thing over here a dot name dot status and dot gender and simple save it and let's see your script is working fine or not see absolutely working fine i'll show you once again let me just stop it and run it again awesome see this time again test automation labs is going to pick and gender is equal to female state is equal to active and the random email id so this is another way of using it you can directly create one cy dot fixture and then at the runtime store in this particular payload object and the same period object you use it in your body and use it in your assertions also so this is my actual result and this is my expected result over here or as i told you you can create one constant data also and then this data also you can store from this particular fixture all the values will be stored over here and this constant data also you can use it so this is how we write the script guys very very simple now in the next video i'll tell you you have to verify whatever the data got created with the helper post you have to get the same user right because when you run this request what is the id is coming so at the run time what exactly we will do from the response we will capture the id store in the id and then again we will hit the get call just to check that okay yeah this see this one four nine four if i'm writing it and then i'm saying okay fine this is my get call and this is my 1494 and token i'll just update the token here as well and then this user is coming or not so simple send the request and see the test automation that we have created the same user we have captured here so this is the second level of assertion we will be writing it the same data got created and then we will try to fetch the data with the help of get call and then we will check it is working or not that i'll tell you in the next video so please practice i'll do one thing this entire code i'll be pushing to my git repository and uh i'll share the get repository link in the description of this video and the first comment of this video also please go and experiment with different other apis with get call and with the post call let me know if you have any issues i'll see the next video till then take care and god bless you all
Info
Channel: Naveen AutomationLabs
Views: 5,083
Rating: undefined out of 5
Keywords: Create User - POST REST CALL, cypress api, cypress api testing, cypress api automation, cypress api mocking, cypress installation tutorial, cypress installation, cypress installation on windows, how to install cypress on mac, how to install cypress in visual studio code, how to install cypress on windows 10, cypress testing, cypress by naveen, cypress vs selenium, naveen automation labs, naveen automation labs selenium, Cypress API Automation - GET Call, rest api using cypress
Id: t9ZQhU3TOjg
Channel Id: undefined
Length: 19min 31sec (1171 seconds)
Published: Mon Jul 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.