protractor-api-resource
API testing is now made simple with protractor. protractor-api-resource is a REST Client framework created using request npm module to use in protractor tests for making API calls. Register all service endpoints as individual service methods and reuse the same inside the test. Inspired from angular-resource project.
Features
- Create reusable methods for all service end points once and use it throughout the tests.
- Supports all rest API methods(GET,POST,PUT,PATCH and DELETE).
- Supports authentication protected endpoints.
- Can be used in any node js projects.
Why is it useful?
If you are using Protractor for e2e testing and you need to get test data from API then this module will come handy with lot of predefined functionalities.
Usage
Install using npm
$ npm install protractor-api-resource
Example
Using javascript, first import the npm module in your tests.
const apiResource = ProtractorApiResource
Javascript
;
Thats it. Now you can directly access the service endpoints from your tests as below.
;
For POST,PUT and PATCH calls, you can also send payloads like,
;
toJson()
method will parse the API respose and returns the respective JSON object and toSting()
willl return the plain string.
What if the service enpoints are protected with authentication?
It's very simple. Just mention the type of authentication at the time of creating a object.
Basic authentication.
var apiClient = "https://jsonplaceholder.typicode.com/";
Token based authentication.
var apiClient = "https://jsonplaceholder.typicode.com/";
You can also modify the authencation type any time inside the tests using apiClient .withTokenAuthentication(token)