Session 2: API Testing | RestAssured | Creating Post Request Payloads in Multiple Ways

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so in our previous class we have seen  the first example how we can create different   type of request like get request post put and  delete request so now today we are going to see   how many ways we can generate the post request so  we can generate or we can create different ways of   post request so whenever you do the post request  we have to send a body along with the request we   have to send a body right and this body we can  create in multiple ways okay so today we will   see how many ways how many ways we can create  we can create request body okay how many ways   we can create a request body so there are mainly  four ways are there the first approach is what by   using hash mapper so by using hashma we can  create a request body and then we can send   along with the post requester so that is a one  approach which we have the second approach is   by using org.js okay so there is one more uh  Library which we have so we have to add that   library or J dot Json so by using org Json we  can also create a request body so using orj dot   Json Library so this Library we have to add in the  com.xml and we will get separate classes for that   and by using them we can create our request  body and the third approach is what by using   pojo class by using pojo class what is poser means  poser means plain object plain old Java object   and this is again we have to create a Java class  okay I say plane old Java object so this is   actually most of the times this is a more famous  way to create a body uh using pojo clean object a   plain old Java object so this is a full form of  pojo and we have to create a separate class and   by using the separate class we call it is a pojo  Class by using that we can create a request body   and the third approach is the third fourth  approach is using an external Json file okay   suppose I have some Json data in external file  and I want to pass the data as a request what a   request I can use that file so using the external  Json file okay these are the four different   approaches which we have to create request body  okay so now we'll see one by one so how to create   hash map yesterday we have seen one example  today I'm going to show you one more example   how to create request body using hashmap using  org.json Library using pojo and using external   Json file okay so this is a concept for today so  first let us start with one by one so before that   we need to have an API so let's try to use student  API to perform these operations and partly I will   also show you show you other requests get post  boot request first let us mainly focus on post   request okay because this is the most important  and multiple ways we can create request body okay   so the first uh before starting them so let us  start our API student API so for that what you can   do is we need to just go to our student.json file  which we already created earlier let me just go to   automation practice Json file so here I  have a student dot Json file now I'm going   to run the file open the command  prompt and uh Json iPhone server   so I hope everybody know how we have to start  our own API right so earlier we have discussed   during Postman session same thing we will  continue so Json server is my command and then   space students dot Json so this will run our  students API So currently how many records we   have in the students.json file currently  we have uh four five records are there so   let me remove some records and because we  will add more number of records and we will   parallely delete multiple records okay so  let me just remove the four and five okay all right so this is the end of third record okay done so from here to here  this is one record and this is   closed array and this is the closing of Json So  currently we have only uh three records and let   me save the file okay so currently we have only  three records now let me just start on my API okay so now it is get started so don't  close this window so we have to just   minimize until we completed our job on  this API so let's minimize this okay   all right so now let us see how we can do the  post request in different ways and later I will   show you get request delete request put requests  and so on so first of all let us focus more on   post request so let's go to eclipse and today  I'll create a new package press ratio day two   uh day two package so inside  this I'm creating a new glass   and take the new glass and here name it as a post  request or we can create another way s to create   post request body okay so different ways to  create post request body so different ways   to create post request body so click on finish  so this is my new class which is created so the   font is visible or else let me just increase  the font go to preferences General appearance colors and font and this is a basic  inside the basic this is my text font edit   let's make it as a 20. okay so this is my new  class which is created so different ways to   create post request body so for that let me create  a new method okay new method we have to create and   we have it different ways right so let me just  keep all the ways here and we will see one by one okay so here so the first approach is what using  hashmap second approach by using json.org.json   third approach by using pojo class and the  fourth approach by using Json file okay   so let's see how we can do this four different  ways of creating the post request right   so for that we have to first import  static packages and other things so   let me try to import them so go to my  day one and get the static packages   so these are the static pack even hash map is also  required annotation is also required so let me   just copy and keep set of all the import packages  because every test we require these packages   all right so now I have imported required packages  now so the first approach is what by using hash   map we can create our request Body by using a  hash map hash map is a Java collection and you   can store the data in the form of key and value  page so even in the Json also most of the times   we have data in the key value page data will  be maintained in the key value page so hash   map is also one of the collection which is exactly  suitable for Json format of data okay so the first   thing we'll see the first approach so what is the  first approach post request body using hash map   okay so post request body using hashmap  so I'm creating a one method here I'll say   public void Public Access modifier if you want  to access method method from somewhere else if   you want to access it you can just put public  or else not required so white and I'll say test   post okay using hashmap using hashmap so this is  a method name I'm giving and I'll specify this   as at the rate test annotation so that we will  able to execute it so white test using test post   using hashmap post request using hashmap  so here we have to prepare the data so how   the data we have to pass how we have to post  the data in the body so if I just look at my   sample Json so this is actual data if you  want to pass one record ID name location   phone number and courses courses contains  the multiple values so we have to create   this courses field as an array and that array  we have to pass in the collection that is Hash   map okay so let us see based on this data we  have to prepare our own hash mapper so let's   look at this data accordingly we will prepare so  let's create one hash map object as a hash map   data and equal to new hash map instead also  you have seen right same thing so hash mapper   okay so we created a new variable of hashmap now  we can store the data in the form of key and a   value page so when you store and key and value  pair let us look at the data so what is the first   field here ID will automatically generate the  runtime ID will be generated so we don't need   to bother about ID and then rest of the values we  have to create name is one field location phone   and the courses totally four fields which we have  so first one is name so in the data I will add   means put put is a collection method if you  want to put some data in the hash mark we   use a put method so the first field is what  name it should exactly match with that okay   so I'm giving some data here this is quota in  my first field second field so data dot put   and the second file is location so I'll  say location and here the value of the   location let's say friends this is second  one and now the third one so data dot put   phone number data output phone number and here  the field name is phone and here phone number   phone number is also available in the string  format so I'm just giving something one two three   four or five six Something I'm Giving so  these are the fields directly available   so they are just a key and value page right  so they're just key and value page but just   look at here uh name is Method name location  phone number and these are the directive Fields   name and uh key and value page but when you  come to the courses which contains a multiple   value courses is also one field this is also key  but sometimes the key can have multiple values   but actually one key is allowed only one value  at a time but if I just look at here this key   is having multiple values actually we should  not say multiple values the key have only one   single array as a value so which will store this  particular key can store array as a one single   value so this array entire array is considered  as a one single value okay so now first we have   to create a courses array which contains the two  values and then we will add that into the field   okay so before keeping this data in the courses  what we have to do is we have to create uh Java   array okay let me just create one course array  course ARR course ARR equal to and here I will add   two courses okay one is let's say C is one course  and then I'll add one more postcode C plus plus   okay so now what is this now course array is one  Java array course array is a Java array which   contains the two values and what type of array it  is which is a string type of an array now course   array is a stringy type of an array single  Dimension array which contains the two values   now can we add this array into the hash map as  a single value obviously we can add so now what   I'll do is just like other fields and values key  and value page similarly I will add data dot put   data.output and what is the field here courses  and what is the value of the courses I say course   ARR this is the way we can create so we  have a Json array first we have to create   a Java array like this and this Java array we  can add as a value to this particular field   so now we have successfully added all the data  into this data variable hash mapper these two   are together so if we have a single Fields uh then  you can directly specify the data and sometimes   you have a multiple sets of data like this then  you have to create an array first and that array   you have to add to that particular field okay so  now we have prepared the data and all the data   is available inside this field data field so now  we have to send a post request and along with the   post request we have to send this data so now let  us write all the sections now first of all let's   start with the given given when and then so these  three methods we have returned so in the given   section what you have to specify what type of data  we are sending which is basically content type   so dot content type so what's the content type we  are sending Json format so application slash Json   and now body we have to send dot body and  whatever the data we created in the hash   map by using hashmap we just pass the data here  that's it and the next one in one section we have   to start with the dot and inside this inside the  van we have to say request dot post request and   inside the post what you have to say URL so what  is the URL of post request let me copy it here   3000 by default port number it will use  you can just look at the request URL so   3000 currently using so to post request  this is the URL which we have to send   for post request right once you send the  post request what is a validation now then   so then we have to validate right so then inside  the then we have to put multiple validation points   so the first validation point is what the first  validation point is status code always the first   validation point is status quo Dot status code Dot  sorry Dot status code status code should be two   zero one okay when you create a new record most  of the time status code will be two zero one but   some apis will return 200 also sometimes and this  is the one thing we have to verify and what else   we have to verify we should also verify the name  student name location phone number and whatever   courses we have added so enter response body we  can verify each and every field in the response   body we can verify so for that what you can do is  dot body so this is the method dot body and which   field you want to verify name field name field  you have to verify and the name field should be   equal to this is the method okay the first  parameter is a field and second parameter   is a expected value so equal to what is the  value we are expecting here is cot because   that is the value we have passed as part of  post request so in the response also we are   expecting the same value which is cotton and  next one so what else we can verify dot body   dot body and location location also we have passed  so what is the location we are expecting here   equals to equal to location what is the  location we are expecting here is France   as if friends this is one validation similarly we  can also validate all other fields one by one so   after location we have also added four number  so I can say phone uh which is also equal to   one two three four five six okay and then we can  also verify the courses the data we can verify   so how we can verify the courses so again the  courses contains the two values right so courses   contains are two values if I just look at the Json  data so courses contains the two values how we can   refer these courses courses of zero courses of one  by using index we can represent the internal data   so now dot body so dot body and uh what is the  field we have to verify now courses field in   the courses verify the first field which is zero  index and what is the value we are expecting here   so what is the value you are expecting c c we  are expecting this is a one validation point   now dot body and what is the second field in the  array courses of 1. courses of one and the value   of it is C plus plus C plus plus so like this each  and every field we can validate in the responsor   okay so we already created a hash map which  contains some data okay so same data we have   posted so after posting the data whether it is  created the record with the same data or not that   we are verifying here so normally we don't need  to test every field but if you just randomly one   or two fields that is enough because whatever data  we are posting here same data is displaying or not   so we don't need to compare each and every field  most of the time just verify one or two fields   that is enough and if you have a complex Json  again we have to specify the complete Json path   here and accordingly we have to specify the value  which will compare okay so this is and uh finally   I want to verify the header also so along with  the body I just want to verify header so how we   can verify the header part so I can say dot header  because header is also part of the response right   so dot headers cookies everything you can weld  in just I'm heading for header so header and   what kind of header we will get here is content  type I want to verify so as part of the header I   will check the response is Json or not I want to  verify so I can say content iPhone type so this   is the name of the header if you send this request  in the postman tool you will know what is a header   okay this header type and what is the value  you are expecting for content type you can   expecting uh application slash Json this  is a header type you are expecting here   application slash Json this is the header and not  only this there is something requires it also part   of it CAD set equal to UDF iPhone 8 okay so  this is a uh actually this is the header one   and if you send this request in the postman uh  you will also see the header or in the response   body also uh we can print the header value  right and after that I want to print entire   response body so to print enter response body  what is the method DOT log of all so this will   print enter response body okay so here it is  giving an error the method body okay one second did we miss something here so this  is equal location dot body location   equal to France and courses of zero quotations closed comma sorry equal to Method  we missed okay sorry equal to so equal to yeah   yeah location we verify two times yeah so equal  to we've missed equal to Method so we cannot uh   misses okay we have to say so location we put  two times let me remove once okay name location   phone number and the courses of zero and here  I can say uh equal to C plus plus okay yeah   and content type so content type equal to is  not required so you can because whenever you   want to verify the values then you have to use  equal to and uh to verify the header part just   specify the name of the header comma value of  the header that's it okay so this is a way we   can validate the response so this is the first  approach by using a hash map we can create a body   and we are sending the data in the form of  Json and we are sending the post request   and in the response we are verifying the  content okay so this is a first approach   to create post request using hash map and some  people will also use this approach but this is   suitable if you have small set of data Okay  small set of data this is preferable to use   okay so after sending this request I am also going  to delete this record parallely so for that I am   going to create one more method wide test delete  okay so delete also I'm going to create and test   annotation I will give you this priority is to  and this is for a deletion request okay deleting   student record for deletion I have written this  okay so let me just write the code so how to   delete the record how to delete the record  and again same given okay given Dot when okay and then then so in the given session we  do we need to pass anything for deletion request   not required anything so in the when  we have to say delete method delete   and if you want to delete any students so what  is the URL we have to specify the student ID   also which is part of the URL so let me just keep  it here and this is representing the student ID   whichever ID is created in the previous request  the same ID I just want to use and if you want   to return the ID yesterday we have seen right  yesterday we have seen ID Bill automatically   generated by our script okay dynamically we'll  generate in yesterday session I told you how to   return the ID and how we can use the same ID for  deletion the chaining we can do it if you want or   else so we can just for now output this value So  currently I'm just going to create uh one record   at a time so immediately I just want to delete  the record okay because this is our dummy API   which we created so it is not more flexible so I'm  just creating the request and immediately I try to   delete the request I just want to work with only  one record okay so later we will see how we can   return multiple values single value how we can use  it for another request chaining process we will   see that later okay simple uh I created one delete  request method and once the delete is successful   what is a validation here I can verify the status  code uh that should be 200 and after deletion we   don't have any content so even if you say log dot  all you will not get anything so I'm just ending   the statement here so simple delete request I have  written here and for this I am giving priority   I say priority equal to 1. okay so now we  created one post request and one delete   request in the post request we have passed body  and we have generated the body by using hashmap   concept okay so let's try to execute this and  right click run as and it will immediately post   the data and the validation will be also done  and after that deletion also will be completed   okay so run as test RNG test and make sure your  API is up and running parallely so I haven't   closed my command window I just minimized it okay  so now both are got passed now if I just look at   the console window and which is generated fourth  recorder okay the same record is got deleted   so this is working fine so now let us see  how we can generate the request Body by using   uh orj dot Json library or J dot Json Library  this is another way of creating the post request   okay so what I will do is I  will just copy the same code I'll make it another entry okay and the first  method I don't want to run now so I'm just   commenting this and if you comment this test  annotation test engine will not execute this   method okay when you comment this test annotation  test NG will not execute this particular method   so I have commented that so now go to the second  approach so second approach is what post request   body using using org dot Json library org.json  library so this is the second approach okay   so in this what we have to do is everything is  same only this part we have to change previously   we have created data by using hashmap but this  time we are going to use something called OJ dot   Json so I'm also going to change this name of the  method test post using or J dot Json or else Json   Library okay Json Library okay now let us see  how we can use this org.json library to create a   post request that is a second approach okay so for  that the prerequisite is we have to go to prom.xml   and in the palm.xml we have to add one  dependency so that is let me show you Json so this is the library which we have  to add this is org dot Json Library so this   dependency we required okay this is the  dependency we required and we can get it   from A1 repository so from where we can  get it so if I add this Json dependency   we will get some additional uh classes  and methods from Json so by using them   we can create our own request body so this is  a prerequisite so we have to add the dependency   now go back to the we already added so let's go  back to the request so let me remove this hash map   okay now we need to create the Border request by  using org dot Json Library so first step what we   have to do is we have to create Json object  variable so we have a predefined class that   is called Json object Json object data equal to  new instead of hash mark we create a Json object   so new Json object there's a first step we  have to create Json object variable or we can   say this object of Json object class this is the  class name predefined class which will come from   org.json and which is created an object data is  my object so we can add data into this variable so   just like a hash map we we also have a put method  here so data dot put same just like a hashback   data dot put and here we can say name that's  my first field and here I will say the value   so I'll says value is a chord so this is a first  value and similarly we can also add just a second   yeah so similarly we can also add  multiple values so just like this copy so name is first field and then location we already deleted this data earlier  so I'm just using same data so location phone number two three four five six  and then name location phone number then   courses we have to we have to create  courses so how to create courses here   so earlier how we have created courses  by using Java array concept right so   same thing here also first we have to  create a Java array so string courses string courses array single Dimension array  and here I'll keep the values the first score   CC and second course is say C plus plus two  courses I have added to the courses array now   this array I can add into the data data dot put  data.put and the key is what courses courses and   the value is courses ARR courses okay so here  yeah courses Ara so at the time of validation   we use index so courses Ara so with this we have  successfully added the data to the Json object   variable so first step we have to create a  Json object variable and uh in this we can   use put method to add the data value so every key  having some value name location phone number and   courses contains multiple values so which first  we have created array Java array which stores some   data and then we have added this array to the  courses variable so with this we have created okay so if it is an integer value so we should  not put the double quotations for the value but   key is always keep inside the double quotations  okay the key is always we have to include in the   double quotations but the data is depends like  sometimes uh the API will take number suppose   that is a phone number if it is accepting number  then we no need to keep in the double quotations   and if it's taking as a string format then  we have to keep in the double quotations   okay so currently it is taking phone number as a  phone number as a Stringer so that's the reason I   put in the double quotations okay right so now  we have prepared the data by using Json object   and where we will get this if you add this  dependency or g dot Json Library dependency   then we will get this class right so as usual we  have to pass the data again in the post body and   same request and same validation no changes at all  rest of the things are everything is same here the   main thing is how to generate the data okay this  is how we can generate the data by using Json   Library what is OJ dot GSM Library okay so let's  try to execute it the first method I have already   anyway commented this will not be executed so  this time it will execute this one and after that   immediately the record will be deleted okay let's  execute now all right click run as testing G test okay so the first one is got failed  let's see why it is getting failed   so Json path name doesn't match okay  so some error a session error we got   Json path I think we have given  some different value somewhere   okay phone number one two three four five  six four number one two three four five six   okay what is wrong so we verified the name  is called a location France phone number   some validation is getting failed yes okay  uh daily okay one second so not division so I   think this is deletion is request is successful  so because we validate only status score 200 so   that is passed actually but does not actually  deleted the record the request is successful   okay so we have done some mistake in the  first one Json Library while sending the   request through Json Library we have done some  mistake let me just cross check the console output   so it is clearly saying Json path doesn't  match and expect it is got but actually is null   okay so it is not taking something here so data  dot put name equal to Scott okay this is a key   and this is a value okay location is France  so phone is okay so the data we have passed   correctly but somehow validation is failed so name  body dot name equal to Scott two brackets location uh Json data Json object everything is fine  no issues at all okay let me go to the file   and see whether it is created record  or not let me close my Json file once   open the Json file storage.json so which is also  not created anything okay let me just try to run   one more time it is executing but somehow this  name is not able to boil it scored here also   Scott name is a field okay uh let's create  one more time let's execute one more time okay so again there is one more field expected  is got but actually so null it is getting   expected is called but null it is getting  that means it is not able to send the body uh we are using games and library right so we  are using Json Library not hash map so when I   use hash map when I use hash map concept so this  data we are directly passing into the body okay   so this is allowed but here we have created  this data right by using org.json Library so   we cannot directly pass this data inside the  body we cannot directly pass the data inside   the body so what we have to do is we have to  convert this into string format and then we   will able to send so here we how to convert  this data into the string format I say data   dot data dot to string method we have to use  data dot two string method we have to use   so then this entire data will be  converted into string format and then   it will send as part of the request in Json format  so ultimately we have to send the request body in   the Json format object but the data should be  in the string format and if the data is in the   string format then it will convert it into Json  format okay so if you are using org.json then   this is the one step you have to remember but  if you use ash map we cannot we can directly   pass the data we don't need to change but when I  use org.json then we have to change to the string   and then we will able to pass it okay so I have  done it now let me execute run as test NG test okay so now both have successfully done so  201 created so two zero one is status code   which is returning and for delete request  for the delete request expected is 200   it is given 200 a status code after  deletion so that is also got passed   so this is the second approach by using  org.json we can create a request body   so far we have seen two approaches one is by using  hashmap by using orj.json so now the third one is   most important using pojo pojo is nothing but  a plain old Java object plain old Java object   so here we have to use some concept called  encapsulation in Java we have a concept of   encapsulation what is an encapsulation so the  wrapping of variables and methods into one single   class and here we use something called Getters  and Setters yes so we have to use Getters and   Setters to generate the data or to create the  data so now we'll see how we can create a post   request by using pojo class so that is a third one  so third approach so for now let me just copy this   I'll make another entry and then comment  this one commented the second one also now   go to the third approach so post request  body using pojo class using pojo class using pojo okay so this we have to modify okay  later we will do modification and rest of the   things are same given when then everything is same  only the data we should modify we we have to take   this data from the pojo class okay so before that  we have to create one course or class we have a   separate class we have to create and from that  we have to capture the data so let me create a   new package under day two so new class and this is  our pojo class so I'll name it as pojo underscore   post request Ojo underscore post request so  this is a class I'm going to create now so   pose underscore okay post request and this  class contains a variables and Setters and   Getters so what are all variables we required to  send the body what are all variables we required   name location phone number and then courses  array courses is a an array so totally four   variables are required three are the Primitive  variables the other one is a array variable so   four variables we required so in the pojo class  I will create four variables one is string   uh it should be same name okay string name  and a string location is one more variable   string a location is one more variable and a  string phone phone number is one more variable   and one more error is required string array  string courses single dimensional I've just   declared the array later we will add the data so  first we have to create a four different variables   now to assign the data and retrieving the data  for every variable we have to assign the data   from every variable we can extract the data  so for these two things we have to write in   Getters and Setters for every method so for  every variables how to generate automatically   Setters and Getters we can just select the  variables go to source and here you can find   one option called generate Getters and set  us just click on this option and for all   variables I want to generate Getters and Setter  so select all the variables click on generate   so it is generated all Getters and Setters for  every variable I can remove from here I can   keep the variables at the beginning so if I just  look at this for every variable there is a getter   method there is a Setter method so what's the  setter method will do it will take one parameter   and assign the value to the class variable same  thing set location it will take location value and   assign the location value into the class variable  so every Setter getter method will be there for   every variable so Setter method will assign the  data to the variable getter method will get the   value from the variable so like this first we  have to create a class this is called pojo class   this is called pojo class play in Old Java object  class so once it is created by using this pojo   class we can generate the data so now once you  created this we don't need to run this anything   we have just used it for data preparation so go to  the actual glass actual test so here we are going   to use this pojo class so how to generate the data  by using pojo class so let's try to remove this   okay now by using pojo class we want to  generate the data by using pojo class   so for that first of all we have to create an  object of pojo class then only we will able   to access all Getters and Setters methods so take  this class name and create an object let's say new pose underscore post request class so I created  one object so by using this object can we access   all the Setters and getter methods can we access  yes so we all have Getters and Setters so we will   able to access all these methods by using  this data method so now what I'll do is I   will use data dot set name method I'm calling  set name I'm setting the value to the name   so here I will pass the data let's say  Scott so now what happens this particular   method will be called from pojo class and  this data scored data will be assigned to   the name similarly we have to assign the data  to all other variables so for that we can say   the next method is what set location set location  and here the location value I'm passing this is a   location and what is the next one phone number  so data Dot data Dot set phone set phone number   and here phone number so name location phone  number the next one is what we have to pass the   courses values so how to pass the courses value  first we have to create a string array here string   courses or courses ARR equal to in the Kali  braces keep the value as you see C plus plus   this is a uh these are the values of array so now  uh we have to set courses data dot set courses set   to courses is also we have our method here set  the courses so get the courses set courses set   courses will take courses array as a parameter  courses array as a parameter so we have to pass   array here so set courses this courses array  I'm passing as a parameter okay so now this   will go here and assign the same data to this  courses okay so this is how we have to create   so now uh this is created so data dot set course  is also done so with this we have added all the   data to the variables by using pojo class we have  just added the data we have not retrieved the data   okay we have just added the data so internally  suppose if you want to get the data if you want   to get the data from the variables we can use get  methods get location get look get phone number get   courses we will use those method if you want to  retrieve the data but for now we are just setting   the data into the variables so with this we have  done and the data is ready now we need to pass   this data as part of your request so I'll close my  post request pojo class now by using pojo class we   prepared the data so now we have to pass this data  as part of your request okay so how to pass the   data here and we don't need to specify two string  we just say only data here to string is required   only for uh Json Library so here not required so  we prepare the data by using pojo class and the   same data we are passing as part of the request  and rest of the things again same no change   okay so let me just execute save it and then  execute and we already commented previous ones   first one second one we commented this is the  third approach post request body using pojo class   and delete request it is there  anyway now run as testing the test okay so now both are executed and also passed you  can see here this is the body which is actually   taken and ID is also generated so it is got  deleted first request and second request so   this is one more approach we can create your post  request Body by using pojo class by using pojo   class this is also most popular way many people  will use it right so the next one approach is what so the next approach is what  by using external Json file   external Json file okay so now we'll see  the fourth approach using external Json   file how we can create the request body using  external Json file suppose I have already data   in my Json file which is an external file I  already have some data so I can use the data   for post request okay so let's see how we can do  that so let's go to your project so I will copy   one file here and that file contains the data  so right click on the project create a new file and I will name it as body dot Json body dot Json and I'm creating this file inside  my inside my project so this is body.json file   so inside this I will keep some data so whichever  data I want to post I will put the data here so   this is my data I want to send along with the post  request this is the data I want to send along with   the post request okay so create a new file and  have the data like this in the Json format and   save the file so this is the data I want to post  along with my post request okay so let's create   uh one more type of post method let's  make another copy and I will comment this   second one commented third one is also  commented now go to the fourth approach   so post a request body  using the external Json file   external Json file so by using external  Json file we can create test post using   the external file external Json file okay so now  what we need to do here is let's remove this part   so rest of them are exactly the same so now we  need to get the data from the external file so   we have to get the data from the external file  okay so to get the data from the external file so   first of all we have to open the file we have to  open the file so how to open the file here we have   to create something called file type object file  so this is again French comes from java itself   file F equal to new file and here I'll specify the  location of my file where exactly it is available   So currently this file is  available inside my project   so what I can do here is instead of giving the  complete path of the file I can simply say dot   slash slash dot is representing your current  project location dot is representing your   current project location you can write any number  of ways you can write system dot get property of   user.dar that will also get the current location  or you have to provide the complete path of your   file you have to capture the path like this go  to properties and you have to copy this entire   path right so again instead of hardcoding  this I can just capture the current project   location and then I will add the file so dot  is representing current project location dot   slash slash the file name is what body dot Json so  here I'll say body dot Json this is my file First   Step so file object file class we have to import  from java.io packet this comes from java itself   now the f is representing the file so from that  file if you want to read the data we have to use   two special classes one is file data the second  is Json tokener okay so first let us try to read   the data from the file so to read the data from  the file we have to use another class called file   reader representing with FR equal to new file  reader and here we have to pass this F because   we already open the file and representing that  file with the F that F we have to pass now this   particular file data also comes from java dot IO  let me translate file data so file data should   come from java.io so you have to carefully import  the packages guys because same class is available   in multiple packages so if you are imported  incorrect package then it will not work properly   so the file related classes always we have to  import from java.io package okay so import this okay so now we open the file by using file  data and then what we have to do we have to   split that file into different tokens means what  ultimately we have to get the Json format of data   okay ultimately we have to get the Json form of  data so we have another class called file uh Json   tokenizer Json tokenizer token or actually token  app is a class Json tokener I create one variable   called JT equal to new Json token now Json  token and here we have to pass FR as a parameter   so first you open the file we're referring the  file by using F and here we have passed the file   and get the file leader then we have passed  FR then we got the Json token up so this Json   token we have to import from org dot Json org  dot Json import package so now by using this   token now we have to extract the data in the Json  format or in the Json object format so now Json   object we have to use Json object data equal to  new new Json object and here we have to pass JD   Json token Etc so this Json object again you  have to import from org dot Json dot so this   Json object okay I think we have imported in  the incorrect package let me remove this foreign object we have to import from org.json only okay so let me just try to import  once again Json object or J Dot Json yeah correct I think somewhere spelling mistake Json object okay so this is fine so here this will  throw some exception just add that   all right so file we have capture and we open the  file by using file data and then we have passed   into token as a Json tokener and then we have  to pass this Json tokener then finally we got   the Json object so once you get the Json object  we can directly pass and again this approach we   already seen if we have a data in the Json  object format we can directly pass right in   the first approach in the second approach  we have seen that in the second approach   uh here after hash map we have seen so here  we have created Json object so the data is   available in the form of Json object if the data  is available in the Json object we can directly   pass the data after conversion of string  right so same thing we have to repeat here   so once you get the data in the Json object so  this data we are getting from the external file we   are getting this data from the external file right  so now we got the data into the data so this data   we can pass directly here but can we pass data  directly here because there's a Json object data   that is a data is available in the form of  Json object if the data is available in the   Json object we have to convert to string format  okay don't forget this two string should be there   okay so rest of them are exactly the same  so this time we are taking the data from   the external Json file we are taking the  data from the external Json file right so   let me save it and then execute so previous  one I already commented third one now this   is the fourth approach post request body using  external Json file so make sure you are having   this Json file same format same field should  have right so let me close it and then execute okay so it is done and both have been executed  so this is how we can create a request body in   multiple ways all approaches are correct and  all of those all approaches are important   but throughout the project we always follow one  single approach okay and whichever you feel easier   you can just follow the approach hash map or using  logic.json or using pojo class or using external   Json file so by using these four approaches  suppose you have data in the external file so you   don't have any other way you have to follow the  fourth approach if you have data in external files   and the rest of three approaches we have  to create the data and again moreover   uh hash map or 0.json so this org  Json required other dependency   and for rest of them we don't need any other  dependency org.json and external file so these   two are almost the same second one fourth one is  almost the same because in both the approaches   we are using Json Library so here also we  are using same Json Library but only thing   is we are getting the data from external file  that's additional thing but uh second approach   fourth approach is using Json Library same and  the hash map if you have a small set of data   you can go with the hash map nothing wrong in  that but a better to create a pojo class we   will have a separate pojo class and by using pojo  classes we can generate the data so all approaches   are correct all approaches are important depends  on the scenario you can use particular approach Okay so body.json you have to create this file  if you know the Json format how to create the   data in the Json you can create so this sample  data I have shown you right so if you already   have some external files in the Json format you  can use it as part of data or if you don't have   you can create your own data okay so this file you  can keep Anywhere But at the time of reading the   file you have to specify the location correctly  so this location this piece of code is required   and current life so I have saved  this file with my project itself   it is there in my project itself so I am directly  able to read so whatever the files external files   you are creating make sure those files are part  of your project so that tomorrow if you move   this project to somewhere else all the files  will automatically mode along with the project   okay so try these approaches for today and  these are the four approaches I have told   you so try these four approaches for today  and tomorrow we will continue with the other   topics okay and this is more related coding  part so you have to practice more it is not   a UI it is a coding part so code means  what you have to practice multiple times   okay so these are the different ways we can create  a post request and try this today and tomorrow we   will discuss other things okay so I'll stop for  today's session here we'll continue tomorrow
Info
Channel: SDET- QA
Views: 98,293
Rating: undefined out of 5
Keywords: Python, Selenium with Python, Selenium webdriver, Automation, Testing tools, Selenium tutorials, Python tutorials, Manual testing, SDET, Java, selenium with java, big data, hadoop, web services, postman, soapui, rest assured, big data testing, hadoop testing, ETL testing, SQL, Programming, Software Testing material, selenium videos, python videos, automation vidios, jira, jmeter, performance testing, functional testing, QA, QC, Agile testing, SDLC, Automation Step by Step, restapi
Id: xIidl6Iua0o
Channel Id: undefined
Length: 60min 27sec (3627 seconds)
Published: Fri Jan 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.