top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

API Automation With Cucumber 6, Rest-Assured and Allure-Report

You are going to learn to setup a java project with Cucumber 6, Rest-Assured and Allure-Report.

Required Software:

  • Install JDK 11 & Apache Maven 3.6

  • Eclipse

  • Allure

  • Clone/download the backend API lms-admin-rest-service

Allure — an open-source framework designed to create test execution reports


Allure Installation:

  • Download the latest version as zip archive from Maven Central. (this project used allure-commandline-2.14.0

  • Unpack the archive to allure-commandline directory.

  • Navigate to bin directory.

  • Set the bin paths of allure folders in path environment variables



allure- environment path variable setup

  • Check the allure version in command line using

allure — version

Project Setup:

Create Maven Project:

  1. Open your Eclipse.

  2. Click on File -> New -> Other.

  3. In Select a wizard window -> search Maven project -> Select Maven Project -> click Next

  4. New Maven Project window -> click create a simple project →click Next

  5. Enter Group Id and Artifact Id and Finish.

  6. Under the src/test/resources directory create a new directory called as features

  7. Add a new package called StepDefinitions under src/test/java .

  8. Add a new package called runners under src/test/java.

Maven Dependencies :

Please add the below dependencies in pom.xml

  1. rest-assured

	<dependency>
		<groupId>io.rest-assured</groupId>
		<artifactId>rest-assured</artifactId>
		<version>4.4.0</version>
	</dependency>
	<dependency>
		<groupId>io.rest-assured</groupId>
		<artifactId>json-path</artifactId>
		<version>4.4.0</version>
	</dependency>
	<dependency>
		<groupId>io.rest-assured</groupId>
		<artifactId>json-schema-validator</artifactId>
		<version>4.4.0</version>
	</dependency>

2. json-simple

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

3. cucumber

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>6.10.4</version>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>6.10.4</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.qameta.allure</groupId>
    <artifactId>allure-cucumber6-jvm</artifactId>
    <version>2.13.6</version>
</dependency>
<dependency>
    <groupId>io.qameta.allure</groupId>
    <artifactId>allure-cucumber6-jvm</artifactId>
    <version>2.13.6</version>
</dependency>

4. Junit

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

Maven Plugins:

Please add below plugins in pom.xml

1.maven-compiler-plugin

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
	<configuration>
		<source>11</source>
		<target>11</target>
	</configuration>
</plugin>

2.maven-surefire-plugin


<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>3.0.0-M5</version>
	<configuration>
		<systemProperties>
			<property>
				<name>allure.results.directory</name>
				<value>${project.build.directory}/allure-results</value>
			</property>
			<property>
				<name>allure.link.issue.pattern</name>
				<value>https://example.org/issue/{}</value>
			</property>
			<property>
				<name>allure.link.tms.pattern</name>
				<value>https://example.org/tms/{}</value>
			</property>
		</systemProperties>
		<testFailureIgnore>true</testFailureIgnore>
		<argLine>
						-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
						-Dcucumber.plugin="io.qameta.allure.cucumber6jvm.AllureCucumber6Jvm"
		</argLine>
	</configuration>
	<dependencies>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.9.7</version>
		</dependency>
	</dependencies>
</plugin>

Reference pom.xml

Please test the project dependency -> Right click pom.xml → Run as -> Maven install





Writing your first Feature File :

Lets now start by creating it Right click on the src/test/resources/features directory and New -> File -> End2End_Test.feature

End2End_Test.feature


Feature: End to End Tests for Lms admin rest service API

Scenario: Authorized User is able to view all program list
	When load all the programs
	Then all programs are displayed
	
Scenario: Authorized User is able to add program
	When Authorized User add program
	Then program is added

Scenario: Authorized User is able to view program
	When Authorized User view program
	Then program is displayed

Scenario: Authorized User is able to update program
	When Authorized User update program
	Then program is updated

Scenario: Authorized User is able to delete program
	When Authorized User delete program
	Then program is deleted

Execute Feature File:

  • For running feature file select feature file -> Run As -> Cucumber Feature

  • You will get the code suggestion in console window like below


You can implement missing steps with the snippets below:

@When("load all the programs")
public void load_all_the_programs() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@Then("all programs are displayed")
public void all_programs_are_displayed() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@When("Authorized User add program")
public void authorized_user_add_program() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@Then("program is added")
public void program_is_added() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@When("Authorized User view program")
public void authorized_user_view_program() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@Then("program is displayed")
public void program_is_displayed() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@When("Authorized User update program")
public void authorized_user_update_program() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@Then("program is updated")
public void program_is_updated() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@When("Authorized User delete program")
public void authorized_user_delete_program() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@Then("program is deleted")
public void program_is_deleted() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

  • Create step definition class file Steps in StepDefinitions package and paste the suggested code like below


  • Modify the code to get output from Rest API using rest-assured library

  • Please refer below code for getting output from Rest API using rest-assured library and Steps.java

package stepDefinitions;

import static io.restassured.RestAssured.given;

import org.json.simple.JSONObject;
import org.junit.Assert;

import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;

public class Steps {

	private static Response response;

	private static final String BASE_URL = "https://lms-admin-rest-service.herokuapp.com";
	private static final String USERNAME = "admin";
	private static final String PASSWORD = "password";
	private static int programId = 0;

	@When("load all the programs")
	public void load_all_the_programs() {
		response = given().auth().basic(USERNAME, PASSWORD).when().get(BASE_URL + "/programs");
	}

	@Then("all programs are displayed")
	public void all_programs_are_displayed() {
		String programId = response.jsonPath().getString("programId[0]");
		Assert.assertNotNull(programId);
	}

	@When("Authorized User add program")
	public void authorized_user_add_program() {

		JSONObject request = new JSONObject();
		request.put("programName", "cucumber learning");
		request.put("programDescription", "cucumber learning");
		request.put("isOnline", false);

		response = given().auth().basic(USERNAME, PASSWORD).header("Content-type", "application/json").and()
				.body(request).when().post(BASE_URL + "/programs").then().extract().response();

		System.out.println("Response Body is: " + response.getBody().asString());

	}

	@Then("program is added")
	public void program_is_added() {
		JsonPath jsonPathEvaluator = response.jsonPath();

		Integer apiProgramId = jsonPathEvaluator.get("programId");

		Assert.assertNotNull(apiProgramId);
		programId = apiProgramId.intValue();
	}

	@When("Authorized User view program")
	public void authorized_user_view_program() {

		response = given().auth().basic("admin", "password").when().get(BASE_URL + "/programs/" + programId);

		System.out.println(response.asString());

	}

	@Then("program is displayed")
	public void program_is_displayed() {
		// First get the JsonPath object instance from the Response interface
		JsonPath jsonPathEvaluator = response.jsonPath();

		// Then simply query the JsonPath object to get a String value of the node
		// specified by JsonPath: programId (Note: You should not put $. in the Java
		// code)
		Integer apiProgramId = jsonPathEvaluator.get("programId");

		// Validate the response
		Assert.assertNotNull(apiProgramId);
		Assert.assertTrue(programId == apiProgramId.intValue());
	}

	@When("Authorized User update program")
	public void authorized_user_update_program() {
		JSONObject request = new JSONObject();
		request.put("programName", "cucumber learning modified");
		request.put("programDescription", "cucumber learning");
		request.put("isOnline", false);

		response = given().auth().basic("admin", "password").header("Content-type", "application/json").and()
				.body(request).when().put(BASE_URL + "/programs/" + programId).then().extract().response();

		System.out.println("authorized_user_update_program Response Body is: " + response.getBody().asString());
	}

	@Then("program is updated")
	public void program_is_updated() {

		JsonPath jsonPathEvaluator = response.jsonPath();

		// Then simply query the JsonPath object to get a String value of the node
		// specified by JsonPath: programId (Note: You should not put $. in the Java
		// code)
		String programNameUdt = jsonPathEvaluator.get("programName");
		System.out.println("From APi " + programNameUdt);

		Assert.assertEquals("cucumber learning modified", programNameUdt);
	}

	@When("Authorized User delete program")
	public void authorized_user_delete_program() {

		response = given().auth().basic("admin", "password").header("Content-type", "application/json").when()
				.delete(BASE_URL + "/programs/" + programId).then().extract().response();

	}

	@Then("program is deleted")
	public void program_is_deleted() {
		System.out.println("Response Body is: " + response.getBody().asString());

		int statusCode = response.getStatusCode();
		System.out.println(response.asString());
		Assert.assertEquals(statusCode, 200);
	}

}
  • Create TestRunner class in runners package and modify the code like below

package runners;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;


@RunWith(Cucumber.class)
@CucumberOptions(
        features = "functional-tests",
        glue = {"stepDefinitions"}
)
public class TestRunner {

}

  • We have completed all coding and now we are going to execute test and generate reports.

  • For executing Test Please Select pom.xml -> Run as -> mvn test


  • Please check the console window out output


  • Please check the target folder, allure-results folder created from system after successful execution


  • Select Target Folder- >right click-> Show in Local Terminal - >Terminal


  • In Terminal window Type allure serve command -> system will display the output like below

and system open browser and display the reports like below


Now we have learned how to do Api Testing using Java, cucumber and Rest-assured and generated reports using Allure.

Thanks for reading… Enjoy coding…

931 views1 comment

Recent Posts

See All
bottom of page