API Testing with Cucumber, The Most Popular BDD Framework
API testing with Cucumber is really innovative way of using BDD style feature file. cucumber-api lets one validate public APIs JSON response in blazingly fast time.
Inspired by cucumber-api-steps.
Checkout sample to see cucumber-api in action.
Installation
Add cucumber-api
gem to your Gemfile
:
gem 'cucumber-api'
Require cucumber-api
in your Cucumber’s env.rb
:
require 'cucumber-api'
Configuration
Verbose logging: enable verbose logging of API calls and responses by settingcucumber_api_verbose=true
in your ENV
, preferably via your cucumber.yml
# config/cucumber.yml
##YAML Template
---
verbose : cucumber_api_verbose=true
Usage
Available steps
Preparation steps
Specify your request header’s Content-Type
and Accept
. The only supported option for Accept
isapplication/json
at the moment.
Given I send and accept JSON
Given I send "(.*?)" and accept JSON
Specify POST body
When I set JSON request body to '(.*?)'
When I set form request body to:
| key1 | value1 |
| key2 | {value2} |
| key3 | file://path-to-file |
Or from YAML/JSON file
When I set request body from "(.*?).(yml|json)"
Example:
Given I send "www-x-form-urlencoded" and accept JSON
When I set JSON request body to '{"login": "email@example.com", "password": "password"}'
When I set form request body to:
| login | email@example.com |
| password | password |
When I set request body from "data/json-data.json"
When I set request body from "data/form-data.yml"
Request steps
Specify query string parameters and send an HTTP request to given URL with parameters
When I send a (GET|POST|PATCH|PUT|DELETE) request to "(.*?)"
When I send a (GET|POST|PATCH|PUT|DELETE) request to "(.*?)" with:
| param1 | param2 | ... |
| value1 | value2 | ... |
Temporarily save values from the last request to use in the next request in the same scenario:
When I grab "(.*?)" as "(.*?)"
The saved value can then be used to replace {placeholder}
in the next request.
Example:
When I send a POST request to "http://example.com/token"
And I grab "$..request_token" as "token"
And I grab "$..access_type" as "type"
And I send a GET request to "http://example.com/{token} with:
| type | pretty |
| {type} | true |
Assume that http://example.com/token have an element {"request_token": 1, "access_type": "full"}
, cucumber-api will execute the followings:
- POST http://example.com/token
- Extract the first
request_token
andaccess_type
from JSON response and save it for next request - GET http://example.com/1?type=full&pretty=true
- Clear all saved values
This will be handy when one needs to make a sequence of calls to authenticate/authorize API access.
Assert steps
Verify:
- HTTP response status code
- JSON response against a JSON schema conforming to JSON Schema Draft 4
- Adhoc JSON response key-value type pair, where key is a JSON path
Then the response status should be "(\d+)"
Then the JSON response should follow "(.*?)"
Then the JSON response root should be (object|array)
Then the JSON response should have key "([^\"]*)"
Then the JSON response should have (required|optional) key "(.*?)" of type (numeric|string|array|boolean|numeric_string|object|array|any)( or null)
Example:
Then the response status should be "200"
Then the JSON response should follow "features/schemas/example_all.json"
Then the JSON response root should be array
Then the JSON response should have key "id"
Then the JSON response should have optional key "format" of type string or null
Also checkout sample for real examples. Run sample with the following command:
cucumber -p verbose
Response caching
Response caching is provided for GET requests by default. This is useful when you have a Scenario Outline or multiple Scenarios that make GET requests to the same endpoint.
Only the first request to that endpoint is made, subsequent requests will use cached response. Response caching is only available for GET method.
Dependencies
- cucumber for BDD style specs
- jsonpath for traversal of JSON response via JSON path
- json-schema for JSON schema validation
- rest-client for HTTP REST request
Read Similar Posts
This is a fantastic blog from which people can learn a lot. It is very informative and is explained in…
Thanks for sharing such knowledgeable Content on Automation testing, to know how to enhance the performance of ERP's like Oracle…
Thanks.. this list is much needed.
This one is also good to use. Thanks for sharing this.. Might be we can also add it in list…
How about youtube-dl?