Building Cucumber BDD API Testing Framework from scratch: Part 1
By: Prem Singh Rathore
Tags: Java, Test Automation, Cucumber, Framework, API

Before we deep dive into it, let me give you a brief description of how we will approach it. I will give you a step by step guidelines for developing the framework and it might be a three-part series where part 1 will be about setting up the project and make the code work, part 2 will be about optimizing the code, and finally, in part 3 we will develop an end to end framework.

Below given a list of the APIs which we will be using in this framework for your practice:

Library API : BaseURI : http://216.10.245.166 1. Resource : Library/Addbook.php Method : POST Input Payload : Json: { “name”:”Learn Appium Automation with Java”, “isbn”:”bcd”, “aisle”:”227", “author”:”John foe” } Output Json { “Msg”: “successfully added”, “ID”: “bcd227” } 1. Resource : /Library/GetBook.php?AuthorName=somename Method : GET Output Json : Output the array of Json object books with all below details { Name : “bookname” ( String) Isbn : “A2fdsf” (String) Aisle : 32 (Integer) } 1. Resource : Library/GetBook.php?ID=3389 — Method : GET Output Json : { “book_name”: “Selenium automation using Java”, “isbn”: “a23hd738”, “aisle”: “1223” } 1. Resource :/Library/DeleteBook.php Method : POST Input Payload : Json: { “ID” : “a23h345122332” } Output Response : { msg : book is successfully deleted” }

Step 1: We need to create a maven project in eclipse as shown in the images below

In eclipse go to File and select New project

Select maven project and click on next

Now on the very next window click on the next one more time

Select maven-quickstart-archetype and click on next

Now provide the Group ID and Artifact ID of your project and click on the finish button

And under package explorer, you will see your project

Step 2: Now we need to provide dependencies in our pom.xml file, for now just provide the below dependencies. Note: I have given the artifact id which you search in the Maven repository and download from there.

<artifactId>cucumber-java</artifactId>, <artifactId>cucumber-junit</artifactId>, <artifactId>rest-assured</artifactId>, <artifactId>jackson-databind</artifactId>, <artifactId>junit</artifactId>.

Note: Make sure the group id is io.cucumber for cucumber-related dependencies.

Step 3: Create all three main folders: Features folder, Step Definition folder, and Test Runner folder.

Create a package called features

Create a feature file under the features package which we have just created

Create a Step Definition file under the step definitions package 📦

Now create a Test Runner file under cucumber.options package

Step 4: Now we need to start writing code

Your feature file will look like this:

# can be provided if we are not considering that step

Your Step Definition file will look like below:

and your TestRunner files will look something like this:

Step 5: Run your code with TestRunner class

Once you run the file you will see a green bar under the Junit tab, which means that your code got executed successfully without any error

So, that was about setting up API Testing Framework and running your code for the first time. In the second part, we will look into how we can optimize our code to make it more user-friendly.

Here is the link to the GitHub repository to download the code

Cheers 🍻