Protractor HTTP Client
Protractor HTTP Client (npm package `protractor-http-client`) is a utility library designed to simplify making HTTP requests within Protractor end-to-end tests. It is currently at version 1.0.4, indicating a stable release, though the package has not seen updates since 2017. The library acts as a wrapper around the now-deprecated `request` package, allowing testers to perform GET, POST, PUT, DELETE, and other HTTP operations. Its primary differentiation lies in its tight integration with Protractor's control flow, ensuring that HTTP calls automatically resolve before subsequent Protractor `browser` or `element` commands are executed. This is particularly useful for setting up test data via REST APIs before a test runs, or for cleaning up resources after a test completes, avoiding manual promise handling common with plain `http` or `request` modules in a Protractor context.
Common errors
-
TypeError: browser.wait is not a function or is deprecated
cause The library's core functionality depends on Protractor's `browser.wait` and control flow. This error occurs if Protractor's global `browser` object is not correctly configured or available, or if an incompatible version is used.fixEnsure Protractor is correctly set up and running, and that `browser` is available in your test scope. Verify that `protractor-http-client` is used exclusively within a Protractor test environment and with a compatible Protractor version (e.g., Protractor 3+). -
Error: connect ECONNREFUSED <your_api_host>:<port>
cause The HTTP client could not establish a connection to the specified API endpoint. This typically indicates the target API server is not running, is unreachable due to network issues, or the URL/port is incorrect.fixVerify that the API server is running and accessible from where the tests are executed. Check the base URL and endpoint paths passed to `HttpClient` and its methods for typos or incorrect ports. Ensure no firewall or proxy is blocking the connection. -
ResponsePromise is not defined
cause This error occurs in a TypeScript environment if `ResponsePromise` (or `JsonPromise`) is used as a runtime value or is not correctly imported for type annotations.fixEnsure that `ResponsePromise` and `JsonPromise` are only used for type annotations in TypeScript and are imported using `import type { ResponsePromise, JsonPromise } from 'protractor-http-client';`. They are not constructible classes or runtime objects. -
HttpError: Request failed with status code 401 (Unauthorized)
cause The API endpoint returned an HTTP error status code (e.g., 401, 403, 404, 500) and `http.failOnError` was set to `true`, causing the request to throw an error.fixExamine the API endpoint, request headers, and body to determine why the server returned the error. This is often due to incorrect authentication tokens, missing data, or an invalid endpoint configuration. The error correctly indicates an API issue that needs to be addressed.
Warnings
- breaking The underlying Protractor framework, which this library tightly integrates with, has been officially deprecated. While `protractor-http-client` itself is functional, its primary use case is no longer supported by the broader ecosystem, making it unsuitable for new projects.
- breaking This library's core functionality relies on the `request` package, which has been deprecated and is no longer maintained. This presents potential security risks (CVEs will not be patched), compatibility issues with newer Node.js versions, and lack of modern HTTP/2 or Fetch API support.
- gotcha Requests do not automatically fail on non-2xx HTTP status codes by default. A 404 or 500 response will still resolve the promise without an error, requiring explicit status code checks in tests.
- gotcha The `protractor-http-client` library appears to be abandoned, with no commits since 2017. This implies no support for newer Node.js versions, security vulnerability patching, or feature enhancements, making it potentially unstable or insecure in modern environments.
Install
-
npm install protractor-http-client -
yarn add protractor-http-client -
pnpm add protractor-http-client
Imports
- HttpClient
import HttpClient from 'protractor-http-client'
import { HttpClient } from 'protractor-http-client' - ResponsePromise, JsonPromise
import { ResponsePromise, JsonPromise } from 'protractor-http-client'import type { ResponsePromise, JsonPromise } from 'protractor-http-client'
Quickstart
import { HttpClient, ResponsePromise } from "protractor-http-client";
import { browser, by, element, expect } from "protractor"; // Assuming these are globally available or imported from 'protractor'
// Instantiate the HTTP client with a base URL
const http = new HttpClient("https://api.example.com/");
http.failOnError = true; // Recommend setting this to ensure API errors throw exceptions
describe("the login page", () => {
// Setup a user via API before each test
beforeEach(async () => {
const postResponse: ResponsePromise = http.post("/users", {
username: "testuser",
password: "securepassword"
});
// protractor-http-client automatically waits for the promise to resolve
expect(postResponse.statusCode).toEqual(200); // Verify creation was successful
});
// Cleanup the user via API after each test
afterEach(async () => {
const deleteResponse: ResponsePromise = http.delete("/users/testuser");
expect(deleteResponse.statusCode).toEqual(200); // Verify deletion was successful
});
it("will allow login with new user", async () => {
// Now you can use the browser to login with the new user
await browser.get("/login");
await element(by.id("username")).sendKeys("testuser");
await element(by.id("password")).sendKeys("securepassword");
await element(by.id("loginButton")).click(); // Assuming a login button
// Assert successful login, e.g., URL change or element presence
expect(await browser.getCurrentUrl()).toContain("/dashboard");
expect(await element(by.id("welcomeMessage")).getText()).toContain("Welcome, testuser");
});
});