Testing Web APIs
Overview
Starting with Cycle 2.15, we have improved the way to call web application programming interfaces (APIs) as part of your test automation. We have introduced a new API editor to create and edit API requests. The API requests can then be used in one or more feature files with the new I call API Step.
Using the API Editor
You can create a new API request using the API Editor by either clicking File -> New -> Cycle API in the menu or by using the Control + K keyboard shortcut.
Review the following information about the API Editor.
- You can set the request method (GET, POST, PUT, PATCH, DELETE).
- You can set the request URL.
- This can be a static URL, such as https://jsonplaceholder.typicode.com/posts
- It can be a reference to a Cycle variable, such as
${url}
- It can be a mix of multiple Cycle variables and static text, such as
${protocol}://${url}:${port}/api/v1
- Supported body types include JSON, XML, and x-www-form-urlencoded.
- The body can be static text or include one or more Cycle variables, such as
{ "title": "${title}", "body": "Hello, ${name}" }
- The body can be static text or include one or more Cycle variables, such as
- A convenient table style interface is available for defining any required http headers, query parameters, or x-www-form-urlencoded body.
- Both the keys and values can be static text, Cycle variables, or a mix of both.
- If your API requires authentication, you can chose from basic or bearer token and provide the necessary credentials.
- Best practices for securely handling credentials are to reference Cycle variables rather than hard coding values.
- More information on injecting secrets and passwords into Cycle can be found in this Knowledge Base Article.
- Additional examples for securely injecting secrets can be found in this example project.
- In the Variables Inspector on the right, you can easily see a list of all variables referenced in the API request. This is for ease of use when referencing the API request from a feature. You can quickly and conveniently find a list of all variables without having to navigate all parts of the request by opening the API editor.
- Execute the request to validate API information entered prior to running a feature.
- View output of the API response.
Save your API request by either clicking File -> Save in the menu or using Control + S. When saving your API requests, you can group them in folders just like any other file type in Cycle.
Validating the API from within the editor
To execute the API request from within the editor, use the Save & Test
button right of the request URL input. Then view the results to ensure that the response is what was expected and make any other necessary changes.
Calling the API from a Feature File
To use the API request from a feature file, use the I call api Step with the path within the project to the .api file. A sample scenario is below.
Scenario: Update Data API
Given I assign value "application/json" to unassigned variable "contentType"
And I assign value "My Updated Title" to unassigned variable "title"
When I call api "Requests/update_data.api"
And I verify http response had status code 200
Then I assign http response body to variable "responseBody"
And I echo $responseBody
And I assign value from JSON $responseBody with path "/userId" to variable "userID"
And I echo $userID
If any Cycle variables referenced in the API request are not present when the I call api Step is invoked, the Step will fail with an error stating Missing required variable with a comma-separated list of missing variables. If no URL is defined when the Step is invoked, the Step will fail with a URL is empty error message. The Step will fail if Cycle is unable to complete the request, as in such a case as the URL is unreachable. The Step will pass when the API provides a response. Additional validations can then be subsequently performed to validate the response's HTTP status, headers, or content of the body.
Sample Project
A video introduction of the API Editor is available.
The sample project from that video can be found here.