protractor-http-client
HTTP Client library to use in protractor tests
What this library is about
Are you using protractor ? me too, and it's awesome! This library is a little utility that allows you to make any HTTP call (GET, PUT, POST, ...) , leveraging the powerful request library.
This library allows you to call HTTP services before, after or during interactions within the browser.
Why is it useful?
for example, for setting up test data via REST API before a test is run, or cleaning up after a test has finished running.
It won't be easy to do the same with plain 'http' or 'request' module, as you will have to wait for the HTTP call promise to complete, before using the protractor browser calls.
Instead, you have to just call e.g.
httpbrowser // this will wait for the previous call to finish// initiate login with "marco" user
and the subsequent calls to protractor API will wait until the previous call finishes.
Requirements
- Protractor 3+ (tested with 3.3.0 and 4.0.14)
- Node 4.2+
Usage
If using Javascript specs
const HttpClient = HttpClient
If using Typescript specs
// HTTP GET;// HTTP POST with JSON, automatically sets Content-Type: application/jsonhttp.post"/users", );// HTTP POST, with form data and custom content typehttp.post"/form", "param1=value1¶m2=value2", );// make HTTP calls fail and throw an exception when response code is not 2xx// default behavior is to continue on any HTTP status codehttp.failOnError = true
You have get
, post
, put
, delete
methods available.
get
and delete
methods do NOT accept request body.
For more complex requests, use the request
method shown below.
Passing complex request options
You can pass any options accepted by the request library, by passing an object to the request method
let options = ... http
Helper methods on response to check status and body
expectresponse.statusCode.toEqual200expectresponse.header"Content-Type".toEqual"application/json"expectresponse.stringBody.toEqual'{"username":"marco","password":"bigsecret"}'expectresponse.jsonBody.get"username".toEqual"marco"
Example spec with API setup