API Automation & Mocking responses with Cypress

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome guys in this section i'm going to talk about one important topic and this topic makes cyprus unique in the market no other tool is supporting such kind of features so let's try to understand what is this method and of course by saying the screen you know that method name is intercept okay so intercept method manage the behavior of http request so when you interact with your web pages so you would be calling a http apis and you get a response back and that response renders on the ui okay but that all happens in the end so from the front end you may not know all that information now let me take you to one practice app so rahul city academy dot com slash angular app demo okay so in this website so this is like a practice app where you can browse the products um select the products add the items to cart continue shopping okay and once you have the items in the cart you can check out and play around so in the same website there is another uh tab called library so when you select the library you will get all the books available in rahul shitty academy library okay so there is one request call going to this page which is giving us the response back with all the data let's see so let's come to the home page angular app demo so you can also reach that library page by clicking on this button so i will open my developer tools okay and go to the network tab so here where you can see what all api calls we are making so once you click on this virtual library and if you scroll down so this is one api is hitting the server okay when you click on this so this is the request url so this url is being sent to server and here it says that get me the book with author name shetty and when you select response tab this is what you are getting the response back so this response is consumed by front end in this case this is built on angular so angular is consuming this response and then it is displaying all the data in tables like this okay so this is how you are able to fetch the data from backend so ideally so the guy who developed this have tied up um that api call to this button so whenever somebody clicks on this button that api call will be made and you will get the response back so that response will be rendered in the table or you can click on this library also okay so when you select it again you see you can go to the network tab and you can see what all requests and responses you are getting to filter out you can select xhr so that it will clearly give us all http request and response details so we just understood that this is the url it's being hitting and we are getting the response so can we modify this request and responses generally people will say no because this is like complete different api thing right you need to use some other tools like rest issued which will help us to automate api calls okay so ideally people will come from selenium background and then they think that it's not possible but using cypress you can also modify and intercept your request calls your response calls okay so cyprus will have the access in this network level as well to modify anything here okay now you might get doubt okay cyprus have this access but how that is going to help me there are a lot of ways it can help us okay so many times you might hear this when you're working in your project that people say that this is not right candidate for automation okay because our people say that this is a flaky test and it's completely depend upon data that when data changes your test may fail so these are like common reasons we hear and we avoid automating those scenarios okay now for example let me explain you here so there are a lot of books displayed here so if there is only one book displayed in the table then you should see a message saying that sorry there is only one book available i have a screenshot here look at this so in this screenshot there is only one book available so this application this website will give you this message that sorry we have only one book available right so this scenario came for testing so ideally what people will do is they will find lot of accounts so that for which account this only one book is displayed okay assume that you are logging into this app okay most of the cases when people log in you will see lot of books but there are some data you found at that particular day that it is returning only one book because this is not in our hands okay this is coming externally the one thing what you have to note here um the data what you are receiving is coming from external api let us assume that you don't have control on that api okay so today it returned lot of records like this and tomorrow it might return only one record because this is coming from external library and you can't control that data so for that day you will check different accounts and see which account have one single row and you will test this scenario right and you will say its pass and if someone ask you that are you automating this then you might answer that this is not right candidate for automation because we never know how many books it will return if i write a test with this account and tomorrow if that account written multiple books then this message should not display right and your automation test will check for this message and it will break so we will stop writing test for that okay so this is how people do i'm just giving you one example but using cyprus there is a way to hack this so one thing first you need to understand about architecture of your application you just need to go to developer and ask like how i am able to see all these books then he will explain you that okay so there is one api call and when you click on that library button we are calling that api and you would get a response back like this all this response we are carefully capturing and rendering it on the front end using angular or react or anything you can see book name rest issued with java okay this guy printed here isp and rsu il number 2301 this is one record right and this is second record so if you need to first understand how this data is coming and you need to call that api and see the response so let's use postman and see how this api is behaving i'll tell you just follow me you will know how to hack this and make this scenario work using cyprus yeah so this is the service okay we are making the request call and we are getting response like this you see these are the different books written for each book these guys are creating one role okay so using cypress cyprus you can just tell that you can basically modify your response if this is the response if you get from your external source you can modify that response and you can return only one array okay there are lot of items present in this array right you can just written one index out of the all the indexes of this array so if only one index if you written as a http response your angler will keep an eye on what is the response i am getting if it returns only one item so only one row will be pre-populated here and then you should see this message comfortably right so that means you should mark your response so that is giving some random response every time from external source and you can mark it and intercept that to return a new response based upon your requirements and based on the response you design you can keep validations on your ui so that's the magic okay so you can read it here that you can modify http responses by changing the body right so right now this is the body right you can change the body and you can change the status quotes before they are received by the browser so cyprus can go to this level and modify your http responses similarly you can also modify your http request bodies and before you send it to the server you can modify after you receive the response or before you send your message so here this is the request okay so on clicking this button we are sending this request to the server you can modify this also to give different data using cypress at runtime when your test is executing and also it helps to perform integration testing between ui and backend so all this you can do with the help of cyprus intercept okay so step by step i will show how to mark response how to mock request and how to perform integration testing between your api calls and the ui okay so first let's get started with modifying or mocking your http responses now our goal is to display only one record and verify if this message is coming that is what i want to test and if you want to do it through selenium it's not possible okay you might find one account which gives you one row today right but when you run your test tomorrow and that time it might give multiple records and your test case will fail if you want to make it test always pass when you have dynamic data like this dynamic data is the most common problem in everywhere in every project with selenium so people just avoid those tests saying that these are not right candidates but let's see how to do that using cyprus so as i told there is a method in cyprus which is called intercept okay so this intercept have different type of syntaxes okay so if you see their official documentation um if you scroll down it support different type of parameters and different type of syntaxes so one common syntax where it always comes as handy is a request object response object so here you have to pass the request what you want to intercept and in the response object you want to give the response how you want to mock it okay so let me show you so in request object basically it's nothing but it takes as an uh json format like this for response also something like this okay so two json one request object json and another is response objection right so in the request object basically we should tell cyprus to have to have an eye on that particular request call because once that request call is made instead of giving actual response we have to give our mock response right so how do you tell that to cyprus so first you have to give the request on which you want to keep an eye for marking okay so this is the method is a get so key value properties method colon is a get you can check your network tab to get all these details okay and the url url and this is the url this is getting called right so copy this okay nice so just these two details are enough for us to make a call now cyprus will not make this call cyprus will just listen for this call to be made on the browser okay it will keep an eye on it once this call is made on the browser then cyprus will ask you all right i just came to know that this call is made and let me know if i want to send the real response or do you want to mark it and send your own response okay yes in this scenario we want to mark it so that market response you have to send in your response object okay so you can pass different parameters here that if you want to mark status code you can give that so right now let's give status code 200 only which is same as the original one the important thing is body so this is what we want to mark right and if you wonder like how would i know that i can give method url in request and status code body in the response you can locate their official documentation request object properties select this and you will see what all you can give in your request so in our case we used method and url similarly there will be response object properties and here we are using status code body if you want to mark headers then you can give the headers or status message okay right now so this is the actual response you are getting from that service call i'll remove all the indexes here and uh oh i think i can't edit this let me take into one notepad and let's remove all of this oh so many yeah so i have only one data now right this is our body so don't disturb the body structure because if it is not in the proper format then you would see the error so we are just removing the remaining indexes of this json and let's pass that into our cypress test and put them in quotes okay oh yeah quotes not required for body level sorry it looks for json response so that's what we sent um nice looks interesting and if you observe what we have done now so we have successfully intercepted the request call as cyprus is listening to it once this call is made instead of sending real response cyprus will send this response okay so ultimately first parameter is the request parameter okay and this second parameter is the response parameter and we are closing that intercept method we open here and we are closing here okay don't confuse between all these braces because there are a lot of braces here which will make you confused or you can write everything in one single line so that way you will at least i'll cut and paste it here okay nice now let's go and do actual automation so we just gave the instructions and now cyprus will take care now you can go ahead and do a normal automation just like how you do for all other apps so first you have to land on this web page okay so let's start on the top cy dot visit and this is our website so once you land here you want to click on this virtual library button because on clicking that button only that service call is getting made okay looks like we have class and this is unique so i will go ahead and use this button class primary cy dot get you can use css button is the tag name class equals to right so on this button you want to make it click operation perfect so once cyprus executes this click method this browser will make this http call immediately once browser make that call cyprus will be notified here and it is ready to send this response okay so all this intercept method i will collect it into one reference variable dot as so that you can refer that using this variable let's say book retrievals okay so i'm just having that into one variable because you have to wait until cyprus intercepted your response so like this you can do cy dot wait and you have to wait until this reference is solved okay basically this particular method will yield one result and you can also say that it will yield one promise that promise we are collecting into one variable and here we are using weight method to tell that wait until this promise is resolved that means this promise will be resolved after cyprus successfully sent mock data back to your browser so if you don't provide this then without i mean before giving the response back from the cyprus your test will continue and that will create a lot of confusion because as the data is not rendered yet your test will start failing so we are explicitly making our test synchronous by waiting until this promise is resolved okay so that happens only when you want to yield your result into one variable and wait that variable promise to get resolved with weight method okay so after clicking this button what does how does your application will behave that you get a response right um the response what you see here on where is that postman yeah this response have nicely made that into table format in angular but now what we did we marked it and we are sending only this one data okay let's see how our page behaves now i'll save this project i'll open the cyprus runner you see awesome your test already passed you can rerun again if you want but you can look at the results here and you see that message sorry we have only one book available how come how come this is possible okay let me run this test again so that you would see the output correctly this time so it's as usual going to that webpage just like how we have done in our uh normal loading and you see it is not giving us this data so that's the beauty of cyprus and this is what i want to show you see and when you click this link ideally all this should come right but see cyprus is showing only one data because you mocked that response how come that became possible because your citrus is very keen and it's keep on listening to your all network request immediately when it encounters this because we told intercept this immediately when it encounters this it stopped the response given by this url and instead it gave its own you body response that response being rendered to the network because your browser is just relying on this um api response to show the data as you have tweaked the data browser don't know all that right it just uses the data what it gives so cyprus tweaked it and gave the new response back and with that response it rendered on the page as you have sent only one you can see only one data and now you got this message so your test will never fail okay now you cannot complain that data dependency here data is dynamic that it might not work tomorrow if you see more records okay so now right click inspect directly on this and you would see that locator anchor p okay tag is p right so you can put a validation now nicely that c y dot get p so we should verify the text there right so i should have dot text so that text what we want here sorry we are we have only one book available all right so i'm running it again you see assertion passed so that's the beauty of a mocking your responses and you can test any scenario what you have in your application so if you are using selenium and if you have such test cases in your project just do a poc using cyprus talk to your dev team like whatever request response call is happening on this page and scan those results and try to mimic the behavior of your response and see if you are able to get the message like this all right so this is one scenario so there is one more scenario where industry-wide people are relying manually without writing automation test so which is checking an integration between backend and ui okay so there are many cases that you might come across a bug when you are integrating your backend code to render on the ui now let's take the same example so here you see all the records came as a response so what does the developer do he will make sure that every record showing in the response have to come on the table here but are you exactly checking that whether the response what you come from the api is matching here so when you test people might test that whether s column names are present and tables are displaying but can your automation check when i say automation it's a selenium like any other qtp selenium or whatever you use protractor or anything so can your automation tool will can validate whether the data what you see here is the correct data you can't tell that you can do only ui level automation but you cannot put a validation to see whether data is correct or not for that you have to compare the data what you are getting from your api response and that response and this is matching that kind of validation you cannot do with a pure front-end tool but using cypress as you have access to the network level you can compare and see the response what you are getting from your api call that response data is correctly rendering on the browser or not okay so i'll explain you one scenario where there went a huge bug in benson nobles so that is one uh hub of library so you can google about benson nobles so what happened like an api gave you a response with a 14 records okay let me show you yeah so 14 records you got in your api response and when user is trying to render on the front end so they'll just have a a for loop like this like for i equals to 0 and i less than third 14 and they will ask to render all in the front end using any front-end tools like angular or react if they forget and start from index one then they will skip first record okay so this is an array of 14 records so they should start from zero right if they start from one every time when they want to retrieve the data will skip the zeroth index okay all the data what they will see in the front end will start from first index because that guy have written a wrong code in interpreting the json response okay so that you have to catch you have to make sure the data what you're coming from response and front end both are same or not that kind of integration you have to perform which we call as an integration testing okay so to catch this bug in our scenario we want to make sure that number of records our api is giving should be equal to number of rows present in this table isn't it so right now our api is giving 50 records so that's why we have a 50 tables or 50 rows sorry 50 rows here so that you have to put validation um for that you really need not use a real mock response because you never know how much records that api is returning every day because that data is dynamic and people will say that we cannot automate it today it returned 50. if you put an assertion in your angular that it's written 50 tomorrow it might return less so for that also let's rely on this a mock data only so what we can do here let me put a logic that lets get the length of the response array and that length of response array should match with the rows of the table even if it is the real data or mock data doesn't matter so if you have this validation your test will pass and if somebody misses this logic and if they mess up with any logic then obviously number of rows in the table will not match with the length of the array right so plus 1 or minus 1 will be difference so this is the assertion you want to put and say that you have properly tested that what you are getting from back into front end kind of integration testing scenario so how would you do that let's see so if you want to get the length of the response array first of all you should have the access to response what browser is giving so right now you don't have access to response you marked your response that's all so how you can get access to response there is a way here so you have cy dot weight right so this weight method will resolve the reference and here on this weight itself that will after this promise is resolved it will give two properties for us one is the request property and one is a response property you can write like this request and response okay right so this request will hold the information about what is the request went to this end url and the response will be the what is the response you got from this endpoint okay so in this case we are only concerned about the response so what you can do here response dot a body so response object holds this information right when you say response dot body then you will be able to retrieve this and ultimately this is an array isn't it if you observe carefully this is an array and there is one index in it and if you want to length of the array if you want to know the size of the array its very simple right you can do dot length this will give you the size of that array so right now as you are marking this this should return only one recorder okay so that means as it is returning only one record here you should see only one row so how would you test it so for that what i will do i'll right click inspect you can use a tools like crow path or selectors hub to validate your exporter css you see for every row there is a tag called tr so if there are 10 rows you will see 10 tiers in our case we are marking the response so there will be only one row so when i do tr right now it is giving 51 okay 50 rows actual data plus 1 is nothing but your header okay so every time plus 1 you have to do because you are getting header as an extra tr so in this case when you mark you will have only one row and one header so that means total two tiers only you should have with your marked data okay so let's put a validation in that way so first i will get the count of the tr tags cy dot get you already know how to get the size of the tags right we have done many examples in the past if not i would recommend you to check out my previous lectures where we have done extensive exercises on retrieving the row count column count so using tr tag i retrieve it the number of rows and this length i am putting an assertion should have dot length so the length of this row okay that should match with your response dot body dot length isn't it okay so this response dot body length is one for one row how many tiars you should see two one is the original data and one is a header okay so plus one let's do right so we are adding plus one for the main row header and this is our actual content one plus one two and trs also should be two only okay so this is how you can put validation and see if the response body is properly rendering on your browser so in my case it's a tr in your case it could be anything other tag or any other css okay so your overall goal is to make sure that how that items in that array are rendering on your browser that related component you should take a locator and get the size okay and you are able to get access to that response with the help of yielding this weight because i explained you right we are waiting until this is the result once this is resolved it will yield two properties request and response and once you get a response and response dot body because i've shown you what are the response object properties you can do response.body response.status code headers everything and on that body we are applying length to get the count of this array okay let me save this and check back your cypress test to see how that behaves um great it's done you see all our paths expected to have length of two assertion pass so let's do one magic here um i will remove or put plus five okay so it returns us only one item plus one plus five six so we are making sure asking that there are total six rows should be there in our page and now it will fail you see it's trying to make that asset valid but if it cannot it fail you see it says i found only two rows but you are asking me six it will fail now um to make this work you can create a body with five more indexes so let me create one more i'll just add one more okay so we are basically marking our response so this time it will return 2 right ok so this should be still valid so it returns 2 plus 1 3 and trs also should come 3 right so no matter how many you pass this is a generic statement generic assertion because at runtime you are getting that value how many rows two rows and here also two data we are passing so it's pass but another thing failed here oh when you don't have one book okay that message that sorry we have only one book will come only when you have one book as the book number increased it failed okay so that's why make sure you mock with the right data for your right validation all right so that's how you can have a check from your response side and make sure that's properly rendering on ui side this is one kind of scenario i'm just giving you different scenarios where you cannot achieve with other automation tools and how you can achieve same using cyprus so in this lecture let's understand how to mock http request and purpose of mocking it so right now when you select this library every time so in the request every time the author name is going as a shitty so let's say you logged in into this app using shitty account so that's why at run time based upon your user id this is getting pre-populated that what if i enter author name as another user okay so you should not be able to view that right because you are into shitty account so that's why you are able to see the books from this library which is related to shetty because you are authorized to see only the books of shitty author okay let's say there is another author called malhotra so in your account from your portal you cannot view the books of malhotra and that when you try to view that then you should get a forbidden 403 status quo do you know about that 403 status code says that you are not authorized we generally get as a forbidden that server understood your request but refuses to authorize it okay so it should tell that as you are not the right user i will not give that details for you and that is what you want to test and you want to make sure that data here should not display it should give me an error saying that you are not authorized to view the books but can you test it using front-end automation tools no you can manually also you cannot test it okay so how do you test it manually you can do it but using external middleman tools like fiddler or there are some birth suite there are some proxy tools which will help you to intercept your request but from your regular testing you cannot check it because whenever you click virtual library always it is showing the books of shitty okay if you want to see that what happens when the books of malhotra are hitting how can you manually test it because on this button click by default your backend front end code is just giving you the books of shitty but if someone ask you to check malhotra books even you cannot do it through manual testing also you should bring some other tools like as i said fiddler and birth suite but using side press yes not only testing manually you can also automate it by intercepting your http request okay let's see how to do that for that i will create another class now i am going to use another syntax of intercept if you remember i told you in the beginning that intercept comes with a different syntaxes okay so it's up to you what you have to use in our case now to show and demonstrate this i will be using this one um cy dot intercept a method url and root handler this is one of the way to declare intercept okay so what is the method of that http call that's a get right so get and next you have to pass the url obviously you know the url of it this is the regular url so we are just asking cyprus to keep an eye again for this request to happen okay so once this request got triggered and you know when this will get triggered this will get triggered when you click on that browse library right when that button got clicked this will be triggered and here this is a request right now you can take this request details let me write in other length line yeah so all the details what you entered are now captured in the request object here that's what root handler okay so url and the method everything is now captured here and i can start writing to modify this object now okay on this request object now i will modify this url you see you can modify this url method by giving a new url here so this is the original one right author name right so in this manner on this request property and i told you like how you can find request object properties here instead in the previous lecture the whole syntax is different don't get confused here this the syntax take request object as a first parameter and response as a second parameter this you can use if you want to mark your response but if you want to mark your request then i would recommend you to use another syntax of this this is exclusively to marking your regular request and now on this request object it yields these two will yield a promise and we resolve that promise now on that object we are modifying this url to this url okay new url after you modify the url you can hit a method called continue on your request object when you call this method continue then what happens then this request is being sent to the server okay okay once again i missed this so once you are reading the promise you are writing everything inside this block okay right so this method now will hit the server with the modified request so modified request is being sent and once you send to the server you get a response back so that response you can again collect as the reference variable here in the response object and on this response object you can put any validation on your response object okay so in our case now so we've modified a request object and we sent to the server and we got response back and now you want to verify whether that response start status code is 403 because we just realized that when you are trying to reach your server with another user instead of shetty if you go with malhotra on your shitty account then your api call should give you back a 403 error and say that you are not authorized okay so this is how you can check with cyprus so right now our our application will just give the data even if you give it any user because we did not implement any security as of now in this application but your test should fail right so ideally we are expecting 403 and now your test should kick you saying that i got 200 success but don't run we still did not click on that button so this is like only intercepting so we are giving the knowledge like what to do when you receive this request so in the earlier lecture we told that when you receive this request go and mark this response in this file we are saying that when you receive this request first modify your input request and then send it to server with continue method and that will yield response object on that response object we are putting validation that it should be status equal to 403 okay and all this intercept we have to yield right yield it into one reference variable let's say dummy url like this we need to resolve this promise because this intercept method yield a promise so you can say wait until this is resolved put that in a code but you missed one prime thing that first to make all this request call happen first you have to click on that button right if you don't click that button this will never get activated because this method will be called only after you click on this button if you don't click on this button this method will never be called and this will be never been stored and there is no point in what you are doing now okay right let's see i will save the project it should fail for us because our app right now is not having that mechanism so it should kick us with failed fake test two okay what happened here it says it written 200 but we want 403 expectation so that's why it failed so right now this is still loading here because your test failed here okay it failed at line number 17 only so this intercept method did not finish so but we are telling wait until this got finished how it will wait because it failed at line number 17 only so it interrupted in the middle and it did not yield anything so it is waiting until this to get yielded right okay so for now i will comment it because you saw that it's failing as it's returning 200 but let's see how that data is rendering in the output i will save the project again okay see this time you received only one record and this time we did not mock anything earlier we got like 50 records and we also got one record when we got one record we are very clear that because we mocked that's why we got one record but why this time we are receiving only one when there is no mocking in place because there is no author named shetty here we modified our request this time it's going with author name malhotra isn't it let's see right click select this request dummy url and you see author name equals to malhotra so this time it is not using shitty and that intercepted request is being sent to the server using our cypress and the results what we are seeing here is the malhotra results if you see something like this in your application then there is a big security bug in your system okay even this app have that is security issue as this is dummy app i did not implement security issue okay so if you are working in any banking application or any application which have username and password after you log in try to hit any endpoint with another user information and you see if the data is loading if it is loading then anyone can hack it okay using tools like burst weight and fiddler you can google that tools using that tools you can intercept your request or you can do using cypress also so now using cypress i am able to see the page of another user called malhotra okay but ideally when you do a testing from your normal application you won't see that right when you are manually testing the page you will never see that but using cyprus we are able to mock and see how that is behaving so in this manner you can go ahead one step and perform security testing also using so ideally when you are trying to access the data which is not allowed for your user developers will give the response as a 403 so that you have to put assertion in your code and make sure that passes if it gives 200 that means data is coming back and that's a security bug so in this manner we will modify and intercept our http request to check security bugs in our application i am not sure how many of you guys doing this now but not only doing it manually you can write automation security test cases also using cypress by intercepting your request calls and you can put validations on your response status quotes like this okay so that's how the cyprus power is all right so that's pretty much about this intercept i hope you guys really enjoyed this and i request you to play around this dummy app what i have and instead of sending one single data try to send multiple data and see that this message is not coming up i'll go and mark the request and put another user and see how the data is behaving because all these are available for practice so that's pretty much about intercept in cyprus so one last topic before we wrap up this api testing concept with cyprus so till last lecture you have seen on how to listen to your http calls and then you captured up after that http call got happened okay but you did not hit that call so you did not hit that call to the server you made your ui hit by selecting the button or whatever but the job what you did is you listened to that call whatever it is happening from the browser and then you either gave the details of that responses or you mark the responses but you yourself did not hit that http call right so now if you want to do pure api testing that you itself hitting the api and getting the response back without depending upon this ui completely now i want to do pure api testing and without bringing any web into picture then i can simply make this http request calls and get responses using the request command i expect that you have some basics of api testing to watch this lecture so if you are not aware of basics reach out me through email i will help you to give some api testing information so that you can go through the videos and come back here to understand this basic knowledge is enough i'm just showing that how to hit apis using cyprus without depending upon any browser or listening just direct it so the request command accepts firstly what type of http call you are trying to make if it is a get post or delete and the end point where you are hitting and if it is a post then you have to give the body json payload of hitting that api all those details if you pass into this request command then this will hit the api call and get the response back as a promise you have to resolve the promise and extract the response object so that you can put validations on that it's very very simple um firstly to demonstrate that i will open one of my own api whatever i designed for practice for students it's a library api the end point is this where the service is located so this is the resource so basically base url slash resource to add a book so we were actually adding a book to the library and all the book details we will send as part of our json payload like name of the book isbn in which i'll it gets stored and the author name all the details will be given as a payload and it's a post request so we will hit this end point so that this book will get stored in the library databases and it will give information on which islet is there okay the output what you get after this api call is made if it is success then you will get a response like this message successfully added and your book will be assigned a unique id number this id number is combination of your isbn and aisle number so if you concatenate both of them that would be your id number which you will see in the response so that is the concept if you have aware of any tools like postman to actually do this testing look at here so this is an end point and this is an complete payload and just you need to hit on send button so that you will get a response back make sure you provide this isbn and i'll they should be unique if there is already existing book with the concatenation of these two information then it will say that book already exist so you have to give the unique value so that it will get stored now when you hit this request with these details look at here successfully added okay so let's try to actually complete this validation in this lecture we will do that using cypress now so with their documentation let's do it in very simple fashion um let me create a new test first i'm not going in-depth and teaching all the concepts of api because it's not really required so we use cypress only whenever we require on-demand okay there could be scenario that you have to fill the form but that test is data is dynamically coming from api so you can happily do this api call get the response capture the values and feed it to into your front and test rating okay you can put all that information in your test data whichever required in your front-end apps in general if you want to achieve it without using cypress we will use a combination of selenium plus rest assured api restriction is one of the rest api testing framework which will help us to do all the api validations we will combine those two tools and solve the scenario of getting the data from response and feeding it into the front end applications but using cyprus you can do everything here with the calls whatever i am showing this lecture all right so i will simply copy a few details so here there is no ui so you can forget about c by dot visit it's just a pure api call with so let's read the documentation and complete the test accordingly first cy dot request method let's capture this here method for our api here is a post okay so let me replace this first with post next url which is nothing but an end point where you are hitting your server so the url to add a book is this i will give this documentation to download the word document whatever i prepared to understand this api you can use this and try to create a test to validate api using cyprus and now body so you have to also send a json payload to tell which book you are adding and what is the aisle number isbn number and who is the other that's a json payload right okay so you should place for your body perfect that's it so this is very simple now when you hit this command through cyprus when you run this particular test this will actually hit the server and get the details for you and now you need to extract the response and you need to put validations right and the response whatever it returned it's again promise so resolve the problem is dot then capture the response whatever it returned and then work on that it's as simple as this so we were grabbing that you know right how to get and resolve the promise then basically you write like this function and then you got response out of it okay so right now when you run this test it will just does whatever you are doing here when you click on send button you will get a response back right same thing this test also does and gives you the response back and in this response object have everything where you can start now validating let's say that if you want to validate whether message is successfully added or not in your output then this response object have response let me show you start look at here i'll author everything is coming body so when you say response start body whatever the response body you got back you can actually retrieve it so this is your body right and if you want to evaluate this message you can simply say expect response body to have property basically we were validating the property message here to have property key value pair this is the message nice and then you are validating if it's coming as successfully added or not perfect so you are already familiar with these kind of assertions right so here response object is what it returns back and when you do response dot body it gets complete captured of the response whatever you see in your output from there these are multiple properties available in your response message is one property id is one property so we were actually validating that from the complete response body for message property verify whether it is having successfully added message or not if it is there that means we can confirm that our api got success on hitting and getting the response back so this is how you can put validation so there are a lot of validations you can actually try on that request method that specific command you can actually come back to the assertions and check out here response.body length total characters of the body is 500 you can validate and if you want to see the status if it's getting 200 or not so what is the status we were expecting here in the response so here it is 200 so now you can also add a validation that make sure that it returns 200 as your status okay just read the official documentation to understand what kind of different assertions you can put if you just wonder how would trainer know that response.body is how if you want to grab the body of the api response i just learned from here everyone should come to this page and learn because the authors of cyprus have written that this is how you need to do everyone reads that and follow and implement that in their live projects so if you want to verify whether this text is present in your response body then you can say dot its body should include read out this documentation to understand but the main assertions are properly displayed here so that's really helpful for you to go through this and also give the unique values here if not you might get a message saying that book already exists if the isbn and ilm were whatever you give it already exist in database and one more thing you have to put promise on request command why are we putting on at this this is wrong right it should be here for your request command we were resolving the promise and then we are working on that process okay we need this so let's run this test okay i will go to my so i press press runner and say api test.js since it started here oh it's done it's very fast let me rerun again perfect look at here assertion is passed expected 200 to equal 200 and the expected message of successfully added is also validated so this is how you can do api testing as well with your cyprus it's very very easy compared to any tool the only documentation you should have handy when you are working with this is the request command keep this documentation handy and just pass the value just the way how they were telling you look at here now if you have any method url and body to test we did the same way right post method url and the json payload you give and then you resolve the promise from the response in the body you are looking for name property should be chain okay we did something like this so this will be the best example for you to understand and explore more on the api testing but as i said if you have a number of api test cases and if you project is more of complete api automation then i would suggest you to have a dedicated tool like rest issued framework for api testing so that's all i have guys thank you
Info
Channel: Rahul Shetty Academy
Views: 3,381
Rating: undefined out of 5
Keywords: cypress, apitesting
Id: mUbQsVcmoKU
Channel Id: undefined
Length: 60min 6sec (3606 seconds)
Published: Sat May 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.