Pages

Sunday, 30 April 2023

How to Automate GET - POST - DELETE with Rest Assured ?

 



Check below link for question and answers with the code:

Automate GET request & Validate Status Code

In this example, we will send a GET request to the API and check the status code of the response. We expect the status code to be 200, which indicates that the request was successful.

@Test

        //public is access modifier and void as nothing is returned for getusers()

public void getusers()
 
    {
 
// resp will contain the response from the api get method for /api/users?page=2
 
            Response resp = given()
.when().get(“serverURL/api/users?page=2”);
 
//save the actual status code in integer variable, variable name is actualstatuscode

int actualstatuscode = resp.getStatusCode();
 
//print the status code in the console
System.out.println(resp.getStatusCode());

      //print the response body in console
System.out.println(resp.getBody().asString());
 
//validating actual status code that we got from API response with the expected status code 200
assertEquals(200,actualstatuscode);
 
}

 

Now in the above code,  instead of assertEquals(200,actualstatuscode); we can also make use of then() like below: 

.then()
        .statusCode(200);

Import below in your package before using the code and add rest assured/testng jar:

import static io.restassured.RestAssured.given;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
import io.restassured.response.Response;

To create our test, we use the given(), when(), and assertion methods in this code. The given() method constructs the request, the when() method sends it, and the then() method examines the response. The StatusCode() method is used to determine the response’s status code.

 

CodeExplanation
Given()‘Given’ keyword, lets you set a background, here, you pass the request headers, query and path param, body, cookies. This is optional if these items are not needed in the request
When()‘when’ keyword marks the premise of your scenario. For example, ‘when’ you get/post/put something, do something else.
Then()Your assert and matcher conditions go here


 What is the difference between 401 vs 403 ?  Very Imp Interview Question


Validate the response body of a GET request

In this example, we will send a GET request to the API and check the response body. Instead of using then() we will use assertEquals for validation of response body, but then90 can also be used for response body validation.

@Test(description=“Verify status code for GET method-users/2 as 200”)

public static void verifyStatusCodeGET() {

Response resp=given().when().get(“https://reqres.in/api/users”);

System.out.println(resp.path(“total”).toString());

assertEquals(resp.getStatusCode(),200);

assertEquals(resp.path(“total”).toString(),“12”);

}

 

To check the response body, we use the body() method in this code. The JSON path and the expected value are passed on to the body() method. In this example, we inspect the JSON path for the first and second blog posts to ensure that the expected values are present.

Automate a POST request with Rest Assured

In this example, we’ll send a POST request to the API and verify that the response contains the data we’re looking for.

Assuming we have an API endpoint that allows us to create new users. JSON request bodies with the fields name, email, and password are accepted by the endpoint. The endpoint returns a JSON response containing the newly created user’s id, name, email, and password.

Through the creation of the following test, we can leverage Rest Assured to automate the process of creating a new user. As we have previously used assertEquals() to verify the response body, we can demonstrate the process of validating response bodies using then() in this example.

@Test
public void testCreateUser() {
    String requestBody = “{\”name\”:\”John Doe\”,\”email\”:\”johndoe@example.com\”,\”password\”:\”password123\”}”;

    given()
        .contentType(“application/json”)
        .body(requestBody)
    .when()
        .post(“https://example.com/api/users”)
    .then()
        .statusCode(201)
        .body(“name”, equalTo(“John Doe”))
        .body(“email”, equalTo(“johndoe@example.com”));
}

 

In this code, we first create a string representation of a JSON request body. We then use the given() method to set the request’s content type to application/json and the body() method to set the request body.

The POST request is then sent to the specified endpoint using the when() method. To specify the HTTP method and endpoint URL, we use the post() method.

Finally, we validate the response using the then() method. The statusCode() method is used to ensure that the response status code is 201. (indicating that the request was successful and a new user was created). We also use the body() method to verify that the response body contains the expected user name and email fields.

Learn how to create a POJO class to send as a Request Body ==> VERY IMP

Similarly, you can also explore to automate the rest of the API methods like PUT & PATCH

Automate DELETE method with Rest Assured

This is an example of a Rest Assured test that validates the status code for a DELETE request. The test sends a DELETE request to the specified API endpoint and verifies that the response status code is 204 (indicating that the user was successfully deleted).

Here’s a rephrased version of the code with comments explaining each step:

@Test(description=“Verify 204 status code for deleting a user”, groups= {“RegressionSuite”,“B_User”})
public void testDeleteUser() {
    // Send DELETE request to delete a user
    Response response = given()
        .delete(“https://reqres.in/api/users/2”);

    // Verify that the response status code is 204
    assertEquals(response.getStatusCode(), 204);

    // Print message indicating the test passed
    System.out.println(“Test passed: User deleted successfully with status code 204”);
}

 

In this code, we use the given() method to set up the DELETE request and specify the API endpoint to delete a user. We then use the delete() method to specify the HTTP method and endpoint URL.

We send the request using given().delete() and store the response in the response variable. We then use the assertEquals() method to verify that the response status code is 204.


*******************************************************************
For any doubts or career guidance from me, reach out here : https://topmate.io/sidharth_shukla

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

Do remember that knowing Linux is one of the most important aspect to work as an SDET.

Basic Linux Commands for Automation QA


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

TOP 15 BDD - CUCUMBER Interview Q&A


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

✍️AUTHORLinkedIn Profile

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

Learn (API-Microservice)Testing+ Selenium UI Automation-SDET with Self Paced Videos prepared by FAANG employees and LIVE Doubt Session 

SDET TRANING VIDEOS AVAILABLE with Live Doubt Session(course-1 below,API TRaining Videos With Class Notes and Coding Set) and (API++Mobile+UI, course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122. 
This includes classnotes, 300+ interview questions, 3 projects, Java Coding question set for product companies along with career guidance from FAANG employees for Automation and SDET.

For more details whatsapp : https://lnkd.in/dnBWDM33

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



SeleniumWebdriver Automation Testing Interview Questions:

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

API Testing Interview Question Set:

https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html

DevOps Interview Q&A:

https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html 

Kubernetes Interview Question Set

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

Docker Interview Question Set

https://automationreinvented.blogspot.com/Docker

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/2021/09/top-40-git-interview-questions-and.html

Coding Interview Question Set:

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

Mobile Testing Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

Python Interview Question Set for QAE - SDET - SDE:

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

Monday, 24 April 2023

How to setup GIT repository in Intelij ?

 



Pipeline Script Fundamentals

Click Here:   DevOps Related Posts

 Check GIT commands for Interview preparation SET-1:

GIT Interview Question 1 to 11

Check GIT commands for Interview preparation SET-2:

GIT Interview Question 12 to 21

Check GIT commands for Interview preparation SET-3:

GIT Interview Question 21 to 30


Set up Git repository

When you clone an existing Git repository or put an existing project under Git version control, IntelliJ IDEA automatically detects if git is installed on your computer. If the IDE can't locate the git executable, it suggests downloading it.

IntelliJ IDEA supports Git from the Windows Subsystem for Linux 2 (WSL2), which is available in Windows 10 version 2004.

If Git is not installed on Windows, IntelliJ IDEA searches for Git in WSL and uses it from there. Also, IntelliJ IDEA automatically switches to Git from WSL for projects that are opened when you use the \\wsl$ path.

If you need to manually configure IntelliJ IDEA to use Git from WSL, go to the Version Control | Git page of the IDE settings ⌘ ,, click the Browse icon in the Path to Git executable field and select Git from WSL via the \wsl$ path, for example, \\wsl$\debian\usr\bin\git.


Check out a project from a remote host (clone)


IntelliJ IDEA allows you to check out (in Git terms clone) an existing repository and create a new project based on the data you've downloaded.

  • From the main menu, select Git | Clone, or, if no project is currently opened, click Get from VCS on the Welcome screen.

  • In the Get from Version Control dialog, specify the URL of the remote repository you want to clone, or select one of the VCS hosting services on the left.
    If you are already logged in to the selected hosting service, completion will suggest the list of available repositories that you can clone.


  • Click Clone. If you want to create a project based on the sources you have cloned, click Yes in the confirmation dialog. Git root mapping will be automatically set to the project root directory.
    If your project contains submodules, they will also be cloned and automatically registered as project roots.

  • When you import or clone a project for the first time, IntelliJ IDEA analyzes it. If the IDE detects more than one configuration (for example, Eclipse and Gradle), it prompts you to select which configuration you want to use.


    If the project that you are importing uses a build tool, such as Maven or Gradle, we recommend that you select the build tool configuration.


    Select the necessary configuration and click OK.
  • The IDE pre-configures the project according to your choice. For example, if you select Gradle, IntelliJ IDEA executes its build scripts, loads dependencies, and so on.

Put an existing project under Git version control


You can create a local Git repository based on the existing project sources.


Associate the entire project with a single Git repository

  • Open the project that you want to put under Git.

  • Press ⌃ V to open the VCS Operations Popup and select Enable Version Control Integration.

    Alternatively, from the main menu, select VCS | Enable Version Control Integration.

  • Choose Git as the version control system and click OK.

  • After VCS integration is enabled, IntelliJ IDEA will ask you whether you want to share project settings files via VCS. You can choose Always Add to synchronize project settings with other repository users who work with IntelliJ IDEA.

Associate different directories within the project with different Git repositories

  • Open the project that you want to put under Git.
  • From the main menu, choose VCS | Create Git Repository.
  • In the dialog that opens, specify the directory where a new Git repository will be created.
    Git does not support external paths, so if you choose a directory that is outside your project root, make sure that the folder where the repository is going to be created also contains the project root.
  • If you are creating multiple Git repositories inside the project structure, repeat the previous steps for each directory.

After you have initialized a Git repository for your project, you need to add project files to the repository.


Add files to the local repository

  • In the Commit tool window ⌘ 0, expand the Unversioned Files node.

  • Select the files you want to add to Git or the entire changelist and press ⌥ ⌘ A or choose Add to VCS from the context menu.
  • You can also add files to your local Git repository from the Project tool window: select the files you want to add, and press ⌥ ⌘ A or choose Git | Add from the context menu.

When Git integration is enabled in your project, IntelliJ IDEA suggests adding each newly created file under Git, even if it was added from outside IntelliJ IDEA. You can change this behavior in the Version Control | Confirmation page of the IDE settings ⌘ ,. If you want certain files to always remain unversioned, you can ignore them.



If you attempt to add a file that's on the .gitignore list, IntelliJ IDEA will suggest force adding it. Clicking Cancel in the confirmation dialog only cancels force adding ignored files - all other files will be added to the Git repository.



*******************************************************************
For any doubts or career guidance from me, reach out here : https://topmate.io/sidharth_shukla

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

These commands cover a range of basic UI interactions and are a good starting point for building automated tests with Cypress.

Do remember that knowing Linux is one of the most important aspect to work as an SDET.

Basic Linux Commands for Automation QA


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

TOP 15 BDD - CUCUMBER Interview Q&A


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

✍️AUTHORLinkedIn Profile

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

Learn (API-Microservice)Testing+ Selenium UI Automation-SDET with Self Paced Videos prepared by FAANG employees and LIVE Doubt Session 

SDET TRANING VIDEOS AVAILABLE with Live Doubt Session(course-1 below,API TRaining Videos With Class Notes and Coding Set) and (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122. 
This includes classnotes, 300+ interview questions, 3 projects, Java Coding question set for product companies along with career guidance from FAANG employees for Automation and SDET.

For more details whatsapp : https://lnkd.in/dnBWDM33

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

SeleniumWebdriver Automation Testing Interview Questions:

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

API Testing Interview Question Set:

https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html

DevOps Interview Q&A:

https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html 

Kubernetes Interview Question Set

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

Docker Interview Question Set

https://automationreinvented.blogspot.com/Docker

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/2021/09/top-40-git-interview-questions-and.html

Coding Interview Question Set:

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

Mobile Testing Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

Python Interview Question Set for QAE - SDET - SDE:

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