RestAssured – API Automation & Building Cucumber Framework

Published On: 10 January 2024.By .
  • Quality Engineering
  • Selenium
  • WebDriver

API automation refers to the process of automating the testing, deployment, and management of Application Programming Interfaces (APIs). APIs allow different software systems to communicate and interact with each other. API automation involves using automated tools and scripts to perform tasks related to API testing, monitoring, and integration.

Key aspects of API automation include:

  1. Testing Automation:
    • Functional Testing: Automated tests are created to verify that the API functions as expected. This involves testing various inputs, outputs, and scenarios to ensure the API behaves correctly.
    • Regression Testing: Automated tests are run to ensure that changes or updates to the API do not negatively impact existing functionalities.
    • Performance Testing: Automated tools can simulate high loads and stress conditions to assess the API’s performance and scalability.

 

Practice APIs : Import these curl into postman which we going to automate.
Get Request: 

curl –location ‘https://jsonplaceholder.typicode.com/posts/1’

Post Request: 

curl –location ‘https://jsonplaceholder.typicode.com/posts’ \

–header ‘Content-Type: application/json’ \

–data ‘{“userId”: 1, “title”: “New Post”, “body”: “This is the body of the new post.”}’

Put Request: 

curl –location –request PUT ‘https://jsonplaceholder.typicode.com/posts/1’ \

–header ‘Content-Type: application/json’ \

–data ‘{“id”: 1, “title”: “Updated Post”, “body”: “This is the updated body of the post.”}’

Patch Curl : 

curl –location –request PATCH ‘https://jsonplaceholder.typicode.com/posts/1’ \

–header ‘Content-Type: application/json’ \

–data ‘{“body”: “This is a partial update of the body.”}’

Delete Curl : 

curl –location –request DELETE ‘https://jsonplaceholder.typicode.com/posts/1

Create Project Structure : Use any IDE to create a blank Maven project, Intellij IDEA or Eclipse.

Create Packages as below

  1. cucumber.Options
  2. features
  3. pojo [ It can be skipped as of now we’ll be using string format for payloads ]
  4. resources
  5. stepDefinations

Let’s Automate

Add Dependencies to pom.xml

  1. Now Create one gloabal.properties file under resources package and place BaseUrl: global.properties

 

2. Get end point and put it in APIresources Enum file:  APIresources.java

Note : Given constant name “createblogAPI” should be called in Feature file. It’s a Enum class.

An enum is a special “class” that represents a group of constants (unchangeable variables, like final variables). To create an enum , use the enum keyword (instead of class or interface), and separate the constants with a comma.

Utils/BaseSteps : utils.java

FeatureFile : AddBlog.feature

      Now need to setup feature file : there are few syntax we need to follow 

  1. At the top of this we will define”Feature: ” Keyword
  2. Then we will define Scenario / Scenario outline ( if multiple inputs ) 
  3. Scenario Outline : Verify Blog added successfully 
  4. Then Follow given, when, then
Note : “” Represents variable parts, “<>” Represents multiple inputs

So let suppose in next case we need to validate “message” is “successful” then we will only put these values in last line in new case  

i.e. And “message” in response body “successful”

StepDefination File :

Now need to set up a step definition file and all our java code will  be there. It supports  “Gherkin” syntax so to achieve this we can run our feature file once and  in error response we can get it. Carefully copy all the steps from undefined steps : And paste to step definition file.

Now extend utils class here where we defined and setup our base url, loggings :  stepDefinations.java

Method 1 :

Here we will be setting up a dynamic payload ( API body ) and base url Convert Json to string first and place it in “TestDatabuild” file We can use this website https://jsontostring.com/ to convert json to string.

But here our pay load is dynamic lets place variable here carefully between “+variable+”

Create a new file :  TestDatabuild.java 

Explanation : 

Move to next Method : It is dynamically setting up request type

Move to next method : These are only for validation purpose

Next create test runner file and give annotation and path to feature and step def file  : Testrunner.java

Run Test Runner File :

 

I hope you enjoyed this informative blog. Feel free to ask any questions or queries in comments or  Join on linkedIn. 

 

Related content

That’s all for this blog