REST Assured API testing Beginner Tutorial | Part 1 - Getting Started

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this session on rest assured McNair tutorial I'm raghav and in this session we are going to go step by step from scratch and we will learn what is rest has showed and how to create a project in rest assured for API testing now do not worry if you are completely new to rest assured and you do not know anything about rest assured or API testing I will go from very basics and we will start from scratch you can follow this tutorial on Windows or Mac operating system so in this session we are going to learn what is rest assured what are its uses how to create a project in Java in rest assured and how you can test api's with rest assured from that project so let's get started and here I will go on my Windows system if you are on Mac you can also follow this and I will show you if there are any differences and before that let me just very quickly show you and tell you what is rest assured so rest assured is a Java library or you can also say it is a Java API for testing restful Web Services so it is used for testing restful Web Services and it can be used to test XML and JSON so it you can test both XML and JSON based Web Services and it supports different HTTP methods like post get put delete and we will see an example of all these options and patch head and so on so all these HTTP methods are supported and you can also integrate it with any testing framework so it can be integrated with testing frameworks like j-unit or test ng etcetera so we will see an example in our project how we can integrate with test ng and this library or this API is implemented in groovy so this is implemented in groovy so this is a good question that you can be asked in interviews or otherwise rest assured is implemented in groovy now using best showed we can create our tests using BDD style that is behavior driven development using keywords like given when then I will explain this to you later so before that let us very quickly go to the rest assured website so here is the website rest assured dot IO go here let me just quickly go to this website and show you so this is rest assured dot IO and here you will find all the details about rest assured this is the official website and you can see some documentation here here you can see some sections in show news release notes docs and hue if I go to the intro you can see all this details here if I go to the new section we have this news about rest assured error the release notes where you can find all the releases and at the time of recording this video the latest one is 4.2 dot oh or you can also go to the documentation here you can see all the documentation getting started downloads user guides Java Doc's everything is here and then if you go to who here you can see the details on the person who developed it and testa shows is developed and maintained by John Hale abhi and you can see all these details here so this will be very handy you can check that now we will learn more about rest assured let me very quickly take you to my window system and we will start creating our project and for that let me first show you the prerequisites so before we start creating a project with rest assured what are the prerequisites so we should be having Java on our system we need a IDE like eclipse or IntelliJ or any IDE that you can use for Java development you can use that we will need maven because we are going to create a maven project and we will need tests Angie for creating a testing project or it testing framework with rest I showed so these are the prerequisites so I will go to my window system and I will go to the command line and I will check the Java version I will say Java space hyphen version and here you can see I am getting my Java version is Java 8 so this should work fine even if you have Java 9 or you know some later versions that should work fine so this is okay now just in case you do not get this a Java version here and you get something like command not found or some other error message it may be that Java is either not installed on your system all the environment variables are not set in either case you can go to my website that is automation step-by-step comm so go here and here automation step by step comm go to the section programming and go to Java and if you click here this go to this section job are here and it will take you to the YouTube playlist on Java and here you will find tutorials on how to install Java on Windows and Mac so if you are on Windows you can check this tutorial and if you are on Mac you can check this one so this is just in case you do not have Java setup on your system and when you run Java space hyphen version if you do not get this option or this this message you can check that or the same thing if I try on my Mac if I go to terminal and I will say Java space hyphen version and here also you can see I'm having Java 8 setup so Java is there so I can exit now we need our IDE we can either get IntelliJ or klipsch now in my case I'm going to use eclipse even if you are using anything other than eclipse like IntelliJ this is completely fine you can follow this tutorial so here if you have eclipse you can get started right away just in case you do not have it you can just go to Google and say Eclipse download and here you will get to this link on Eclipse which is Eclipse dot o-- r-- g-- and if you go to the download section here let me very quickly show you here you can see you can download eclipse from here now here if you download this it will be downloaded as a Exe or some installer will get downloaded and you can install Eclipse or you can also click on download packages here this one so if you click on download packages it will take you to the eclipse packages page and here you can see you will find all these packages now we can take this one Eclipse IDE for Java developers and based on your operating system you can click for example I am on Windows I can say a windows 64-bit if you're on Mac you can click on Mac 64-bit and then it will take you to the page where you can download it and here you can see again you are getting your option to download directly it will be an installer however if we just want to get a zip folder where that you can unzip a clip on your system you can click this one so this will download a zip folder and you just have to unzip and you can keep it at any location on your system and that is exactly what I have done if I go to my folder in my case I have kept it under my D Drive and tools and eclipse and you can see this was downloaded as a zipped folder I have just unzipped it and kept it here and this is how it looks like and if you double click on this Eclipse dot exe file it will start your Eclipse again if you have any issues you can again go to my website and if you go to let me just very quickly check where you will find this so if you go to this tools or go to Java Tips & Tricks this link this link will have our video which will show you how to setup a Clips so yeah you can go back to the Java playlist yeah here we have this video getting started with eclipse ID so and my first Java program this will give you very good details in case you are using Eclipse for the first time and then in the Java Tips & Tricks playlist I have a video on how to add test energy plugin in Eclipse this we will see later but for now we are done with our prerequisites Java and ID now we need maven and test Angie so let me first open eclipse so I'm going to open eclipse so you just have to double click on the Eclipse dot exe file here and this will open Eclipse so I am just going to my Eclipse now this is already open here now I will create a new maven project so let me also write down the steps create first project for API testing and here step one is open eclipse step number two is create a maven project so I will go here and you can either go to file new and go to other or you can also just do a right click here on this packages and just in case you do not get this packages or package Explorer here you can go to this perspective and click here and make sure that you are in the Java default perspective and still if you do not get it you can always click on this search and just start typing package and you will see we have a package Explorer here you can click that and it will show you the package Explorer so if in case you do not get this you can do that or you can then you can do file new or do a right click under package Explorer and say new and go to other and start typing maven and you will get a maven project option now in all the recent versions of Eclipse this maven option and the maven capabilities are already built in so you will always get maybe in project just in case you do not find this may be an option here or maven project option here that means we are using some older version of Eclipse and then you will have to add maven plug-in separately so just in case you do not get this you can always go to window so this is the window menu go to window or go to help here it is help and go to eclipse marketplace and here you can search for maven and then you will get a maven plug-in that you will have to install so this is only in case you do not get the maven option so this is what you can install and then restart eclipse but if you are using any recent versions of Eclipse then you will have the Mabon already in build so I will again go to file new others and search for maven and then select maven project and say next and here I will just say create a simple project and say next you can also select the location where this project will be stored so in my case I have selected this location I will gainsay next now here I have to give the project name so I will say this is let us say rest assured project and I can give the same group ID and our defect ID and say finish and you will see the rest rest show the project is here now you will also see that I can see my other projects also here in the package Explorer just in case you do not want to see all these different projects here you can also put this project in a working set now what is working set or this is optional just in case you want to do that or you can just click on these three dots here and here we have an option of selecting working set and here you can click on new to create a new working set you can say Java and next and give some name to your working set here and then you can select the projects that will go in that working set and then from here again you can say select working set and then you can select what working set you want to view and say ok and now you can see only the projects which are in that working set will be displayed so this is just a extra information let me deselect the working set now I am looking at all my projects now this project is created here that is rest assured project and you can see all these files and folders inside now if you go to a pom dot XML here this is the file where we manage the dependencies or where we put the dependencies of this project that is the libraries that we will need during our working on this project and to put the dependencies you just have to create a tag for dependencies within the project tag so if you say dependencies here you can also see it as getting auto completed and now all the dependency that we required will be put inside this so to get the dependencies for rest assured I will go to even central and here I can get the maven dependencies so I will search for Leicester showed first and you can see this is test assured make sure that you are getting it from IO dot rest assured and go here so this is the latest version 4.3 dot o at the time of recording this video I will click here and here is the maven dependency you can also see other build tools Gradle SPT IV grape etc I just need the maven one so I will just copy this and inside or within the dependency stat just copy and paste this dependency and this is the comment we do not need this you can delete this you can just say ctrl D and the entire line will be deleted also if you want to correct the identity you can select everything with ctrl a and then press ctrl I so it will correct all the identity so here you can see this is now added and what else we will need is we will need test energy because we are going to create a test energy framework I will again search for testing G and here again you can see it is coming from our G dot test energy so I will click here and the latest one is 7.1 dot the maven dependency and I will add DNA here and again I will correct the ident a shin so we now have two dependencies at it let me show you in full screen so this is my palm dot XML now if I save my project you can see if you see the tree here we have GRE system libraries here so these are the inbuilt GRE system libraries but we do not have any maven library or maven dependency folder here but as soon as I will save my project it will download all the dependencies that I have mentioned in form dot XML and all the required files and libraries will be added to the project so let me just save my project you can press ctrl s or save from here I will say ctrl s and you will see now you will have a folder called maven dependencies and it has all our required dependencies so you can see rest assured is here and you will also find tests in G here and then other required libraries or other supporting libraries or jars are also added so this is how easy it becomes to mint manage and maintain your libraries and jars if you are using maven if you do not use a maven project then you will have to manually download all these jars and add to your project and in case there is any change or any new version coming up then you will have to delete all the older jars and then get the new ones but here now let us say if there is any change or any new version coming up for example let us say tomorrow we have 4.4.2 version of rest assured I will just change it here and see if my project and nothing else I have to do it will take care of all the library management it will delete the older ones get the new ones download and put it here everything will be managed by maven and also if I take this project to any new system again I do not have to worry on any management or any libraries or any jars it will take care of everything so when first time you save your project on a new system it will download and get all the libraries from the internet and save it in the project and do all the management so that is why we create a maven project now also before I go or further if you do not have maven physically on your system even then it is fine if you are as long as you are running from eclipse because Eclipse has maven just in case later on if you want to run from command line using maven then you will need maven on your system as well and you will find again if you go to my website and go to maven under build tools you will find tutorials on how to get moving on Windows and Mac operating system so here are the videos but as of now you do not have to worry on that because in this session we are just going to run from here from eclipse so we have done step number three which is add dependencies in pom dot XML now if you go to the website of rest assured so if you go here and go to the documentation and if you go to getting started you will find some good details there so you can see in getting started here you can see these are the maven dependencies so which we have already done that is the rest assured and then it is also showing some other dependencies for JSON path so if you are working with our API that has JSON response this will be very handy JSON path then we have XML path for XML based responses and JSON validation schema validation libraries and then some other libraries now we will see all this later as of now we just need rest assured and we need test ng that we have already done so as of now we just need these two libraries so now we will also need Apache Pui later when we do data driven testing so if you just search for Apache Pui so this is for reading and writing to excel and CSV files so this we will need later then we start doing our data-driven testing but for now let us leave this and let us go back to our project now here I will create a class so you will see here in the project structure we have these folders we have SRC main Java then we have SRC test Java so because we are creating tests we will go to SRC test Java and all if you find a little difference in the folders and these icons do not worry you may be in project Explorer so you can see this is package Explorer okay this is package Explorer here you can also see the same structure in a project Explorer so let me just show you a file open project Explorer let me check yes it is here project Explorer if I go here you can see the same project is shown in a little different structure or the icons are little different than this so you can see you can check anything that is fine I am just going with the package Explorer so I will go to the SRC test Java and do a right click and go to new and now I can either create a new package just for easy categorization or I can directly create a class so let me create a class directly here and here I will name it let us say I want to run a get request or let me just also say this is TC or test zero one underscore get so that's it I have just given a name to the class and I do not need a main method because I'm going to create test ng method so I will just skip this mean and I will say finish so you will see a class getting created and this is the class let me increase the font so I have created a class here now inside the class I will just create a method let us say void now just in case you are all soon completely new to Java or you can check the Java tutorials here on my website so if you go to automation step by step comm go to programming and go to Java you will get to this playlist where you can see some basic Java tutorials so I will just directly start by a function I will say void and let me say test underscore zero one you can give any name to your function and brackets and a curly bracket start and stop so this is a very simple function and to make it as our test energy function I will give the annotation at test now as soon as you say at test you will see there is a error here if you hover over this or click on this error it says we have to import this from any of these libraries and we have options for importing from test ngoj unit so if you hover over this you can see all these options so make sure that you import from rajid or test ng dot annotations if you import from j unit then it will be working as a j unit test so I will just get the test ng annotation and you can also verify that the import is from the hodgiedot test ng dot annotation so now this is a test ng test now I can use rest-assured library and classes because I have already added rest-assured or dependency so I will say rest assured dot and you can see if I say get we have we are getting this auto completion if you do not get this auto suggestion you can press ctrl spacebar on your keyboard similarly if I want to do a post I will say dot post and we have post here we also have put we have delete then we have patch options everything will be here so all these HTTP methods you can use I will say get and here I have to give the URL of the or the end point of my API so for now let me just take some public API I will say public or rest API for testing I will search on Google and here you can see we have got a lot of options I will get this ar e QR e s dot in I will go here and here you can see on this website that is req arias Totten we have some fake REST API is that we can use for testing now in the coming parts of this video I will also show you how you can create a local server and host your dummy API send use them just in case you do not get this particular website or is it is down or if you are not getting the exact APS you can use any API your own API or any public REST API is for testing and I will also show you how you can create a local server for testing your own API is later so here you can see we have some API s-- get post put delete patch etc I will just take this list users which is a get API and if I click on this request here it will open in this opens the actual request and response and this is the URL I will just copy this URL I will copy this URL and provide it here so you can see I have just given this URL here and this is done also you can see it has also imported IO dot rest assured dot rest assured class from here so this is done so this will do a get on this endpoint now to store this I can just say response responses again a class from rest assured and I can use some variable let us say response here and as Java is case-sensitive this is different from this and here you are seeing that there is a error again under respond so if I hover over it make sure that you get it from the IO dot rest assured so this should be let me again show you if I hover over the response I have to get the library from IO dot rest assured dot response I will click this and you can see this is imported and now we have no errors so I have just stored that response in a variable called response and now using this response object or this variable I can get different data from response so I can say response dot get status code so you can see it will give me the status code I can also say response dot get body I can also say response dot or I can just say response as string I can print the entire response and to print it on console I have to use a print statement so I will say system dot out dot println which is a print statement in Java or I can just say sys oh and press ctrl spacebar on my keyboard and it will auto complete the statement so this is a shortcut type sys o and then press ctrl spacebar and it will autocomplete the print statement so I am just printing the body here so I'll put this inside I will again print the response body that is dot get body I'll cut it from here and paste it here and again I have to view it as string otherwise it will be in a different format and then the status code now do not worry VI we have not yet started the BDD style I will show you that later this is just for getting started how you get the response and different data from the response similarly I can also get if I want to get response dot get we can get cookies headers I will say that get status line also you can get from here then if you want to get let us say you want to get some particular header I will say I want to get the content type from the response headers you can do that and also if you just want to get that time of the response I will say get time so all this is coming from rest assured library so this is done let me show you the entire class so this is our entire class and this is our function and this is a test energy function now to run this I will do a right click and say run s and I will select test ng now just in case you are not getting the test ng option here that means the test ng plug-in has to be added to your eclipse and for that what you can do is go to help and go to install new software and here you have to give the link or the website link from where you will get test ng and for that you can just search for test ng and go to test in goog so I am going to test ng dot o-r-g here and here you can see we this is the official website of testing G here search for Eclipse plug-in so you will have this link Eclipse plug-in go here and it will take you to our github page and in the github page you will see all the details and if you scroll a little down you will see the latest release and this is the link so get the latest release and get the link from here and in your Eclipse under install new software just paste this and add and you can give some name also here and we'll have destiny here you can select test ng and then click on next and finish in my case you can see it as already installed or so I am not getting the option also I have a video here so if you go to the website and go to Java tips and tricks you will have a video here how to add tests ng plug-in so you can also check this video just in case you do not have the plug-in so once you have that plug-in you will also need to restart hairclips after getting the plug-in and once you have that when you do a right click and go to run as you will have the option of test ng also in case you do not get it here you can go to run configurations and here you can see test energies here you can select new give your class name here and then create the configuration but if you have test ng plug-in you will get this here so I will say run s test ng test and run this and let us see so you can see I am getting all my details here so this is the response body then this is the output of get body then we have the status code the status line this is the content type header this is the time and everything is passed if you go to the results of test ng you can see everything is passed so you can do like this now you can also use some asserts for example if I want to get the status code into a variable I will say in to status code or use any variable name equals to response dot you get status code so whatever is the status code will get stored into this variable status code and then I can use asserts I can say a third dot assert equals and I will say status code equals to the expected is 200 so now I am asserting that status code should be 200 if I run this again now I can directly use the run button it will always run as test ng because I have already said that earlier so if I run this you can see it is stunning and if I go to test engine results and we think is passed just in case I change this for example I say I'm expecting 201 here now if I run this they should fail and if I go to the results you can see it has failed and this has failed and here it is saying it was expecting 201 but it found 200 so that is why it is a failure so now you can do the testing like this now until now we have done a very simple we have created a very simple test if you go to the website of testing G or sorry if you go to the website of a test I showed here let me show you very quickly if you go to rest assured here yeah if you go to rest assured and then if you go to the user guide or usage guide where we have some examples so here you will find some examples and here you have a very good documentation you can also check from here or there for this if I scroll down and show you some examples you can see here is one example this is a APA response and to test this response this is how the example is shown this is get get the URL and then dot then dot body and directly I am checking this particular ID or this particular parameter should be equal to five similarly if you see other example in a single line we are using given then and all these options so how to do that for that you have to do static imports of the rest assured library for example here you can see in this import I will say make this static I will say static and to get all the libraries under rest assured class I will say dot star so I have now done a static import and now I can directly use the methods without creating object for example you can see now it is no longer required to create rest assured or put rest here I can directly use get if I remove this you can see it will work fine let me now create a new function and show you I will create a new function here I will say void test underscore zero two and again I will make it test ng test so I will give the annotation at test here now I can directly say given and if I press ctrl spacebar you can see I am getting this option given I will say dot get and inside get I will give the end point or the URL of the API and then I will say dot then and then I can directly check the status code so I can say status code and I am checking status code is 200 so here I will give the URL or the endpoint which is this one now we can also give the parameter separately in the given and then we can say when this particular URL that I will show you later this is very simple as of now I am showing you in a very simple way how to get started so this is my statement I am saying given dot get and the URL or the end point of the API and then I am saying dot 10 and I'm checking the status code is two hundred and a semicolon at the end and if I run this let me run this in check so this is running both our test cases and if I go to the run results you can see our test one is passed and test two is also pass if I make any change here let us say I say 201 and I say this and run again now this time this test too should fail and you can see here test two has failed and it is showing me the reason as well it was expecting 201 but caught 200 so that is why it's a failure in the console as well you can see it says it ran 2 and there was one failed so this is how straightforward it can be and now you can compare it with what you are seeing here in the official documentation of test I showed also you can put this in different lines for easy understanding for example I will say given and get I will put here and then I will put then here and I will say status code here like this so you can see you can also put it like this and I can also say after status code let us say I want to say body I can say body and I can give some arguments and the matcher here let us say for example in this particular API here this ID is 7 now I want to check under data and under data array and this particular value that is ID should be 7 I can see here dot body and I can give the path here that is data dot ID and you can see ID is here there are many ideas so I have to give the index position as well I want to get the first ID so that is 0 index I will say 0 and I want to check this should be equal to 7 so I will put 7 here and that's it now you can see I am getting some error here so this body if I hover over it you can see it is not messing anything so for that if I go back to the official documentation of rest assured you can see you can also add these static imports forth matches I will copy this and put them here in the imports and I will again import in a static way import static import static and put a semicolon at the end and save and let me just check so here let me see it should be if I look at the examples so it should be a-okay equal to this so I have to make use equal to here let me give equal to and I will say 7 and that's it so now this is done now if I run this again let me check if I run this again now I will make it back to 200 and I will run this again and let us see the results so this time everything passed you can see here test 1 and test 2 passed if I change this let us say I make this equals to 9 and if I run my test again and this time this should fail if I go to the results you can see this has failed test 2 has failed and it is saying it was expecting 9 but actual was 7 so this is how you can test different values as well if you go to the documentation you can see a lot of different examples and as of now we have started with a very very simple example in the coming sessions we will see some more complex examples so let me also put the steps here step number 4 was add test and G plug-in and I have also shown you the video or where you can refer in case you do not have the plug-in then let me also say step number 4 should be save project because after you add the dependencies when you will save the project you will have all the libraries added then step number six will be create tests and step number seven is run and verify so these are the steps we have done and as of now as I told you this is very very simple we have just got started with rest assured in the coming sessions we will see some more advanced examples and some different features of rest assured I hope all this session and everything was very useful for you thank you for watching
Info
Channel: Automation Step by Step
Views: 138,896
Rating: 4.9183221 out of 5
Keywords: rest assured beginner tutorials, what is rest assured, how rest assured works, rest assured for api testing beginner tutorials, how to create rest assured framework, rest assured from scratch, rest assured questions, rest assured for complete beginners, rest assured examples
Id: JJ7Tp7_fX4c
Channel Id: undefined
Length: 44min 39sec (2679 seconds)
Published: Mon Mar 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.