How To Automate Rest Api Using Rest Assured | How to Automate API Testing using Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone this is mukesh from blonde - Ordovician calm today in this video we'll talk about how to automate REST API using rest assured now maybe for you it's rest assured it's a completely new term but it's a very powerful framework for testing rest api s-- so before starting rest assured let me talk about what exactly the rest api is and why nowadays rest api testing or overall EP testing is getting popular and why everybody is focusing more on AP testing so in this video we'll talk about what exactly the best api is what if why EPA testing is in demand the different tools available in the market for rest api testing then we will talk about what exactly is rest assured okay and why we are using rest assured I'm what are the different framework apart from rest assured then once all good will start creating our first project which is based on rest assured and you don't need to worry about it because it's a pure Java framework so they have some couple of libraries that we need to use so if you have worked on Java earlier then it's a piece of cake for you then we'll create our first API test and finally we will validate the output so previously I created two videos on REST API s-- so I will share this link so if you have not gone through this then I will share these two videos these two videos we'll talk about what exactly is web services okay and soap services REST API these two will be covered in this video and I found a very interesting video on the internet which is this one this is mainly on the REST API so I will highly recommend you to watch this video regarding this this will clear all your doubts regarding a REST API it has crossed almost 15 million views because it is one of the best video in that internet forest now let's check the initial thought thank you so before jumping into what exactly the REST API let's talk about the API first so if a is nothing but application programming interface okay so here in API you will not get the UI okay you will not get like proper HTML page where you can see the data here the data will be in a different format so in API is we will get the data in different formats like XML JSON proper raw format and so on so generally who develop this API developer use the separate project or tools to create this API s-- and as a testers we will get a simple URL and using that URL we can test the API so what kind of test we need to perform what exactly data we need to extract from the AP that we will see but if you see this picture the programmer will create API and they will expose this API so will expose in a form of URL once you get the URL you can hit this URL it's will this will go to the server and you will get the data so data you might get in different format that we will discuss so let me give some examples of REST API ice okay you can see this make my trip all this travel sites so have you ever thought how these API or how these applications are working okay so let me give you two examples of make my trip I will hardly take four to five minutes to explain this example but once you're done with this examples you will be clear what exactly the API role is in the application okay so if you go to make my trip go I before or clear trip everywhere you will find the same flights right so let me open three different applications and let me open clear trip okay so when asserts certain flight from source to destination when you click on search so you are getting this data right now how you are getting this data from the different airlines so make my trip is a set great company how make my trip is getting data from AirAsia from a spice at indigo Air India and so on right so how these applications are working so if you have gone through my previous video you will understand APA will help you to communicate between two different applications so how make my trip is communicating with air ratio or indigo or Jet Airways through AP ice I am communicating with you in English so we have a pure language right to communicate in a same way when two applications talk they talk in a form of AP ice so now make my trip is communicating with AirAsia Indigo they are sending some requests and we are getting the details so these details are getting displayed on the UI in a form of HTML or some other form right so these data actually is coming from the API only it is coming as a HTML for the users ok so now you can see magnetic trip is able to communicate with so many vendors through EPA's if you see this AirAsia same flight you will find in clear trip and Kauai people if I try from Delhi to Bangalore enter Bangalore so we'll get the same flight so let's select any date okay you can see the same flight is coming for tea a trip save will come for go I Bebo so these companies are exposing their AP ice and all these companies like make may trip they are getting their data through AP ice and then displaying on their pages so this is how they are working every application you take they take help of REST API so okay there too kind of a pace over rest and slope so that you will see in my previous videos here we are talking purely about rest a pace so these ApS are giving some data and these data are getting consumed by different applications 95 percent companies use REST API X highly few companies which are very ristic to the data or some protocols they use soap so if you take 95 percent including Google they use pure REST API s-- so it makes sense to first cover REST API and because in rest we have so much flexibility we'll get the data in different format it's the language independent and so on so I will give me giving you some URLs where you can get all this details you will get some demo API is to test and if you want some effective a pace that also I will share so once you go through this video here you will get one tool called a PG that a PG is a separate tool now this tool is acquired by Google so this a PG will give you all the AP ice which you can test ok so officially it has been acquired by Google so you will find so many ApS on this a PG it's a paid tool but if you want to see how exactly this a PG works you can sign up for free and check certain AP ice let me see if I have a log and I can show you if I don't have then maybe I will create and show you no I don't have a court yet so once you log in you will find so many examples where you can get pure YouTube API is Facebook ApS Google APs Twitter API and you can start using it okay so let's go back to the PPD and see what exactly does she pay it's it's the stands for representational state transfer and it's just the architecture style it's not a protocol if you go through the soap it's a pure protocol where you have to follow certain standard but it is not a protocol in the CPA you will get the data in different formats you can even send that it in different formats so in terms of flexibility it is high flexible as compared to your soap so now why why nowadays everyone talking about ap ice first of all it's very fast ok so if you talk about one particular test in selenium where you have to just check the login functionality it takes minimum 10 seconds right you need to open the browser enter username password click on login it takes few seconds to hit the server get back to the reply in a form of UI but if you do the same test in APA it will hardly take one second and it will show you whether you are valid user or invalid user because it does not have to wait for the UI to appear it's quickly send the data to the server get the data back end you will get your final result and you can compare you can use assert it is mainly for testing the core functionality now think about it now if you are getting that correct data from APs then definitely the same data will you will get on the UI yes word if ApS are working you I definitely will work there few chances that it will not shown the UI UI is also important because at the end of the day and users are going to use UI only but if a peers are working definitely UI will work so nowadays people are focusing more on API s-- because of quick response type of time effective it's a language independent as I said it supports multiple format so if certain applications are working next ML JSON plain data format it will work easily integration with GUI ok so once you have the API ready you will get the data and you can easily integrate with any kind of Gy I have taken this a couple of example from this slide this I will give in the description you can go and check this PPD they have mentioned in detail ok now what is rest assured before jumping into rest assured this has certain open source plugin which is available for you so you can use postman plug-in you can use the wristlet client and soap UI so P as we know we have two versions pro and free so you can use free version as well I have given three tutorials on that now if you go through this rest let and a postman so this is one res 'let client and the is the postman these two plugins are free and they will help you to test the APS so if you use these tools it means you are testing this a piece manually you're not automating it okay but we are trying to automate so we'll write a plain Java code to automate this I will show you some one example for wrestle it one example with postman but these are called again a manual process wait again you have to start these plugins you have to send the data you have to click on send the data and you will get the response then you will assert something ok we are trying to automate it so we are going to use one framework which is called rest assured framework so what exactly is rest assured it's an open source DSL dava java domain-specific language so if you don't use rest assured we have an open source API called HTTP client so rest assured is built on top of rest said rest assured is built on top of HTTP client so internal to use all the methods classes interfaces of that particular framework but they have created so many wrapper so it's very easy to use I will show you in few minutes and you will see how easy to use a rest as shown it supports both the form and XML format and just Jason format so you can use based on your requirement and I got this statement from one of the web application I guess from the rest assured itself that rest assured is so easy even a grandmother can learn it only because you have certain keywords that you need to remember send the data and you will get the response from the server ok so rest assured support all these requests so whenever you are making any request ok it sends to the server and you will get the letter back so these are the protocols or I will say request it will support it will support post get put delete options patch and head and this for example I will show you with get and if we get certain API will go through post and delete as well okay so now only get will give you the data and others requests which we are talking like post put delete this will make changes to the resources on the server okay so you will hardly get certain ApS which will allow you to make the changes to their resources get is fetching the data and other protocols or requests will make the changes to the server so the preconditions for rest assured like you need Java so you can use Java six seven eight minimum is Java six you can use any version of Eclipse so I'm using Eclipse neon but if you have Eclipse oxygen also to work so you can use any version of Eclipse I will recommend go with the latest Eclipse neon or the oxygen because it has integrated maven if you use older the clips you need to download maven then it's your choice do you want to use destined goj in it since destined is the latest I will go with test engine now let me show you so I have my project sorry I have my clips ready I don't have projects so I will quickly create a maven project if you are not getting maven project option here then go to project and search for maven click a maven project and give create a simple project and just write rest assured framework you can copy paste the same and click on finish you will get your project ready if you are not sure about the maven then I have three videos on maven you can go back and check my maven videos so you can see our maven project is ready and you will get all this structure I will quickly go to ponder'd XML and at the dependency so this is the official site for rest assured rest - I showed you and you can see here they have given very beautiful code very small code I will explain you in a minute so you can see these are the versions which is available I will go with latest which is 3.0 dot 0 and now if you go down you will see the download section usage guide Java dog and so on so I opened all in the new tab ok so you can see here they have given two options for you either you can use the rest assured which is coming from condor JV dos rest assured or else you can private io dot rest assure so i will go with the second version and now you can see a small trick here if you use this regular expression like dollar and rest assured version it will download the latest one every time when you run the code so either you can give the same exact copy paste in your maven dependency or else you can give the exact number make 3 dot 0 dot 0 okay so if you want to see we have maven - poster comp so if you go and search for rest assured you will see this rest assured and these are the versions the stable one you can see the most mostly uses 3.0.2 you can give this exact version if you wish ok and they have lost till 0.5 years well you can go and download but you let's take 3.0.2 which is mostly used here also you will find the same but here if we use it we'll always download the latest one so it's up to your wish so we'll go to eclipse and go to this pondered XML okay i will increase the fontsize just add one dependency stag okay and just copy paste this dependency and you will see it will hardly take few seconds and it will download all these dependencies okay for the first example you need only this okay once we move forward we need couple of more dependencies based on your requirement as of now one is enough and I will recommend you to remove this test scope because sometime it might create issues so once you press ENTER you will see a different section called maven dependencies and you will see all these are getting added automatically as I said earlier it is built on top of HTTP client so these jars are must if you're using normal Java project then you might have to download individual jars which will create oh so much pain for you I will recommend go with maven project and it will internally use on this so HTTP client is the base jar which internal rest assured we'll use hamcrest is a library which will help you to see the pattern and put more assertions once you get the data you will use JSON path once we get the XML data we use xml path and so on so internally we use all this once we move forward we'll see all this libraries one bad word for the time being we need only this which internally downloads everything now you are ready now let's see how we can create and how we can test one API so good we always previously they had like you need to also add one dependency but in latest release once you add one dependency groovy all by default it will come so if you see groovy will come here groovy and groovy XML and groovy Jason ok if it request will go in again add groovy all but it's not required and these are the URL for practice first of all you will not get a proper URL or proper APA which will allow you to change the resources it means you cannot delete certain data you cannot update ok but once you start working with some some of the companies they will give you URL or API which will give you the flexibility that you can update something on the server you can delete something you can do all the operations for the time being I will use this open weather map this is one API so actually open weather map it's a separate company which provides so many open api s-- some are paid some are free so now it makes sense to buy certain APIs and create your application now just take very simple example if I want to create my own application where I want to show the data of my locality like weather information and some other information then I have two approaches either I will create my own team I will assign individual candidate that go and check the weather and update every time or else I can take this API send I will pay some amount and I will get all this details within a seconds so open weather map is company separate company they have price if you want to go with Pro Edition as of now when you click on sign in ok I already sign it in case if you are not sending it will ask you to create account once you create account you will get one API key this API key will help you to weld it whether you are authenticate user or not so there are so many open api s-- okay so like current further data forecast weather map history you can go and take any of their APIs as often I will go and take their first API which is getting the weather data so generally what exactly the APA will look like it will be enough form of URL so you can see this when I open this link in a new tab so this API is giving me weather information of London you can see here cue means query when you pass certain city here or country it will give you the specific weather details and you can see that this app ID is my app ID which is only for me it is getting added then only it is giving me the data and you can see this data is coming in JSON format so you're getting everything here like sunset name coordinate base cloud details country details and so on okay this data we are getting from the server we are sending the data like we want weather detail of this particular city or country and this is my app ID it is hitting through the server and we are getting the response and this response we need to validate okay now how to see the validation part we will see but this is how we get the data this is just one example if you go they have so many AP ice as of now we have captured the data through city name but if you want to capture the data using city ID they also have a separate API where you simply pass the city ID and it will give you the details in the same way you can also pass the coordinate zip code and based on your request it will give you the data so once you have free time go through all the APS which they have and you will get all this data now some other if I go show you this Bank application so this example which I showed it has the data in JSON format but the data which I'm showing you it is an XML format I got the safe way from one of the application it's some Bank application so when you send this a specific customer it will check if this customer exists it will give you the details of this customer so you can see we have some ID first name last name address detail phone number and necessary right now if you try to search some other data let's say one three this customer does not exist so this also is coming from the server so I can see when you log into your bank application how this communications happening right how these APIs are getting consumed and so on now if I go to the base URL is not working this also is not working okay so you can see these are the different services which they have okay so this is bookstore information and this is Bank service and so on so the current one which we have used is for customer so like this once you get the effective URL you can hit the request and you will get the data back so this is XML this is JSON rest assured supports both so it's up to you what exactly you want to verify sometime in application we have requirement when I search for this customer I should get first name John this is what my assertion is this is what my validation and in other terms when I hit when I send the city name London I should get this name and this coordinate or this country as GB would get bitten it's a P which kind of test you want to perform so let's see we have the applications we have some data now let's see how we can validate so first of all I want to show you with postman and there's some other links also which I have found I will share with you and if you want to see the exact all end-to-end scenarios end-to-end tutorials then you can see this site called hash code com this guy has published so many articles on rest assured first of all it's not new this guy published in 2011 but still you will find all the content is relevant okay so let me start with postman so when you starts working with poem oh sorry this postman first of all let me delete all this like close all this this I will show you once again this once we start rest assured so you need to just search for postman so either you can go to their Chrome Web Store and search for postman or else you can just search here postman and you will get there first of all Chrome Web Store where you can go and create or else you can go to the website as well so if you go to the Chrome Web Store as of now I already have so it is showing the launch application in your case it will be installed when you click on install it will launch it will install as a small client in your system like this and when you work first time it will ask you to create account here so when you create account so some data you can store in your account and you can use after said after some time okay if you want to continue with guest also you can continue so I have created my account I have logged into my accounts you can see my account is coming here is Mukesh atrani and you can see here they have all these options get post put pass delete copy had options link and so on so you will see if you have multiple application API you can create you can run them in different tabs so this is collection collection is like you can create a collection store all this ap into one collection they have new option runner you can import and you can export as a code so code also you can see they have different language it's pretty good plugin which is really works without any error and it's completely free I will not say completely free because if you want to go with some ApS then they will charge you something actually that is up to you but 99% you can use as free you can see if you want to expose certain you can expose in different languages so if you want to expose in Java JavaScript node.js PHP Python they will expose the complete code so this is complete Python code if you want to test same application so once you write your application click on code and they will generate the code for you and you can just copy paste in your editor and you can start using it so let me first copy the URL okay well again open open weather ok this API key will be different for you I will again post on my description if you if you can use the same APA it's good otherwise you need to use your own API keys I guess it will work because they have not given some restriction okay so I will just take this complete API URL enter here and you can see get get means we simply are sending some requests to the server and we need the data back post means we want to update something delete we want to delete so when I hit the send button okay you can see per param is nothing but parameters if you want to pass certain parameters individually you can do that as of now there two parameters which is already in the URL so when I click on send you can see the complete data is coming in JSON format you can see JSON is selected here but you might get in different form it depends on your requirement or application and you can see the status code status code already I discussed in the previous video it will give you which kind of response is coming from the server generally we look for 200 ok so we have 5 category 1 X X 2 X X 3 X X 4 X 6 and 5 X 6 2 X 6 means a this data is correct - not one - 100 is completely fine okay so if you go and check the status code you will find all the status code linked in the Wikipedia we generally look for 200 okay we just come which comes under sexist category so as of now what I will do same thing I am going to e do in rest assured we'll discuss more about this postman it's very big plug-in and very handy as well so now our project is ready so let's create a new package package I will say get request and I will go and create a new class so mainly I will give this class name as get data and I can either take published static void main or else I can go ahead and take a unit and distantly so in this example I will take test in G and I will say public void test response code so now we have a separate class called rest assured in this class first of all you can see with rest assured they have so many classes that is assured rest assured matches multi-part entity so these are different classes each class have different usage like matrices when you have to match certain response code multi-part entity when you upload certain friends and so on so once you move forward you will get to know the functionality of each as of law rest assured have so many inbuilt static methods so what are the method which I will use it will be get method so in this if whatever URL you will pass it will simply hit that particular server request and you will get the response back okay so let's take this URL and let's go to a clip and paste here okay so let me decrease the font size so you can see this is the request or URL now what exactly I want to do first of all I want to see when I send this request what kind of data I will get so when you put mouse over on this you can see it is returning me response so now what is response response is a separate interface here you can see it is coming from IO dot rest assured response so whatever information we will get from this server will store in this rest object now this response object will have all the details of the current response so there is one method called get station status code as the name says it will return you the status code of that particular request so as we know we are looking for 200 so first of all I will try to print and I will say status code is code and then I will put assert here assert I'm using of test ng so in case if you are not aware of test ins you can go ahead and watch my previous videos on test and J so assertequals will ask you two arguments one is actual so actual is anyways coming from the code and expected expected is 200 so when I run this code you will see it will return you the data in okay we need to add at the retest so right click run Esther stingy test and you will see you will get the data back either pass or fail so status code is 200 right and yes pass it is giving you pass because we have given we are expecting 200 what if I change to 400 definitely it will fail because assert will check if both are not matching it will fail code will come 200 we are expecting 400 and it will fail right so this is how this rest assured works so we are simply sending a request get request we are getting the data we are storing in a response interface object since we cannot create objects we are getting a reference here in this reference you will get all the data we are fetching only the status code I should ask me McKay how can I get the other data well if you don't need a scored let's say you need the complete body so just in spite of checking the get status code you can write as a string and you will get the data in complete string format okay so cyst and let's say this is the data part so I will say the data is and I will add that data variable and I will if I want to assert anything I will simply go and assert it so as often I am just printing here so you will see this particular method will assert this particular method will simply print the complete data in a form of a string okay because this method will return the data like this we can capture so many details let me capture one more detail what I will do I have a say sis out and I will say response time for time also they have a separate method so if you say it is dot get time or you can change the time in decimal you can see get time in some different format it will do as of now it will give the time in milliseconds I guess so let's see 331 milliseconds you can see here now if you do the same thing through postman also when you hit the here it is taking 358 millisecond 315 almost same so nothing to worry it's almost same it's taking even less than a second so you can see how easy it is to write the code and rest assured now if I show you some code like good practices so now if you go back and check their official website they have written the code in different format like you don't have to you actually use this rest assured so what you can do you can go ahead and import this rest assured methods so if you go and say static and if you put dot star it means you want to import all the methods of rest assured statically at the class level in that case you don't need the at all you can directly say get okay if you want to see you can go and and this a rest assured and if you go to the usage guide then you will see that same code they have written they don't use this rest assured dot something they directly starts at given when then and so on here also they mentioned you can import this rest assured statically which we did just now okay so let's change the code little bit in spite of writing storing in a variable and then call you can directly say in code equal to get past the URL and see if Todd get status code okay and here also you can do the same thing you can say get just hit the URL say get or as string it will return you the data in string format okay and this time if you need you can say time who get sorry you can say get time it will return you the time in milliseconds okay so it's it will give you in it in terms of lawns you need to store in long and this time you can pass it okay this is the time and now if you go back and use this and if you want to see the debug part you can see here when I see right click debug s test in your test it will open in debug mode okay let's send the data to the server sorry request to the server and it will take few seconds it is still communicating with the server and yes we got the code as 200 which you can print assert and this is for another method response time this time is for not one and your complete test is done so like this you can explore this API because it's a very big API it pays like rest assured framework I would say so they have given almost everything every information which kind of method you want to use everything they're given so just explore this they have given examples also only thing which you need to do you need application so if you find your official application it's fine otherwise you need to host your own applications and then you need to test it okay these are the URLs which you can give a try you will find certain URLs from this and the diagram which I showed it starting I had taken from this knowledge tester dot orz so you can refer the site as well and try to explore as much you can and the apg one APG which i showed that is very good you will find all the public aps for YouTube Google Facebook Twitter LinkedIn you can go and use that EPG as well so that's all I have for this video in upcoming videos we'll talk more about the rest assured more about API testing and there's one more EP called karate API which works Kumba with rest assured so that also will discuss in future so in case if you have any doubt feel free to ask in the comment section so thank you so much if you liked this video kindly share with your friends subscribe to my channel for further updates and if you have any doubt feel free to ask to my email id ok short one yet third learn - at amazon.com that's all I have see you in the next tutorial have a nice day bye bye
Info
Channel: Mukesh otwani
Views: 274,877
Rating: 4.837923 out of 5
Keywords: How To Automate Rest Api Using Rest Assured, how to automate rest api using java, rest api automation using selenium, rest api automation using java example, java rest api testing framework, java api testing framework, rest assured maven dependency, rest assured, how to automate api testing, api testing using postman, API Testing, api, testing, software testing (software)
Id: aqrxDxumKZQ
Channel Id: undefined
Length: 42min 20sec (2540 seconds)
Published: Tue Oct 10 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.