How to test POST Request using Rest Assured

HOME

In the last tutorial, I explained How to test GET Request using Rest Assured. In this tutorial, I will automate a POST Request using Rest Assured. I will verify the status code, line of Status, and content of the Response. To set up a basic Rest Assured Maven Project, click here and Gradle project, click here.

Add the below-mentioned dependencies to the pom.xml. The latest dependency can be downloaded from here.

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
</dependency>
 
<dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <version>5.5.1</version>
      <scope>test</scope>
</dependency>

What is the POST Method?

An HTTP POST method is used to create a new resource in the collection of resources. The request body is passed as a JSON/XML or in a different format. If a resource is created successfully by the endpoint or server, it returns a status code 201(Created). It also provides a Location header with a link to the newly-created resource with the 201 HTTP status. It may return 200 (OK) and 204 (No Content) status code as well, based on how the API is developed.

POST is neither safe nor idempotent. It is therefore recommended for non-idempotent resource requests. Making two identical POST requests will most result in two resources containing the same information.

Below are the steps to test a POST Request using Rest Assured:

Step 1 Specify the base URL to the RESTful web service using the RestAssured class.

RestAssured.baseURI = "https://dummy.restapiexample.com/api/v1/create";

Step 2  Every Request in the Rest-Assured library is represented by an interface called RequestSpecification. This interface allows modifying the request, like adding headers or adding authentication details. Use the RestAssured class to generate a RequestSpecification.

requestSpecification = RestAssured.given();

RequestSpecification is imported from package:

import io.restassured.specification.RequestSpecification;

Step 3 – Set the content type. This step specifies the format in which the request payload will be sent to the server. Here, the Content-Type is JSON.

requestSpecification.contentType(ContentType.JSON);

contentType is imported from restassured.http package:

import io.restassured.http.ContentType;

Step 4 Pass Request Body as String.

requestSpecification.body(jsonString);

Step 5 – Send the POST request to the server. Then receive the response of the request made by REST Assured. This response contains every detail returned by hitting request i.e. response body, response headers, status code, status lines, cookies, etc. The response is imported from package:

import io.restassured.response.Response;

Step 6 To validate a response like status code or value, we need to get the reference of type ValidatableResponse

ValidatableResponse is an interface. A validatable response to a request made by, REST Assured. ValidatableResponse is imported from package:

import io.restassured.response.ValidatableResponse;

PrettyPrint() – It prints the response body if possible and returns it as a string. Pretty printing is possible for content-types JSON, XML, and HTML.

Below is the example of testing a POST request in Non-BDD format. I have used ValidatableResponse for the assertion of status. It is also used for the status line and body of the Response.

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;

public class POST_NonBDDDemo {

    RequestSpecification requestSpecification;
    Response response;
    ValidatableResponse validatableResponse;

    @Test
    public void verifyStatusCode() {

        String jsonString = "{\"name\":\"newapitest\",\"salary\":\"4000\",\"age\":\"29\"}";

        RestAssured.baseURI = "https://dummy.restapiexample.com/api/v1/create";

        // Create a request specification
        requestSpecification = RestAssured.given();

        // Setting content type to specify format in which request payload will be sent.
        requestSpecification.contentType(ContentType.JSON);

        // Adding body as string
        requestSpecification.body(jsonString);

        // Calling POST method
        response = requestSpecification.post();

        // Let's print response body.
        String responseString = response.prettyPrint();

        /*
         * To perform validation on response, we need to get ValidatableResponse type of
         * response
         */
        validatableResponse = response.then();

        // Check status code
        validatableResponse.statusCode(200);

        // It will check if status line is as expected
        validatableResponse.statusLine("HTTP/1.1 200 OK");

        // Check response body - name attribute
        validatableResponse.body("data.name", equalTo("newapitest"));

        // Check response body - message attribute
        validatableResponse.body("message", equalTo("Successfully! Record has been added."));

    }
}

The below image shows the test result of the above test.

Test implemented in BDD Format

import static org.hamcrest.Matchers.equalTo;

2. given is a static import from package:

import static io.restassured.RestAssured.given;

Below is an example of a BDD Test.

import io.restassured.http.ContentType;
import io.restassured.response.ValidatableResponse;
import org.junit.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;

public class POST_BDDDemo {
    
    ValidatableResponse validatableResponse;

    @Test
    public void createUser() {

        String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";

        // GIVEN
        validatableResponse = given()
                .baseUri("https://dummy.restapiexample.com/api")
                .contentType(ContentType.JSON)
                .body(json)

                // WHEN
                .when()
                .post("/v1/create")

                // THEN
                .then()
                .assertThat().statusCode(200).body("data.name", equalTo("apitest"))
                .body("message", equalTo("Successfully! Record has been added."));

        System.out.println("Response :" + validatableResponse.extract().asPrettyString());
    }

}

The below image shows the test result of the above test.

String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";

.baseUri("https://dummy.restapiexample.com/api")
.contentType(ContentType.JSON)
.body(json)

".assertThat().statusCode(200)"
.body("data.name", equalTo("apitest"))
.body("message", equalTo("Successfully! Record has been added."))

The above tests can be used in both Maven and Gradle projects.

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

How to test GET Request using Rest Assured

HOME

In the last tutorial, I explained the Setup of the REST Assured Maven Project In Eclipse IDE. In this tutorial, I will automate a GET Request. I will verify the status code, line of Status, and content of the Response.

RestAssured is a class that consists of many static fields and methods. It supports POST, GET, PUT, DELETE, HEAD, PATCH, and OPTIONS requests and verifies the response to these requests.

 <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
</dependency>

<dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <version>5.5.5</version>
      <scope>test</scope>
</dependency>

Below are the steps to test a GET Request using Rest Assured:

Step 1 Specify the base URL to the RESTful web service using the RestAssured class.

RestAssured.baseURI = "http://dummy.restapiexample.com/api/v1/employees";

Step 2 Every Request in the Rest-Assured library is represented by an interface called RequestSpecification. This interface allows modification of the request, like adding headers or adding authentication details.

requestSpecification = RestAssured.given();

RequestSpecification is imported from the package:

import io.restassured.specification.RequestSpecification;

Step 3 Send the request to the server and receive the response to the request made by REST Assured. This response contains every detail returned by hitting request i.e. response body, response headers, status code, status lines, cookies, etc.

response = requestSpecification.get();

The response is imported from package:

import io.restassured.response.Response;

Step 4 To validate a response like status code or value, we need to acquire a reference. This reference should be of type ValidatableResponse. ValidatableResponse is an interface. A validatable response to a request made by, REST Assured. ValidatableResponse is imported from the package:

import io.restassured.response.ValidatableResponse;

PrettyPrint() It prints the response body if possible and returns it as a string. Pretty printing is possible for content-types JSON, XML, and HTML.

Below is an example of creating a test in Non-BDD format. I have used ValidatableResponse for the assertion of the status. It is also used for the status line of the Response.

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.junit.Test;

public class Get_NonBDDDemo {
    RequestSpecification requestSpecification;
    Response response;
    ValidatableResponse validatableResponse;

    @Test
    public void verifyStatusCode() {

        RestAssured.baseURI = "http://dummy.restapiexample.com/api/v1/employees";

        // Create a request specification
        requestSpecification = RestAssured.given();

        // Calling GET method
        response = requestSpecification.get();

        // Let's print response body.
        String resString = response.prettyPrint();
        System.out.println("Response Details : " + resString);

        /*
         * To perform validation on response, we need to get ValidatableResponse type of
         * response
         */
        validatableResponse = response.then();

        // Get status code
        validatableResponse.statusCode(200);

        // Check status line is as expected
        validatableResponse.statusLine("HTTP/1.1 200 OK");

    }
}

If you don’t want to use ValidatableResponse for the assertion, you can use Response from io.restassured .response to get the status code and status line, which are asserted using JUnit.Assert.

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.junit.Assert;
import org.junit.Test;

public class Get_NonBDDResponseDemo {
    RequestSpecification requestSpecification;
    Response response;

    @Test
    public void verifyStatusCode() {

        RestAssured.baseURI = "http://dummy.restapiexample.com/api/v1/employees";

        // Create a request specification
        requestSpecification = RestAssured.given();

        // Calling GET method
        response = requestSpecification.get();

        // Let's print response body.
        String resString = response.prettyPrint();
        System.out.println("Response Details : " + resString);

        // Get status line
        String statusLine = response.getStatusLine();
        Assert.assertEquals(statusLine, "HTTP/1.1 200 OK");

        // Get status code
        int statusCode = response.getStatusCode();
        Assert.assertEquals(statusCode, 200);

    }
}

The output of the above program is

Below is the test implemented in BDD Format. In this test, I am asserting the data of Employee of Id 2. I have validated the name of the employee as well as the response message.

1. equalTo is used for assertion, and is imported from a static hamcrest package:

import static org.hamcrest.Matchers.equalTo;

2. given is a static import from package:

import static io.restassured.RestAssured.given;

import org.junit.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;

public class Get_BDDDemo {

    @Test
    public void verifyUser() {

        // Given
        given()

                // When
                .when()
                .get("http://dummy.restapiexample.com/api/v1/employee/2")

                // Then
                .then()
                .statusCode(200).statusLine("HTTP/1.1 200 OK")

                // To verify booking id at index 3
                .body("data.employee_name", equalTo("Garrett Winters"))
                .body("message", equalTo("Successfully! Record has been fetched."));
    }

}

    given
    
    .when()
    .get("http://dummy.restapiexample.com/api/v1/employee/2")
    
    .then()
    .statusCode(200)
    .statusLine("HTTP/1.1 200 OK")
    
    .body("data.employee_name", equalTo("Garrett Winters"))
    .body("message", equalTo("Successfully! Record has been fetched."));
    

    That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

    Rest Assured Tutorials

    HOME

    RestAssured is a Java-based library that is used to test RESTful Web Services. REST-assured was designed to simplify the testing and validation of REST APIs. It takes influence from testing techniques used in dynamic languages such as Ruby and Groovy.

    Chapter 1 Assertion of JSON in Rest Assured using Hamcrest
    Chapter 2 Extraction from JSON in Rest Assured – JsonPath
    Chapter 3 How to perform multiple assertions in Rest Assured? 
    Chapter 4 How to validate JSON body in Rest Assured?
    Chapter 5 Compare JSON Objects using JSONAssert Library
    Chapter 6 Compare JSON Arrays using JSONAssert Library
    Chapter 7 How to Read JSON with JSON.simple – NEW
    Chapter 8 How to create and write to JSON with JSON.simple – NEW

    JSON Handling and manipulation

    Category 10: XML Manipulations

    XML Handling and manipulation

    Gradle

    Chapter 1 Setup Basic REST Assured Gradle Project In Eclipse IDE

    Frameworks

    Chapter 1 Integration of REST Assured with TestNG
    Chapter 2 Integration of REST Assured with JUnit4
    Chapter 3 Integration of REST Assured with JUnit5
    Chapter 4 Serenity BDD with Cucumber and Rest Assured
    Chapter 5 Serenity BDD with Cucumber and Rest Assured in Gradle
    Chapter 6 How To Create Gradle Project with Cucumber to test Rest API
    Chapter 7 Rest API Test in Cucumber and JUnit4
    Chapter 8 API Automation with REST Assured, Cucumber and TestNG

    Integration of REST Assured with JUnit5

    HOME

    In this tutorial, I’ll create a Test Framework for the testing of REST API using REST Assured and JUnit5 as the test framework.

    What is Rest Assured?

    Rest Assured enables you to test REST APIs using Java libraries and integrates well with Maven/Gradle. REST Assured is a Java library that provides a domain-specific language (DSL) for writing powerful, maintainable tests for RESTful APIs.

    What is JUnit5?

    JUnit 5 is the next generation of JUnit. JUnit 5 is composed of several different modules from three different sub-projects.

    Dependency List:-

    1. REST Assured – 5.3.2
    2. Java 11
    3. JUnit Jupiter API – 5.10.0
    4. JUnit Jupiter Engine – 5.10.0
    5. Maven – 3.8.1
    6. Json – 20230618

    Detailed Step Description

    Step 1- Download and Install Java

    Java needs to be present on the system to run the tests. Click here to know How to install Java. To know if Java is installed or not on your machine, type this command in the command line. This command will show the version of Java installed on your machine.

    java -version
    

    Step 2 – Download and setup Eclipse IDE on the system

    The Eclipse IDE (integrated development environment) provides strong support for Java developers, which is needed to write Java code. Click here to learn How to install Eclipse.

    Step 3 – Setup Maven

    To build a test framework, we need to add a number of dependencies to the project. It is a very tedious and cumbersome process to add each dependency manually. So, to overcome this problem, we use a build management tool. Maven is a build management tool that is used to define project structure, dependencies, build, and test management. Click here to learn How to install Maven.

    To know if Maven is already installed or not on your machine, type this command in the command line. This command will show the version of Maven installed on your machine.

    mvn -version
    

    Step 4 – Create a new Maven Project

    Click here to learn How to create a Maven project

    Below is the Maven project structure. Here,

    Group Id – com.example
    Artifact Id – RestAssured_JUnit5_Demo
    Version – 0.0.1-SNAPSHOT
    Package – com. example.RestAssured_JUnit4_Demo

    Step 5 – Add REST Assured and JUnit5 dependencies to the project

    Add the below-mentioned dependencies to the project.

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>org.example</groupId>
      <artifactId>RestAssured_Junit5_Demo</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>RestAssured_Junit5_Demo</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <rest-assurd.version>5.3.2</rest-assurd.version>
        <json.version>20230618</json.version>
        <hamcrest.version>1.3</hamcrest.version>
        <junit5.version>5.10.0</junit5.version>
        <maven.surefire.report.plugin.version>3.1.2</maven.surefire.report.plugin.version>
        <maven.compiler.plugin.version>3.10.1</maven.compiler.plugin.version>
        <maven.surefire.plugin.version>3.1.2</maven.surefire.plugin.version>
        <maven.compiler.source.version>11</maven.compiler.source.version>
        <maven.compiler.target.version>11</maven.compiler.target.version>
        <maven.site.plugin.version>3.12.0</maven.site.plugin.version>
      </properties>
    
      <dependencies>
    
        <!-- Rest Assured Dependency -->
        <dependency>
          <groupId>io.rest-assured</groupId>
          <artifactId>rest-assured</artifactId>
          <version>${rest-assurd.version}</version>
          <scope>test</scope>
        </dependency>
    
        <!-- JUNIT Jupiter API Dependency-->
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-api</artifactId>
          <version>${junit5.version}</version>
          <scope>test</scope>
        </dependency>
    
        <!-- JUNIT Jupiter Engine Dependency-->
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-engine</artifactId>
          <version>${junit5.version}</version>
          <scope>test</scope>
        </dependency>
    
        <!-- JSON Dependency -->
        <dependency>
          <groupId>org.json</groupId>
          <artifactId>json</artifactId>
          <version>${json.version}</version>
        </dependency>
    
        <!-- Hamcrest Dependency -->
        <dependency>
          <groupId>org.hamcrest</groupId>
          <artifactId>hamcrest-all</artifactId>
          <version>${hamcrest.version}</version>
          <scope>test</scope>
        </dependency>
    
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
              <source>${maven.compiler.source.version}</source>
              <target>${maven.compiler.target.version}</target>
            </configuration>
          </plugin>
    
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <configuration>
              <testFailureIgnore>true</testFailureIgnore>
            </configuration>
          </plugin>
    
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>${maven.site.plugin.version}</version>
          </plugin>
        </plugins>
      </build>
    
        <reporting>
          <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-surefire-report-plugin</artifactId>
              <version>${maven.surefire.report.plugin.version}</version>
              <configuration>
                <outputName>JUnit5 Report</outputName>
              </configuration>
            </plugin>
          </plugins>
        </reporting>
    
    </project>
    

    Step 6 – Create the TEST file

    The tests should be written in src/test/java directory. To learn how to create a JSON Request body using JSONObject, please refer to this tutorial.

    import io.restassured.http.ContentType;
    import org.json.JSONObject;
    import org.junit.jupiter.api.Test;
    import static io.restassured.RestAssured.given;
    import static org.hamcrest.Matchers.equalTo;
    
    public class APITests {
    
            String BaseURL = "https://reqres.in/api";
    
        @Test
        public void createUser() {
    
            JSONObject data = new JSONObject();
    
            data.put("name", "NewUser1");
            data.put("job", "Testing");
    
            // GIVEN
            given()
                    .contentType(ContentType.JSON)
                    .body(data.toString())
    
                    // WHEN
                    .when()
                    .post(BaseURL + "/users")
    
                    // THEN
                    .then()
                    .statusCode(201)
                    .body("name", equalTo("NewUser1"))
                    .body("job", equalTo("Testing"))
                    .log().all();
    
        }
    
        @Test
        public void getUser() {  //Failed Test
    
            // GIVEN
            given()
                    .contentType(ContentType.JSON)
    
                    // WHEN
                    .when()
                    .get(BaseURL + "/users/2")
    
                    // THEN
                    .then()
                    .statusCode(200)
                    .body("data.first_name", equalTo("Janet1"))
                    .log().all();
    
        }
    
    }
    

    Step 7 – Test Execution through JUnit Test

    Go to the Runner class and right-click Run As JUnit Test. The tests will run as JUnit tests.

    Below is the image to run the tests in IntelliJ.

    This is how the execution console will look like.

    Step 8 – Run the tests from the command line

    Maven Site Plugin creates a folder – site under the target directory, and the Maven Surefire Report plugin generates the JUnit Reports in the site folder. We need to run the tests through the command line to generate the JUnit Report.

    mvn clean test site
    

    The output of the above program is

    Step 9 – Report Generation

    After the test execution, refresh the project, and a new folder with the name site in the target folder will be generated. This folder contains the reports generated by JUnit. The structure of the folder site looks as shown below.

    View the Report

    Right-click on the Junit5 Report.html and select Open In -> Browser ->Chrome.

    Summary Report

    Below is the summary Report.

    Surefire Report

    Below is an example of a Surefire Report. This report contains a summary of the test execution.

    We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

    Logging in Rest Assured

    HOME

    Logging plays an important role in understanding the behaviour of the test. When we are testing an API, it is good to know how the APIs are behaving. We should understand how the request is made and how we received the response from the API. It’s important to check what the headers look like. We also need to see what the body looks like. Additionally, verify what parameters we are providing to the request. All of this helps us debug the test code. It helps us identify the reason for the failure of the test.

    REST Assured, provide support to a different type of logging as shown below:-

    Request Logging

    To log all request specification details including parameters, headers, and body of the request, log().all() needs to be added to post given() section.

    import org.testng.annotations.Test;
    import io.restassured.http.ContentType;
    import static io.restassured.RestAssured.*;
    import static org.hamcrest.Matchers.equalTo;
    
    public class RestTests {
    	
    	@Test
        public void requestLoggingDemo() {
     
            String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
     
            // GIVEN
            given()
                   .log().all()
                   .baseUri("https://dummy.restapiexample.com/api")
                   .contentType(ContentType.JSON)
                   .body(json)
     
            // WHEN
             .when()
                    .post("/v1/create")
     
            // THEN
              .then()
                     .assertThat()
                     .statusCode(200)
                     .body("data.name", equalTo("apitest"))
                     .body("message", equalTo("Successfully! Record has been added."));
     
        }
    
    }
    

    The output of the above program is

    Other different request logging options are:-

    given().log().params(). .. // Log only the parameters of the request
    given().log().body(). .. // Log only the request body
    given().log().headers(). .. // Log only the request headers
    given().log().cookies(). .. // Log only the request cookies
    given().log().method(). .. // Log only the request method
    given().log().path(). .. // Log only the request path
    

    Response Logging

    If you want to print the response body regardless of the status code, you can do

    get("/x").then().log().body()..
    

    This will print the response body regardless of an error occurring.

    import org.testng.annotations.Test;
    import io.restassured.http.ContentType;
    import static io.restassured.RestAssured.*;
    import static org.hamcrest.Matchers.equalTo;
    
    public class RestTests {
    
      @Test
    	public void responseLoggingDemo() {
    
    		String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
    
    		// GIVEN
    		given()
                  .baseUri("https://dummy.restapiexample.com/api")
                  .contentType(ContentType.JSON)
    			  .body(json)
    
    		 // WHEN
    		  .when()
                     .post("/v1/create")
    
    		// THEN
    		  .then()
                     .log().all()
                     .statusCode(200)
                     .body("data.name", equalTo("apitest"))
    				 .body("message", equalTo("Successfully! Record has been added."));
    
    	}
    }
    

    The output of the above program is

    Conditional Logging

    What if you want to perform logging conditionally? For example, log in if validation fails and the status code is equal to 200. Also, log in if the server returns a status code >=400.

    .then().log().ifStatusCodeIsEqualTo(302). .. // Only log if the status code is equal to 302
    .then().log().ifStatusCodeMatches(matcher). .. // Only log if the status code matches the supplied Hamcrest matcher
    
    import org.testng.annotations.Test;
    import io.restassured.http.ContentType;
    import static io.restassured.RestAssured.*;
    import static org.hamcrest.Matchers.equalTo;
    
    public class RestTests {
    	
    	@Test
    	public void conditionalResponseLoggingDemo() {
    
    		String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
    
    		// GIVEN
    		given()
                   .baseUri("https://dummy.restapiexample.com/api")
                   .contentType(ContentType.JSON)
    			   .body(json)
    
    		// WHEN
    		 .when()
                   .post("/v1/create")
    
    		// THEN
    		 .then()
                    .log().ifStatusCodeIsEqualTo(200)
                    .assertThat().statusCode(200)
    				.body("data.name", equalTo("apitest"))
                    .body("message", equalTo("Successfully! Record has been added."));
    
    	}
    
    }
    

    The output of the above program is

    Logging to a text file with Rest Assured

    We will see how we can log all the request and response data to a txt file using Rest Assured.

    1. Create a PrintStream object. You have to provide an object of FileOutputStream() to the PrintStream() constructor. Provide the path to the logging.txt file in FileOutputStream().
    2. REST Assured gives us a filter() method, this filter method accepts RequestLoggingFilter and ResponseLoggingFilter. They have two methods, logRequestTo() and logResponseTo() methods respectively. These methods expect a Stream.
    3. Pass the log stream we created to these methods.
    import org.testng.annotations.Test;
    import io.restassured.filter.log.RequestLoggingFilter;
    import io.restassured.filter.log.ResponseLoggingFilter;
    import io.restassured.http.ContentType;
    import static io.restassured.RestAssured.*;
    import static org.hamcrest.Matchers.equalTo;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    
    public class RestTests {
    	
    	@Test
    	public void responsetoFileDemo() throws FileNotFoundException {
    
    		PrintStream log = new PrintStream(new FileOutputStream("logging.txt"));
    
    		String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
    
    		// GIVEN
    		given()
                   .baseUri("https://dummy.restapiexample.com/api")
                   .contentType(ContentType.JSON)
    				.body(json)
                    .filter(RequestLoggingFilter.logRequestTo(log))
    				.filter(ResponseLoggingFilter.logResponseTo(log))
    
    		// WHEN
    		 .when()
                    .post("/v1/create")
    
    		// THEN
              .then()
                     .log().ifStatusCodeIsEqualTo(200)
                     .assertThat().statusCode(200)
    				 .body("data.name", equalTo("apitest"))
                     .body("message", equalTo("Successfully! Record has been added."));
    
    	}
    }
    

    Mostly we have more than 1 test, and we want to save the log of all the tests in the text file. We can create a @BeforeClass method, and this class contains the code to create the file and append the data to that file.

    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    import io.restassured.filter.log.RequestLoggingFilter;
    import io.restassured.filter.log.ResponseLoggingFilter;
    import io.restassured.http.ContentType;
    import static io.restassured.RestAssured.*;
    import static org.hamcrest.Matchers.equalTo;
    
    public class LogTest {
    	
    	 public PrintStream log ;
    	 RequestLoggingFilter requestLoggingFilter;
    	 ResponseLoggingFilter responseLoggingFilter;
    	
    	@BeforeClass
    	public void init() throws FileNotFoundException {
    		
    		 log = new PrintStream(new FileOutputStream("test_logging.txt"),true);	
    		 requestLoggingFilter = new RequestLoggingFilter(log);
    		 responseLoggingFilter = new ResponseLoggingFilter(log);
    		 
    	}
    	
    	@Test
    	public void test1() {
    
    		// Given
    		given()
    
             .contentType(ContentType.JSON)
             . filters(requestLoggingFilter,responseLoggingFilter)
           
              .when()
                 .get("https://dummy.restapiexample.com/api/v1/employee/2")
    				
               .then()
               		.log().ifStatusCodeIsEqualTo(200)
               		.assertThat().statusCode(200).statusLine("HTTP/1.1 200 OK")
    				
               		// To verify booking id at index 2
    				.body("data.employee_name", equalTo("Garrett Winters"))
    				.body("message", equalTo("Successfully! Record has been fetched."));
    	}
    	
    	@Test
    	public void test2() {
    
    		// Given
    		given()
    
             .contentType(ContentType.JSON)
             . filters(requestLoggingFilter,responseLoggingFilter)
           
              .when()
                 .get("https://dummy.restapiexample.com/api/v1/employee/1")
    				
               .then()
               .log().ifStatusCodeIsEqualTo(200)
               .assertThat().statusCode(200).statusLine("HTTP/1.1 200 OK")
    				// To verify booking id at index 1
    				.body("data.employee_name", equalTo("Tiger Nixon"))
    				.body("message", equalTo("Successfully! Record has been fetched."));
    	}
    
    	
    	
    	@Test
        public void test3() throws FileNotFoundException {
     
            
            String json = "{\"name\":\"apitest\",\"salary\":\"5000\",\"age\":\"30\"}";
     
            // GIVEN
            given()
                   .baseUri("https://dummy.restapiexample.com/api")
                   .contentType(ContentType.JSON)
                    .body(json)
                   .filters(requestLoggingFilter,responseLoggingFilter)
     
            // WHEN
             .when()
                    .post("/v1/create")
     
            // THEN
              .then()
                     .log().ifStatusCodeIsEqualTo(200)
                     .assertThat().statusCode(200)
                     .body("data.name", equalTo("apitest"))
                     .body("message", equalTo("Successfully! Record has been added."));
     
        }
    	
    }
    
    

    The below file shows that the log for multiple requests is saved here.

    Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

    How to test PATCH Request using Rest Assured
    How to test POST request from JSON Object in Rest Assured
    How to test POST JSON Object request using Java Map in Rest Assured
    How to create JSON Array Request Body – org.json
    Assertion of JSON in Rest Assured using Hamcrest
    Extraction from JSON in Rest Assured

    Basic Selenium Tutorials

    HOME

    Selenium – Introduction, Installation, Test Script

    Chapter 1 Introduction to Selenium Automation Tool
    Chapter 2 How to Download & Install Java JDK 11 in Windows
    Chapter 3 How to Download and Install Eclipse IDE
    Chapter 4 How to install IntelliJ on Windows
    Chapter 5 How to Download & Install Selenium WebDriver 
    Chapter 6  How to create first Selenium WebDriver Script using Java
    Chapter 7 How to run Selenium Tests using on Internet Explorer

    Locators in Selenium

     Chapter 1 How to Locate Elements in Chrome, Firefox and IE Browsers for creating Selenium Scripts
    Chapter 2 Locators in Selenium – Locate by ID, ClassName,  Name, TagName,  LinkText, PartialLinkText
    Chapter 3 Dynamic XPath  in Selenium WebDriver
    Chapter 4 CSS Selector in Selenium WebDriver

    Launching Browsers and headless Browser

    Chapter 1 How to run Chrome tests in headless mode in Selenium
    Chapter 2 How to run Firefox tests in headless mode in Selenium
    Chapter 3 How to run Edge tests in headless mode in Selenium4
    Chapter 4 How to manage driver executables using WebDriverManager
    Chapter 5 How to disable infobar warning for Chrome tests in Selenium
    Chapter 6 How to maximize and minimize the window in Selenium

    WebDriver Commands

    Chapter 1 Difference between FindElement and FindElements in WebDriver
    Chapter 2 Difference between getText() and getAttribute() method in WebDriver
    Chapter 3 WebDriver Browser Commands – get,  getTitle, getCurrentUrl, getPageSource, getClass, close, quit in WebDriver
    Chapter 4 WebDriver Navigation Commands – Navigate, Forward, Back, Refresh in  WebDriver
    Chapter 5 Selenium Form WebElement Commands – Sendkeys, Clear, Click,Submit
    Chapter 6 How to automate selecting Checkbox and Radio Buttons in Selenium WebDriver
    Chapter 7 How to Select value from Drop down list or perform Multiple Selection  Operations in WebDriver
    Chapter 8 How to get all options in a DropDown list in WebDriver
    Chapter 9 How to automate Radio Button in WebDriver
    Chapter 10 How to automate BootStrap DropDown using WebDriver
    Chapter 15 How to handle Dynamic Web Tables using Selenium WebDriver
    Chapter 16 How to get all the values from a Dynamic Table in Selenium WebDriver 
    Chapter 17 isDisplayed, isSelected, isEnabled in Selenium
    Chapter 18 How to test HTML ordered list in Selenium
    Chapter 19 How to test HTML5 validation messages with Selenium

    Waits in Selenium

    Chapter 1 Implicit and Explicit Wait in Selenium WebDriver
    Chapter 2 What is Fluent Wait in Selenium WebDriver

    Handle Window and Alerts

    Chapter 1 Switch Window Commands in Selenium WebDriver
    Chapter 2 How to handle Alerts in Selenium WebDriver
    Chapter 3 How to Switch Between Frames in Selenium WebDriver

    Selenium Interview Questions and Answers
    Advanced Selenium Interview Questions and Answers
    Selenium Multiple Choice Questions – MCQ1
    Selenium Multiple Choice Questions – MCQ1
    Selenium Multiple Choice Questions – MCQ3

    Page Object Model with Selenium, Cucumber, and TestNG

    Last Updated On

    HOME

    In the previous tutorial, I explained the Page Object Model with Selenium, Cucumber and JUnit. In this tutorial, I’ll create a BDD Framework for web application testing. I will use the Page Object Model with Selenium, Cucumber, and TestNG.

    Table of Contents

    1. What Is Page Object Model (POM)?
    2. What is Cucumber?
    3. Dependency List
    4. Project Structure
    5. Implementation Steps
      1. Download and Install Java
      2. Setup Maven
      3. Install Cucumber Eclipse Plugin (Only for Eclipse)
      4. Create a new Maven Project
      5. Create source folder src/test/resources to create test scenarios in the Feature file
      6. Add Selenium, TestNG, and Cucumber dependencies to the project
      7. Add Maven Compiler Plugin and Surefire Plugin
      8. Create a feature file in the src/test/resources
      9. Create the classes for locators, actions, and utilities in src/main/java
      10. Create a StepDefinition class in src/test/java
      11. Create a Hook class in src/test/java
      12. Create a TestNG Cucumber Runner class in the src/test/java
      13. Run the tests from TestNG
      14. Run the tests from testng.xml
      15. Run the tests from Command Line
      16. Cucumber Report Generation
      17. TestNG Report Generation

    What Is Page Object Model (POM)?

    The Page Object model is an object design pattern in Selenium, where web pages are represented as classes, the various elements on the page are defined as variables in the class and all possible user interactions can then be implemented as methods in the class.

    What is Cucumber?

    Cucumber is one such open-source tool, which supports Behavior Driven Development(BDD). In simple words, Cucumber can be defined as a testing framework, driven by plain English. It serves as documentation, automated tests, and development aid – all in one.

    Dependency List

    1. Cucumber Java – 7.18.1
    2. Cucumber TestNG – 7.18.1
    3. Java 17
    4. Maven – 3.9.6
    5. Selenium – 4.23.0
    6. TestNG – 7.10.2
    7. Maven Compiler – 3.13.0
    8. Maven Surefire – 3.3.1

    Project Structure

    Implementation Steps

    Step 1- Download and Install Java

    Cucumber and Selenium need Java to be installed on the system to run the tests. Click here to learn How to install Java.

    Step 2 – Setup Maven

    To build a test framework, we need to add a number of dependencies to the project. Click here to learn How to install Maven.

    Step 3 – Install Cucumber Eclipse Plugin (Only for Eclipse)

    The cucumber plugin is an Eclipse plugin that allows eclipse to understand the Gherkin syntax. When we are working with cucumber we will write the feature files that contain Feature, Scenario, Given, When, Then, And, But, Tags, Scenario Outline, and Examples. By default, eclipse doesn’t understand these keywords so it doesn’t show any syntax highlighter. Cucumber Eclipse Plugin highlights the keywords present in Feature File. Refer to this tutorial to get more detail – How to setup Cucumber with Eclipse.

    Step 4 – Create a new Maven Project

    To create a new Maven project, go to the File -> New Project-> Maven-> Maven project-> Next -> Enter Group ID & Artifact ID -> Finish.

    Click here to learn How to create a Maven project.

    Step 5 – Create source folder src/test/resources to create test scenarios in the Feature file

    A new Maven Project is created with 2 folders – src/main/java and src/test/java. To create test scenarios, we need a new source folder called – src/test/resources. To create this folder, right-click on test directory ->select New ->Directory, and then it shows Maven Source Directories as resources as shown below.

    Double-click on the resources directory and a new source directory under your new Maven project is created as shown in the below image.

    Step 6 – Add Selenium, TestNG, and Cucumber dependencies to the project

    Add below mentioned Selenium, TestNG, and Cucumber dependencies to the project.

      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cucumber.version>7.18.1</cucumber.version>
        <selenium.version>4.23.0</selenium.version>
        <testng.version>7.10.2</testng.version>
        <apache.common.version>2.4</apache.common.version>
        <maven.compiler.plugin.version>3.13.0</maven.compiler.plugin.version>
        <maven.surefire.plugin.version>3.3.1</maven.surefire.plugin.version>
        <maven.compiler.source.version>17</maven.compiler.source.version>
        <maven.compiler.target.version>17</maven.compiler.target.version>
      </properties>
    
      <dependencies>
    
        <dependency>
          <groupId>io.cucumber</groupId>
          <artifactId>cucumber-java</artifactId>
          <version>${cucumber.version}</version>
        </dependency>
    
        <dependency>
          <groupId>io.cucumber</groupId>
          <artifactId>cucumber-testng</artifactId>
          <version>${cucumber.version}</version>
          <scope>test</scope>
        </dependency>
    
        <!-- Selenium -->
        <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
          <version>${selenium.version}</version>
        </dependency>
    
        <!-- TestNG -->
        <dependency>
          <groupId>org.testng</groupId>
          <artifactId>testng</artifactId>
          <version>${testng.version}</version>
          <scope>test</scope>
        </dependency>
    
        <!-- Apache Common -->
        <dependency>
          <groupId>org.apache.directory.studio</groupId>
          <artifactId>org.apache.commons.io</artifactId>
          <version>${apache.common.version}</version>
        </dependency>
    
      </dependencies>
    

    Step 7 – Add Maven Compiler Plugin and Surefire Plugin

    The compiler plugin is used to compile the source code of a Maven project. This plugin has two goals, which are already bound to specific phases of the default lifecycle:

    • compile – compile main source files
    • testCompile – compile test source files
    <plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<version>${maven.compiler.plugin.version}</version>
    				<configuration>
    					<source>${maven.compiler.source.version}</source>
    					<target>${maven.compiler.target.version}</target>
    				</configuration>
    			</plugin>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-surefire-plugin</artifactId>
    				<version>${maven.surefire.plugin.version}</version>
    				<configuration>
                    <includes>
                        <include>**/*Tests.java</include>
                    </includes>
    				</configuration>
    			</plugin>
    		</plugins>
    

    The complete POM.xml looks like as shown below

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.example</groupId>
      <artifactId>POM_Cucumber_TestNG_Demo</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>POM_Cucumber_TestNG_Demo</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cucumber.version>7.18.1</cucumber.version>
        <selenium.version>4.23.0</selenium.version>
        <testng.version>7.10.2</testng.version>
        <apache.common.version>2.4</apache.common.version>
        <maven.compiler.plugin.version>3.13.0</maven.compiler.plugin.version>
        <maven.surefire.plugin.version>3.3.1</maven.surefire.plugin.version>
        <maven.compiler.source.version>17</maven.compiler.source.version>
        <maven.compiler.target.version>17</maven.compiler.target.version>
      </properties>
    
      <dependencies>
    
        <dependency>
          <groupId>io.cucumber</groupId>
          <artifactId>cucumber-java</artifactId>
          <version>${cucumber.version}</version>
        </dependency>
    
        <dependency>
          <groupId>io.cucumber</groupId>
          <artifactId>cucumber-testng</artifactId>
          <version>${cucumber.version}</version>
          <scope>test</scope>
        </dependency>
    
        <!-- Selenium -->
        <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
          <version>${selenium.version}</version>
        </dependency>
    
        <!-- TestNG -->
        <dependency>
          <groupId>org.testng</groupId>
          <artifactId>testng</artifactId>
          <version>${testng.version}</version>
          <scope>test</scope>
        </dependency>
    
        <!-- Apache Common -->
        <dependency>
          <groupId>org.apache.directory.studio</groupId>
          <artifactId>org.apache.commons.io</artifactId>
          <version>${apache.common.version}</version>
        </dependency>
    
    
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
              <source>${maven.compiler.source.version}</source>
              <target>${maven.compiler.target.version}</target>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <configuration>
              <suiteXmlFiles>
                <suiteXmlFile>testng.xml</suiteXmlFile>
              </suiteXmlFiles>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    

    Step 8 – Create a feature file in the src/test/resources

    Create a folder with name features. Now, create the feature file in this folder. The feature file should be saved with the extension .feature. This feature file contains the test scenarios created to test the application. The Test Scenarios are written in Gherkins language in the format of Given, When, Then, And, But.

    Below is an example of Test Scenarios in the feature file. I have failed one test scenario intentionally – @MissingUsername.

    Feature: Login to HRM Application
    
      Background:
        Given User is on HRMLogin page "https://opensource-demo.orangehrmlive.com/"
    
      @ValidCredentials
      Scenario: Login with valid credentials
    
        When User enters username as "Admin" and password as "admin123"
        Then User should be able to login successfully and new page open
    
      @InvalidCredentials
      Scenario Outline: Login with invalid credentials
    
        When User enters username as "<username>" and password as "<password>"
        Then User should be able to see error message "<errorMessage>"
    
        Examples:
          | username   | password  | errorMessage           |
          | Admin      | admin12$$ | Invalid credentials    |
          | admin$$    | admin123  | Invalid credentials    |
          | abc123     | xyz$$     | Invalid credentials    |
    
      @MissingUsername @FailedTest
      Scenario: Login with blank username
    
        When User enters username as " " and password as "admin123"
        Then User should be able to see a message "Required1" below Username
    

    Step 9 – Create the classes for locators, actions, and utilities in src/main/java

    Create folders – actions, locators, and utils in src/main/java.

    Create a Java Class for each page where define WebElements as variables using Annotation @FindBy. Create another Java class that contains methods for actions performed on WebElements. Here, I’m going to create 2 classes for locators – LoginPageLocators and HomePageLocators as well as 2 classes for actions – LoginPageActions and HomePageActions

    The Locator class contains WebElements which are identified by @FindBy annotation as shown below:-

    @FindBy(name = "txtUsername")
    WebElement userName;
    

    Action class contains methods for the action to be performed on the web elements identified in the locator class.

    The initElements is a static method of PageFactory class that is used to initialize all the web elements located by @FindBy annotation. Only after the WebElements are initialized, they can be used in the methods to perform actions.

    public Login(WebDriver driver) {
               this.driver = driver;
               // This initElements method will create all WebElements
               PageFactory.initElements(driver, this);
         }
    

    Below is the sample code of the LoginPageLocators.

    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    
    public class LoginPageLocators {
    
    	@FindBy(name = "username")
        public WebElement userName;
     
        @FindBy(name = "password")
        public WebElement password;
        
        @FindBy(xpath = "//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/form/div[1]/div/span")
        public WebElement missingUsernameErrorMessage;
        
        @FindBy(xpath = "//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/button")
        public WebElement login;
     
        @FindBy(xpath = "//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/div/div[1]/div[1]/p")
        public  WebElement errorMessage;
           
    }
    

    Below is the sample code for the HomePageLocators.

    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    
    public class HomePageLocators {
    
    	  @FindBy(xpath = "//span[@class='oxd-topbar-header-breadcrumb']/h6")
    	  public  WebElement homePageUserName;
    	  
    }
    

    Create the action classes for each web page. These action classes contain all the methods needed by the step definitions. In this case, I have created 2 action classes – LoginPageActionsHomePageActions 

    LoginPageActions

    import org.example.locators.LoginPageLocators;
    import org.example.utils.HelperClass;
    import org.openqa.selenium.support.PageFactory;
    
    public class LoginPageActions {
    
        LoginPageLocators loginPageLocators = null;
    
        public LoginPageActions() {
    
            this.loginPageLocators = new LoginPageLocators();
    
            PageFactory.initElements(HelperClass.getDriver(),loginPageLocators);
        }
    
        // Get the error message when username is blank
        public String getMissingUsernameText() {
            return loginPageLocators.missingUsernameErrorMessage.getText();
        }
    
        // Get the Error Message
        public String getErrorMessage() {
            return loginPageLocators.errorMessage.getText();
        }
    
        public void login(String strUserName, String strPassword) {
    
            // Fill user name
            loginPageLocators.userName.sendKeys(strUserName);
    
            // Fill password
            loginPageLocators.password.sendKeys(strPassword);
    
            // Click Login button
            loginPageLocators.login.click();
    
        }
    

    HomePageActions

    import org.example.locators.HomePageLocators;
    import org.example.utils.HelperClass;
    import org.openqa.selenium.support.PageFactory;
    
    public class HomePageActions {
    
        HomePageLocators homePageLocators = null;
    
        public HomePageActions() {
    
            this.homePageLocators = new HomePageLocators();
            PageFactory.initElements(HelperClass.getDriver(),homePageLocators);
        }
    
        // Get the User name from Home Page
        public String getHomePageText() {
            return homePageLocators.homePageUserName.getText();
        }
    
    }
    

     Create a Helper class where we are initializing the web driver, initializing the web driver wait, defining the timeouts, and creating a private constructor of the class, it will declare the web driver, so whenever we create an object of this class, a new web browser is invoked. 

    package com.example.utils;
    
    import java.time.Duration;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import io.github.bonigarcia.wdm.WebDriverManager;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    public class HelperClass {
    
    	 private static HelperClass helperClass;
         
    	    private static WebDriver driver;
    	    public final static int TIMEOUT = 5;
    	      
    	     private HelperClass() {
    	           
    	        WebDriverManager.chromedriver().setup();
    			ChromeOptions options = new ChromeOptions();
    			options.addArguments("--start-maximized");
    	        driver = new ChromeDriver(options);
    	        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(TIMEOUT));
    	     }      
    	              
    	    public static void openPage(String url) {
    	        driver.get(url);
    	    }
    	  	      
    	    public static WebDriver getDriver() {
    	        return driver;              
    	    }
    	      
    	    public static void setUpDriver() {
    	          
    	        if (helperClass==null) {
    	              
    	            helperClass = new HelperClass();
    	        }
    	    }
    	      
    	    public static void tearDown() {
    	           
    	        if(driver!=null) {
    	             driver.quit();
    	        }
    	           
    	       helperClass = null;
    	   } 
    	      
    	}
    

    Step 10 – Create a StepDefinition class in src/test/java

    Create a Java Class called Definition where we will create the Test Code related to the Given, When, Then of Feature file in src/test/java.

    Now, we need to create the Step Definition of the Feature File – LoginPageDefinitions.java.

    import io.cucumber.java.en.Given;
    import io.cucumber.java.en.Then;
    import io.cucumber.java.en.When;
    import org.example.actions.HomePageActions;
    import org.example.actions.LoginPageActions;
    import org.example.utils.HelperClass;
    import org.testng.Assert;
    
    public class LoginPageDefinitions {
    
        LoginPageActions objLogin = new LoginPageActions();
        HomePageActions objHomePage = new HomePageActions();
    
        @Given("User is on HRMLogin page {string}")
        public void loginTest(String url) {
    
            HelperClass.openPage(url);
    
        }
    
        @When("User enters username as {string} and password as {string}")
        public void goToHomePage(String userName, String passWord) {
    
            // login to application
            objLogin.login(userName, passWord);
    
        }
    
        @Then("User should be able to login successfully and new page open")
        public void verifyLogin() {
    
            // Verify home page
            Assert.assertTrue(objHomePage.getHomePageText().contains("Dashboard"));
    
        }
    
        @Then("User should be able to see error message {string}")
        public void verifyErrorMessage(String expectedErrorMessage) {
    
            // Verify error message
            Assert.assertEquals(objLogin.getErrorMessage(),expectedErrorMessage);
    
        }
    
        @Then("User should be able to see a message {string} below Username")
        public void verifyMissingUsernameMessage(String message) {
    
            Assert.assertEquals(objLogin.getMissingUsernameText(),message);
        }
    
    }
    

    Step 11 – Create a Hook class in src/test/java

    Create the hook class that contains the Before and After hook to initialize the web browser and close the web browser. I have added the code to take the screenshot of the failed scenario in @After Hook.

    Below is the code for the Hooks class.

    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import com.example.utils.HelperClass;
    import io.cucumber.java.After;
    import io.cucumber.java.Before;
    import io.cucumber.java.Scenario;
    
    public class Hooks {
    
    	@Before
        public static void setUp() {
    
           HelperClass.setUpDriver();
        }
    
    	@After
    	public static void tearDown(Scenario scenario) {
    
    		//validate if scenario has failed
    		if(scenario.isFailed()) {
    			final byte[] screenshot = ((TakesScreenshot) HelperClass.getDriver()).getScreenshotAs(OutputType.BYTES);
    			scenario.attach(screenshot, "image/png", scenario.getName()); 
    		}	
    		
    		HelperClass.tearDown();
    	}
    }
    

    Step 12 – Create a TestNG Cucumber Runner class in the src/test/java

    Cucumber needs a TestRunner class to run the feature files. It is suggested to create a folder with the name of the runner in the src/test/java directory and create the Cucumber TestRunner class in this folder. Below is the code of the Cucumber TestRunner class.

    Below is the code for CucumberRunnerTests class.

    import io.cucumber.testng.AbstractTestNGCucumberTests;
    import io.cucumber.testng.CucumberOptions;
    
    @CucumberOptions(tags = "", features = "src/test/resources/features/LoginPage.feature", glue = "com.example.definitions",
            plugin = {})
    
    public class CucumberRunnerTests extends AbstractTestNGCucumberTests {
    }
    
    

    Note:- The name of the Runner class should end with Test otherwise we can’t run the tests using Command Line.

    Step 13 – Run the tests from TestNG

    You can execute the test script by right-clicking on TestRunner class -> Run As TestNG. (Eclipse)

    In the case of the IntelliJ project, right-click on the runner class and select Run ‘CucumberRunnerTests’.

    The output of the above program is

    Step 14 – Run the tests from testng.xml

    Create a testng.xml as shown below and run the tests as TestNG.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
    <suite name="Suite">
        <test  name="Cucumber with TestNG Test">
            <classes>
                <class name="com.example.runner.CucumberRunnerTests"/>
            </classes>
        </test> <!-- Test -->
    </suite> <!-- Suite -->
    

    The testng.xml is highlighted below:

    Step 15 – Run the tests from Command Line

    Run the below command in the command prompt to run the tests and to get the test execution report.

    mvn clean test
    

    The output of the above program is

    Step 16 – Cucumber Report Generation

    To get Cucumber Test Reports, add cucumber.properties under src/test/resources and add the below instruction in the file.

    cucumber.publish.enabled=true
    

    Below is the image of the Cucumber Report generated using the Cucumber Service.

    In the above example, as we can see, one of the tests has failed. So, when a test fails, we have written the code to take a screenshot of the failed step. The Attached Image shows the image of the failed test. You can click on that to see the screenshot.

    Step 17 – TestNG Report Generation

    TestNG generates various types of reports under the target->surefire-reports folder like emailable-report.html, index.html, testng-results.xml.

    We are interested in the “emailable-report.html” report. Open “emailable-report.html“, as this is an HTML report, and open it with the browser. The below image shows emailable-report.html.

    emailable-report.html

    Index.html

    TestNG also produces an “index.html” report. The below image shows the index.html report.

    That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

    Integration of Cucumber with Selenium and JUnit
    Integration of Cucumber with Selenium and TestNG
    Rest API Test in Cucumber BDD
    Integration Testing of Springboot with Cucumber and JUnit4
    Integration of Cucumber7 with Selenium and JUnit5
    Run Cucumber7 with JUnit5 Tests from Maven Command Line

    How to create Jenkins pipeline for Selenium tests

    Last Updated On

    HOME

    In the previous tutorial, I discussed the Jenkins pipeline. This tutorial will discuss the steps to create the Jenkins pipeline for Selenium tests. This is an important step in CI/CD.

    Table of Contents

    1. Prerequisite
    2. Implementation Steps
      1. Create a new pipeline project
      2. Scroll down to Pipeline
      3. Create Jenkinsfile
      4. Specify branches to build a section under Repositories
      5. Execute the tests
      6. Pipeline Steps
      7. View the Report
    3. How to get the pipeline syntax?

    Prerequisite

    1. Jenkins installed and started on the computer – How to install Jenkins on Windows 10.

    2. Java and Maven are installed and configured in Jenkins. To know more about this, please refer to this – How to configure Java and Maven in Jenkins.

    3. To generate a HTML Report in Jenkins, we need to download the HTML Publisher Plugin. Please refer to this tutorial to install the plugin – How to generate HTML Reports in Jenkins

    Implementation Steps

    Step 1: Create a new pipeline project

    1. Give the Name of the project – Selenium_PipelineDemo.
    2. Click on the pipeline project. 
    3. Click on the OK button.

    In the General section, enter the project description in the Description box.

    Step 2: Scroll down to Pipeline

    From the Definition field, choose the “Pipeline script from SCM” option. This option instructs Jenkins to obtain your Pipeline from Source Control Management (SCM), which will be your locally cloned Git repository.

    From the SCM field, choose Git.

    The “Repositories” section contains the “Repository URL” and “Credentials“.

    In the Repository URL field, specify the directory path of the GitLab/GitHub project.

    In the Credentials field, specify the username and password needed to log in to GitLab/GitHub.

    In this case, I have the project present in GitLab and using it.

    Step 3: Create Jenkinsfile

    Create and save a new text file with the name Jenkinsfile at the root of the project in the GitLab repository. Here, we are using the Selenium project with TestNG. To learn more about the Integration of Selenium with TestNG, please refer to this tutorial – Integration of Selenium and TestNG.

    For this tutorial, we are using Declarative syntax. The sample example is given below:

    Here, I have used emailable-report.html, you can also use index.html and that report will be published.

    pipeline {
        agent any
    
        stages {
            stage('Test') {
                steps {
                    bat "mvn -D clean test"
                }
    
                post {                
                    // If Maven was able to run the tests, even if some of the test
                    // failed, record the test results and archive the jar file.
                    success {
                       publishHTML([
                           allowMissing: false, 
                           alwaysLinkToLastBuild: false, 
                           keepAll: false, 
                           reportDir: 'target/surefire-reports/', 
                           reportFiles: 'emailable-report.html', 
                           reportName: 'HTML Report', 
                           reportTitles: '', 
                           useWrapperFileDirectly: true])
                    }
                }
            }
        }
    }
    
    

    Step 4: Specify branches to build a section under Repositories

    1. Branch Specifier – */master (This is my main branch)
    2. ScriptPath – Jenkinsfile

    Click on the Apply and Save buttons.

    We have created a new Maven project Selenium_PipelineDemo” with the configuration to run the Selenium Test with TestNG.

    Step 5: Execute the tests

    Let’s execute it now by clicking on the “Build Now” button. 

    Right-click on Build Number (here in my case it is #11) and click on Console Output to see the result.

    Below is the test execution summary.

    Step 6: Pipeline Steps

    Once the execution is completed, and we want to see the Pipeline Steps, click on the Pipeline Steps mentioned on the left side of the page.

    Step 7: View the Report

    Once the execution is completed, go back to “Selenium_PipelineDemo”. We can see below that the HTML Report is generated.

    We could see a link to view ‘HTML Reports’. Click on the HTML Reports. It displays the emailable-report.html Report.

    Tip: If you don’t see the Report UI intact, then you need to configure a simple groovy script. For that, go to Dashboard–>Manage Jenkins–>Script Console and add the script as:

    System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")
    

    How to get the pipeline syntax?

    If you don’t know the syntax for the pipeline, there is an option “Pipeline Syntax” in the UI as shown below.

    Select any of the suitable options. In my case, I have used Publish HTML reports.

    Specify the name of the Report in the Index page[s]and Report Title such as HTML Report and click on the “Generate Pipeline Script”.

    Below is the sample syntax.

    We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

    Additional Tutorials

    How to install Jenkins on Windows 10
    Integration Of Jenkins With Selenium WebDriver
    How to install Maven Plugin in Jenkins
    Integrate Gradle project with Jenkins
    Jenkins GitLab Integration
    Integration of Cucumber Report with TestNG in Jenkins

    How to configure Junit in Intellij

    HOME

    In this tutorial we will discuss to create a JUnit  project using IntelliJ. We will be at first creating a simple Java Project and will add JUnit5 as well as create a Maven Project, then we will add a basic Class and a JUnit Test for it.

    Create a Java Project

    Step 1 – Create a new Java Project.

    To create a new Java project in Intellij, please refer to this tutorial.

    Step 2 – Right click on the project and select Open Module Settings.

    Step 3 – Go to the “Libraries” group, click the little plus (look up), and choose From Maven…option.

    Step 4 – Search for “junit” — something like “junit:junit-4.13“. Click the OK button.

    Step 5 – A new dialog will appear to confirm that “junit:junit:4.13.2” will be added to the module. Click the OK button.

    Step 6 – This screens shows that junit:junit:4.13.2 is added to the Libraries. It contains the highlighted classes – junit-4.13.2.jar and hamcrest-core-1.3.jar. Click the “OK” button.

    Step 7 – This image shows that the Junit is added to the External Libraries.

    Step 8 – Create a Java Class – JUnit4Test under src and create a JUnit test to verify that it is installed properly.

    import org.junit.Assert;
    import org.junit.Test;
    
    public class JUnit4Test {
    
        @Test
        public void Test() {
    
            String str1 = "Happy";
            String str2 = new String("Happy");
            Assert.assertEquals("String1 and String 2 are equal",str1, str2);
    
        }
    }
    

    Step 9 – There are many ways to run the test. One of the way is to Right-Click and select Run JUnit4Test

    The successful execution of the test shows that the JUnit is configured properly.

    Create a Maven Project

    Add Junit dependency to the POM.xml and build the project.

    <dependencies>
    
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        
    </dependencies>
    

    Now we need to apply the changes in the build script. Press Ctrl+Shift+O or click Load Maven Changes in the notification that appears in the top-right corner of the editor.

    Create a Java Class – JUnit4Test under src/test/java and create a JUnit test to verify that it is installed properly.

    import org.junit.Test;
    import static org.junit.Assert.assertArrayEquals;
    
    public class JUnitMavenTest {
    
        @Test
        public void Test() {
    
            String[] expected = {"happy","days","summer","spring"};
            String[] actual = {"happy","days","summer","spring"};
    
            assertArrayEquals("Expected and Actual Arrays are not equal",expected,actual);
    
        }
    }
    
    

    The output of the above program is

    Similarly, to add JUnit5 we can add below mentioned dependencies to the POM.xml.

    <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-engine</artifactId>
         <version>5.8.2</version>
         <scope>test</scope>
    </dependency>
    
    <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-api</artifactId>
         <version>5.8.2</version>
         <scope>test</scope>
    </dependency>
    

    Congratulations. We are able to add JUnit to Java or Maven project. Happy Learning!!

    Integration of Allure Report with Rest Assured and TestNG

    HOME

    In the previous tutorial, I explained the Integration of the Allure Report with Rest Assured with JUnit4. In this tutorial, I will explain how to Integrate Allure Report with Rest Assured and TestNG.

    The below example covers the implementation of Allure Report for Rest API using Rest Assured, TestNG, Java, and Maven.

    Prerequisite

    1. Java 17 installed
    2. Maven installed
    3. Eclipse or IntelliJ installed
    4. Allure installed

    Dependency List:

    1. Java 17
    2. Maven – 3.9.6
    3. Allure Report – 2.14.0
    4. Allure Rest Assured – 2.25.0
    5. Allure TestNG – 2.25.0
    6. Aspectj – 1.9.21
    7. Json – 20231013
    8. Maven Compiler Plugin – 3.12.1
    9. Maven Surefire Plugin – 3.2.3

    Implementation Steps

    Step 1 – Update Properties section in Maven pom.xml

    <properties>
            <hamcrest.version>1.3</hamcrest.version>
            <allure.rest.assured.version>2.25.0</allure.rest.assured.version>
            <json.version>20231013</json.version>
            <maven.compiler.plugin.version>3.12.1</maven.compiler.plugin.version>
            <maven.compiler.source>17</maven.compiler.source>
            <maven.compiler.target>17</maven.compiler.target>
            <aspectj.version>1.9.21</aspectj.version>
            <maven.surefire.plugin.version>3.2.3</maven.surefire.plugin.version>
            <allure.maven.version>2.12.0</allure.maven.version>
            <allure.testng.version>2.25.0</allure.testng.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
    

    Step 2 – Add the Allure-Rest Assured dependency

     <!--Allure Reporting Dependency-->   
       <dependency>
          <groupId>io.qameta.allure</groupId>
          <artifactId>allure-rest-assured</artifactId>
          <version>${allure.rest-assured.version}</version>
       </dependency>
    

    Add other dependencies like Rest Assured and Allure-TetNG dependencies in POM.xml

     <dependencies>
    
            <!-- Hamcrest Dependency -->
            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-all</artifactId>
                <version>${hamcrest.version}</version>
                <scope>test</scope>
            </dependency>
    
            <!-- JSON Dependency -->
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>${json.version}</version>
            </dependency>
    
            <!-- Allure TestNG Dependency -->
            <dependency>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-testng</artifactId>
                <version>${allure.testng.version}</version>
            </dependency>
    
            <!-- Allure Rest-assured Dependency-->
            <dependency>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-rest-assured</artifactId>
                <version>${allure.rest.assured.version}</version>
            </dependency>
    
        </dependencies>
    

    Step 3 – Update the Build Section of pom.xml in the Allure Report Project

     <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven.compiler.plugin.version}</version>
                    <configuration>
                        <source>${maven.compiler.source}</source>
                        <target>${maven.compiler.target}</target>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven.surefire.plugin.version}</version>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                        <argLine>
                            -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                        </argLine>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjweaver</artifactId>
                            <version>${aspectj.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
    
                 <plugin>
                    <groupId>io.qameta.allure</groupId>
                    <artifactId>allure-maven</artifactId>
                    <version>${allure.maven.version}</version>
                    <configuration>
                        <reportVersion>${allure.maven.version}</reportVersion>
                    </configuration>
                </plugin>
    
            </plugins>
        </build>
    </project>
    

    Step 4 – Create the Test Code for the testing of REST API under src/test/java

    To see our request and response in more detail using Rest Assured, we need to add a line to our Rest Assured tests. This will provide the request and response details in the report.

     .filter(new AllureRestAssured())
    
    import io.qameta.allure.*;
    import io.qameta.allure.restassured.AllureRestAssured;
    import io.restassured.http.ContentType;
    import org.json.JSONObject;
    import org.testng.annotations.Test;
    import static io.restassured.RestAssured.given;
    import static org.hamcrest.Matchers.equalTo;
    
    @Epic("REST API Regression Testing using TestNG")
    @Feature("Verify CRUID Operations on User module")
    public class RestAPITests {
    
        @Test(description = "To get the details of user with id 3", priority = 0)
        @Story("GET Request with Valid User")
        @Severity(SeverityLevel.NORMAL)
        @Description("Test Description : Verify the details of user of id-3")
        public void verifyUser() {
    
            // Given
            given()
    
                    .filter(new AllureRestAssured())
                    // When
                    .when()
                    .get("https://reqres.in/api/users/3")
    
                    // Then
                    .then()
                    .statusCode(200)
                    .statusLine("HTTP/1.1 200 OK")
    
                    // To verify user of id 3
                    .body("data.email", equalTo("emma.wong@reqres.in"))
                    .body("data.first_name", equalTo("Emma"))
                    .body("data.last_name", equalTo("Wong"));
        }
    
        @Test(description = "To create a new user", priority = 1)
        @Story("POST Request")
        @Severity(SeverityLevel.NORMAL)
        @Description("Test Description : Verify the creation of a new user")
        public void createUser() {
    
            JSONObject data = new JSONObject();
    
            data.put("name", "RestAPITest");
            data.put("job", "Testing");
    
            // GIVEN
            given()
    
                    .filter(new AllureRestAssured())
                    .contentType(ContentType.JSON)
                    .body(data.toString())
    
                    // WHEN
                    .when()
                    .post("https://reqres.in/api/users")
    
                    // THEN
                    .then()
                    .statusCode(201)
                    .body("name", equalTo("RestAPITest"))
                    .body("job", equalTo("Testing"));
    
        }
    
    }
    

    Step 5 – Create testng.xml for the project

    <?xml version = "1.0"encoding = "UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name = "Suite1">
        <test name = "TestNG Test Demo">
            <classes>
                <class name = "org.example.RestAPITests"/>
            </classes>
        </test>
    </suite>
    

    Step 6 – Run the Test and Generate Allure Report

    To run the tests, use the below command

    mvn clean test
    

    In the below image, we can see that all three tests are passed.

    This will create the allure-results folder with all the test reports. These files will be used to generate the Allure Report.

    To create an Allure Report, use the below command

    allure serve
    

    This will generate the beautiful Allure Test Report as shown below.

    Allure Report Dashboard

    The overview page hosts several default widgets representing the basic characteristics of your project and test environment.

    Categories in Allure Report

    The categories tab gives you a way to create custom defect classifications to apply for test results. There are two categories of defects – Product Defects (failed tests) and Test Defects (broken tests).

    Suites in Allure Report

    On the Suites tab a standard structural representation of executed tests, grouped by suites and classes, can be found.

    View test history

    Each time you run the report from the command line with the mvn clean test command, a new result JSON file will get added to the allure-results folder. Allure can use those files to include a historical view of your tests. Let’s give that a try.

    To get started, run mvn clean test a few times and watch how the number of files in the allure-reports folder grows.

    Now go back to view your report. Select Suites from the left nav, select one of your tests and click Retries in the right pane. You should see the history of test runs for that test:

    Graphs in Allure Report

    Graphs allow you to see different statistics collected from the test data: status breakdown or severity and duration diagrams.

    Timeline in Allure Report

    Timeline tab visualizes retrospective of tests execution, allure adaptors collect precise timings of tests, and here on this tab, they are arranged accordingly to their sequential or parallel timing structure.

    Behaviors of Allure Report

    This tab groups test results according to Epic, Feature, and Story tags.

    Packages in Allure Report

    The packages tab represents a tree-like layout of test results, grouped by different packages.

    We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!