Sunday, 25 April 2021

How to read String, List and Map in JSON response from API ?

 Check the below link for question and answers with code:

 


@Test(description="Verify status code for GET method-users/2 as 200",groups="SmokeSuite")

public static void verifyResponsejson() throws IOException, org.json.simple.parser.ParseException {

ExtentReport.extentlog = ExtentReport.extentreport.startTest("Execute post method",

" validate post method");

Response resp=given().when().get("https://reqres.in/api/users?page=2");

System.out.println(resp.path("total").toString());

assertEquals(resp.getStatusCode(),200);

 

assertEquals(resp.path("total").toString(),"12");

 

List<String> expected = new ArrayList<String>();

 

List<String> jsonResponse = resp.jsonPath().getList("data");

     System.out.println(jsonResponse.size());

     System.out.println("The number of data in the list is : " + jsonResponse.size());

     assertEquals(jsonResponse.size(),6);

     String usernames = resp.jsonPath().getString("data[1].email");

     System.out.println("Email is : " + usernames);

//   //  List<String> jsonResponses = resp.jsonPath().getList("data");

//   //  System.out.println(jsonResponses.get(0));

     Map<String, String> company = resp.jsonPath().getMap("data[2]");

     System.out.println(company);

     List<String> tset = new ArrayList<>(company.keySet());

     for (int j=0;j<tset.size();j++) {

    System.out.println("The keys for map in data is : "+ tset.get(j));

     }

     System.out.println("Fetch firtsname using map and get: " + company.get("first_name"));

   //  List<Map<String, String>> companies = resp.jsonPath().getList("data");

   //  System.out.println(companies.get(0).get("first_name"));

    List<String> ids = resp.jsonPath().getList("data.email");

//     Collections.sort(jsonResponse);

//     Collections.sort(expected);

//     assertEquals(jsonResponse,expected);

     for(String i:ids)

     {

    System.out.println(i);

        if (i.contentEquals("lindsay.ferguson@reqres.in")) {

        System.out.println("lindsay.ferguson is present in the response body");

        }

     }

}


*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions



What is Map in Java? How to create and run loop in Map and Set?

 




  • Map is an interface
  • Part of Collection
  • Key and value pair
  • Usage: Search, edit and delete elements respective to key
  • Map doesn't allow duplicate keys.
  • Hashmap will allow null keys and values
  • Methods: getKey(), getValue(), entrylist


CODE to run loop with Map and Set?

public static void listcheck() {

 

   Map<String,String> hmap= new HashMap<String,String>();

   hmap.put("urigetuser","https://reqres.in/api/users?page=2");

   hmap.put("first_name","janet");

   

   Set<String> hset = new HashSet<String>();

   Set<Entry<String, String>> esets = hmap.entrySet();

   for (Map.Entry<String, String> ent : esets) {

   System.out.print(ent.getKey() + ent.getValue());

   }

 }


*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions




Saturday, 24 April 2021

How to Create POJO class? How to convert json to pojo?










Check the below link for question and answers with code:

Top API Interview Question 1-10

 


In the below pojo structure name is a string, jobs are a list of string, and cityModels is a map list. So for cityModels we need to create a separate POJO class as we have done below.

POJO Structure:

{

"name": "morpheus", "jobs": ["tester","developer","support"] "cityModels":[ { "Name":"bangalore", "Temperature":"30" }, { "Name":"delhi", "Temperature":"40" } ] }

POJO CLASS:


package pojo;


import java.util.List;


public class pojoTry {

private String name; // this is for the name filed in pojo

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

private List<String> jobs; // this is for jobs filed in pojo

public List<String> getJobs() {

return jobs;

}

public void setJobs(List<String> jobs) {

this.jobs = jobs;

}

private List<cityModel> citymodels; //refer the city model class below, we have created separate pojo class for it as it is a list of map

public List<cityModel> getCitymodels() {

return citymodels;

}

public void setCitymodels(List<cityModel> citymodels) {

this.citymodels = citymodels;

}


}


CityModel class:

package pojo;


public class cityModel {

private String name;

public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}


public String getTemperature() {

return temperature;

}


public void setTemperature(String temperature) {

this.temperature = temperature;

}


private String temperature;


}


*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions



How to Automate POST/PUT/Patch method with Rest Assured?



Check below link for question and answers with code:

Top API Interview Question 1-10

 

Let's learn about PUT, POST and PATCH.

PUT, PATCH, and POST are HTTP methods used to interact with a REST API.

PUT:
It is used to update a resource completely. It requires the client to send the complete resource representation. Example: Updating a user's profile information.

PATCH:
It is used to partially update a resource. It requires the client to send only the changes. Example: Updating a user's password.

POST:
It is used to create a new resource. It requires the client to send the representation of the resources to be created. Example: Creating a new user account.

Note:
For RESTful APIs, PUT method should only be used if the client can update the entire resource, while PATCH can be used if the client can update only a part of the resource.
The POST method should be used to create a new resource.


- First option: By using the entire JSON in an external file



- Second way: Using POJO class


First way: By using the entire JSON in an external file

1. IOUtils class will provide a method toString()

2. tostring will take two input param: Inputstream, encoding type

3. Fileinputstream will take param as File

4. File will require input param as the path where we kept .json file


@Test(description= "Automate post method for users")

public static void postmethodjson() throws IOException {


FileInputStream file = new FileInputStream(new File (System.getProperty("user.dir")+"\\Resources\\TestData\\testdata.json"));

Response resp = given().header("Content-Type","application/json").

body(IOUtils.toString(file, "UTF-8")).

when().post("https://reqres.in/api/users");

assertEquals(resp.getStatusCode(),201);

System.out.println("The status code for post method is : " + resp.getStatusCode() );

System.out.println("The response body of the post method is : " + resp.getBody().asString());

assertEquals(resp.path("job"),"leader");

}

Second way: Using pojo class

   How to create POJO class

How to automate with pojo class?

@Test

public static void postmethodwithpojoclass() {

String job = "tester";

String name = "automation";

pojoClass obj = new pojoClass(name,job);

Response resp = given().header("Content-Type","application/json").

body(obj).

when().post("https://reqres.in/api/users");

assertEquals(resp.getStatusCode(),201);

System.out.println("The status code for post method is : " + resp.getStatusCode() );

System.out.println("The response body of the post method is : " + resp.getBody().asString());

assertEquals(resp.path("job"),job);

assertEquals(resp.path("name"),name);

}

How to Create Fake-API which can be used for Testing in Postman or Rest Assured? Very Useful for real-time practice

 Ans: Steps with Screenshot to Create API from Scratch - Click Here



=========PATCH WITH POJO=======

@Test(groups="RegressionSuite")

pojoClass pjc = new pojoClass("morpheus", jobname);

Response resp = given().header("Content-Type","application/json").

          // body(requestParams.toString()).

            body(pjc).

           

    when().

            patch("https://reqres.in/api/users/2");

    System.out.println(resp.getBody().asString());

    assertEquals(resp.path("job"),jobname);

   

}


******If you are preparing for API Testing Interview then do refer the below list of Interview Q&A, which is prepared by industry leaders with 11+ years of experience: API Testing Interview Q&A **


https://automationreinvented.blogspot.com/2021/01/java-interview-questions-for.html

What are the status codes you have come across in your API testing project?

Ans: Click Here For Status Codes


Check below link for question and answers with code:

Top API Interview Question 1-10

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions

Saturday, 17 April 2021

Install JDK and set up Java home path for macOS and Windows?





WHERE to DOWNLOAD JDK:

https://www.oracle.com/java/technologies/javase-jdk16-downloads.html


Install JDK

Installing the JDK on macOS

To install the JDK on macOS:

1. Download the JDK .dmg file, jdk-15.interim.update.patch_osx-x64_bin.dmg from Java SE Downloads page.

Click Accept License Agreement.

2. From either the browser Downloads window or from the file browser, double-click the .dmg file to start it.

A Finder window appears that contains an icon of an open box and the name of the .pkg file.

3. Double-click the JDK 15.pkg icon to start the installation application.

The installation application displays the Introduction window.

4. Click Continue.

The Installation Type window appears.

5. Click Install.

A window appears that displays the message: Installer is trying to install new software. Enter your password to allow this.

6. Enter the Administrator user name and password and click Install Software.

The software is installed and a confirmation window is displayed.

After the software is installed, you can delete the .dmg file if you want to save disk space.

Add java home path in MacOs

Open ~/.bash_profile in any text editor and add:

export JAVA_HOME=$(/Library/Java/JavaVirtualMachines/jdk-11.0.10.jdk/Contents/Home)

Save and close the file.

Open a Terminal and run the source command to apply the changes:

source ~/.bash_profile

Now we can check the value of the JAVA_HOME variable:

echo $JAVA_HOME


Install jdk in Windows:
https://docs.oracle.com/javase/10/install/installation-jdk-and-jre-microsoft-windows-platforms.htm#JSJIG-GUID-371F38CC-248F-49EC-BB9C-C37FC89E52A0

Set Java home in windows-10:

Windows 10 and 8

1. Open Search and type advanced system settings

2. In the shown options, select the View advanced system settings link

3. Under the Advanced tab, click Environment Variables

4. In the System variables section, click New (or User variables for single user setting)

5. Set JAVA_HOME as the Variable name and the path to the JDK installation as the Variable value and click OK

6. Click OK and click Apply to apply the changes 







Sunday, 11 April 2021

How to create test cases for API Testing on GET/POST/PUT/PATCH/DELETE?




Status Codes in real-Time:

https://automationreinvented.blogspot.com/2019/03/what-are-most-used-api-status-codes.html


STATUS CODES TYPES:

1xx-Information

2xx-Success

3xx-Redirection

4xx- Client Error

5xx- Server Error

To know more about status codes check :https://httpstatuses.com/


Prac: https://reqres.in/

Target : GET - https://reqres.in/api/users?page=2

Test Cases on GET:

1. Scenario Related to Query param (create test data with both key(page) and value(2))

TEST API ENDPOINT: GET - https://reqres.in/api/users?page=2

        https://reqres.in/api/users?page=00002

https://reqres.in/api/users?page=teststring

https://reqres.in/api/users?page= 1

https://reqres.in/api/users?page=1.45666777

https://reqres.in/api/users?page=@#$%

https://reqres.in/api/users?test=1

https://reqres.in/api/users?page=2test

https://reqres.in/api/users?page=*2*

https://reqres.in/api/users?"page"=2

https://reqres.in/api/users?page=2//

https://reqres.in/api/users?page=2\\

https://reqres.in/api/users?page=//2//

2. Scenario related to server address

        http://reqres.qa/api/users?page=2

http://retest.in/api/users?page=2

3. Scenario related to end point

   https://reqres.in/test/users?page=//2//

   https://reqres.in/API/users?page=2 

   

4. Scenario related to Protocol

   http://reqres.in/api/users?page=2

   https://reqres.in/api/users?page=2

   Try to test with both https and http protocol   


Test Cases on POST:

Let you have a POST method with endpoint as:  https://reqres.in/api/register

and request body as below:

{

    "email": "eve.holt@reqres.in",

    "Password": "pistol"

}


Now lets create some of the test scenarios to check on Request Body format for the POST method:

1.Try with wrong password

  {

    "email": "eve.holt@reqres.in",

    "password": "tdstcwcdygwec544"

  }

O/P:200(coz its a prac api)

2.

{

    "EMAIL": "eve.holt@reqres.in",

    "password": "tdstcwcdygwec544"

}

O/P:400 status code

3.

{

    "email": "eve.holt@reqres.in"   

}

O/P: 400 status code

4. Try to change the domain

{

    "email": "eve.holt@reqres.com",

    "password": "tdstcwcdygwec544"

}

O/P: 400

5.

{

    "email": "eve.holtwww@reqres.in",

    "password": "tdstcwcdygwec544"

}

O/P: 400

6.TRy to change the key name from password to test

{

    "email": "eve.holt@reqres.in",

    "test": "pistol"

}

Status: 400

7.

{

    "username": "eve.holt@reqres.in",

    "password": "tdstcwcdygwec544"

  }

O/P:200


401 vs 403:

https://automationreinvented.blogspot.com/2020/09/difference-between-api-status-code-401.html

Check below link for question and answers with code:

Top API Interview Question 1-10

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions


Saturday, 10 April 2021

API Testing Interview question on Postman? what are API methods?

 


This is the fourth Set of API Testing Interview Question, for Set-01, 02 &03 refer below links.
 
Check below link for question and answers with code:

Top API Interview Question 1-10

 

1. What tool to use for API testing?

2. How to use Postman?

3. Collections and parameters in Postman?

4. Where to check status code and headers in Postman?

5. How to set global variable in Postman?

6. How to use authentication and authorization in Postman?

7. Check response meesage in Postman?t

8. How to download the response of a request using Postman?

9 How to use collection runner?

10. Cookies and Headers also we need to validate apart from status code and response body?

11. What type of status codes you have worked ?

12. Which one has the higher priority in Postman? A global variable or a local variable?

13: Whar are the types of workspace in Postman?

14. How to run the requests in collection for multiple times?

Ans: got to Collection Runner and define the number of times require to execute the requests in "Iterations" inputbox

15. How to give delay between iteration cycle in POstman while running collection?

Ans: Use the Delay inputbox, the default time unit is in ms.

16. What are the methods in API you have used?

Ans: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS 

17. How you get request details or end points in your project and test it?

Ans: Swagger or confluence and append your server address with request in Postman.

18. How to set up proxy in Postman?

Ans: Go to Settings--> Proxy-->Add a custom proxy configuration-->Provide Proxy Details

19. How to add certificates in Postman?

Ans: Go to Settings--> Certificates -->Import-->Add Certificate

20. What variables we have in POSTMAN?

Ans: Global and Environment


Download Postman:

https://www.postman.com/downloads/


API documentation:

- Excel Sheet

- Confluence Page

- Swagger

- URL


API Methods:

GET/POST/PUT/PATCH/DELETE-HEAD/OPTIONS


Story:TO EXPLAIN API METHODS

James Harper-Son-Charles H.(10)

Delhi-April

Bangalore-April 10th


Day-1:@school

Registration: (POST)

Name:

Age:

Address:

PhoneNumber:


Day2:@School

Charles

1. Admin get information about Charles (GET)

2. Data is wrong

Day3:J+C (PUT)

Name:

Age:

Address:

PhoneNumber:

Day4-----

Day-30 J+C (PATCH)

Address:


1year:

Bangalore to Delhi (DELETE)

SLC


HEAD=Get BUT without response body

OPTIONS

Components of an API:

https://reqres.in/api/users?page=2

Https:protocol

Reqres.in=server address/domain

/api/users?page=2: endpoint

www.reqres.in=host


https://learning.postman.com/docs/getting-started/creating-your-first-workspace/


Check below link for question and answers with code:

Top API Interview Question 1-10

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions


All Time Popular Posts

Most Featured Post

Introduction to Web Testing with Playwright

  Introduction to Web Testing with Playwright 📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUG...