Posts

Showing posts with the label API Testing

Beautify JSON and directly copy JSON Path using this chrome extension

Image
Beautify JSON Generally the JSON that we get when we hit an API is in raw format and difficult to read as follows { "error" : false , "category" : "Programming" , "type" : "single" , " joke" : "Two C strings walk into a bar.\nThe bartender asks \ "What can I get ya?\"\nThe first string says \"I'll have a gin and tonic. \"\nThe second string thinks for a minute, then says \"I'll take a tequila sunriseJF()#$JF(#)$(@J#()$@#())!*FNIN!OBN134ufh1ui34hf9813f8h8384h981h3984h5F!##@ \"\nThe first string apologizes, \"You'll have to excuse my friend, he's not nu ll-terminated.\"" , "flags" :{ "nsfw" : false , "religious" : false , "political" : false , "racist" : false , "sexist" : false , "explicit" : false }, "id" : 28 , "safe" : true , "lang" : "en" } Either...

Update values in Post Data using Json Path in Rest Assured

Image
In the previous post we studied about how to validate json schema in Rest Assured. In this post we will study about how to update values in Post Data (JSON) using Json Path in Rest Assured. When we have multiple dynamic values in POST Data which will change every time  then we need to read those values from previous request and use those values to update the POST Data (JSON). This is specifically used when the hardcoded values in JSON Data like specific ID's won't work. For example in following POST Data : { "id" : 1 , "name" : "ashwin" , "surname" : "karangutkar" , "details" :{ "City" : "Mumbai" }} If I need to change City then Json Path that we will use is details.City . Before having a glance at code, we will need to import  Jayways JsonPath .  Following is the maven import: <!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path --> <depende...

Validate JSON Schema using Rest Assured

Image
JSON Schema is a powerful tool for validating the structure of JSON data or your JSON Response. JSON Schema itself is written in JSON. It is data itself, not a computer program. It’s just a declarative format for “describing the structure of other data”.  The JSON document being validated or described we call the   instance , and the document containing the description is called the   schema . JSON Schema validation is done to check whether the response received is correct or is structured according to the defined schema for your project.  Let's loo k at an example of JSON Schema validation using Rest Assured. Here we are making use of Google Search API that we studied in  Testing GET Requests and their Responses using Rest Assured . I would highly recommend reading previous posts before reading this one. Lets us create a JSON File where we will define the JSON Schema for our response ( JSON Schema of a Response that we will get when...

Comparing JSON responses using JsonAssert Library - Lenient Mode

Image
  In one of the earlier post, we studied about Extracting a JSON Response with RestAssured ,  before following this tutorial, I would recommend reading the previous blogs.  I would assume that everyone following this tutorial has knowledge of Java and TestNG. If you need help then please follow tutorials on   Java   and   TestNG . In this post, we will learn about comparing JSON Responses using JSONAssert . Once the response is extracted we will store it into Response class and later verify the entire response using a single assert statement instead of comparing specific attributes. The disadvantage of directly comparing the response as string without JSONAssert is as follows: If we compare two jsons by simply comparing the json string then it fails to let us know where exactly the comparison has failed. It will also mark json as failed if the order of array elements in json is different.  However if use JSONAssert Library, it will  let us know th...

Comparing JSON responses using JsonAssert Library - Strict Mode

Image
  In one of the earlier post, we studied about Comparing JSON responses using JsonAssert Library - Lenient Mode , before following this tutorial, I would recommend reading the previous blogs.  I would assume that everyone following this tutorial has knowledge of Java and TestNG. If you need help then please follow tutorials on Java and TestNG .   In this post, we will learn about using the Strict Mode provided by the  JSONAssert  Library . In case of Lenient mode if the order of elements in an array are not same then still the test will pass. In case of Strict mode  if the order of elements in an array are not same then the test will fail. We will be using  findplacefromtext  Google Map API. Follow the create API Key  Article  for generating your key. Now let's look at an example where we will search for a particular Railway Station in Mumbai City and then assert the expected response present in  expectedResponse.txt  file to ...

Verifying File Download using Rest Assured.

Image
In the previous post, we studied about f inding broken links using Rest Assured . In this post, we will learn about verifying file download using Rest Assured. We will first download the file for which we will perform the verification of size. We will download  rest-assured-3.0.5-dist.zip   file   from Rest Assured Download   Section . We will save the file inside our eclipse project.  Now let us look at an example of verifying file download using Rest Assured: import org.testng.Assert ; import org.testng.annotations.Test ; import java.io.File ; import static io . restassured . RestAssured .*; public class GetRequestTest { @Test public void verifyFileDownload () { int fileSize ; File filePath = new File ( System . getProperty ( "user.dir" )+ "/resources/rest-assured-3.0.5-dist.zip" ); fileSize = ( int ) filePath . length (); System . out . println ( "The actual value is " + f...

Popular posts from this blog

Using RequestSpecBuilder in Rest Assured ( Code Reuse )