npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

protractor-api-resource

v1.0.3

Published

This library extends request npm module. It helps write easy protractor tests when test data are objtained from API response.

Downloads

1,086

Readme

protractor-api-resource

Build Status

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 = require("protractor-api-resource").ProtractorApiResource
Javascript 
describe("Test response for all REST API methods", function () {

    var apiClient, serviceEnpoints = {
        getPosts: {
            path: "/posts/:postId:"
        },
        createPost: {
            path: "/posts",
            method: "POST"
        },
        updatePost: {
            path: "/posts/:postId:",
            method: "PUT"
        },
        patchPost: {
            path: "/posts/:postId:",
            method: "PATCH"
        },
    };

     beforeAll(function () {
        apiClient = new apiResource("https://jsonplaceholder.typicode.com/");
        apiClient.registerService(serviceEnpoints);
    });
});

Thats it. Now you can directly access the service endpoints from your tests as below.

it("Test GET method", function (done) {
    var expectedResponse = {
        "userId": 1,
        "id": 1,
        "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
        "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
    };

    apiClient.getPosts({postId: 1}).toJSON().then(function (actualResponse) {
        expect(actualResponse).toEqual(expectedResponse);
        done();
    });
});

For POST,PUT and PATCH calls, you can also send payloads like,

it("Test POST method", function (done) {
    var payLoad = {
        title: 'foo',
        body: 'bar',
        userId: 1
    };

    var expectedResponse = {
        id: 101,
        title: 'foo',
        body: 'bar',
        userId: 1
    };
    //First parameter is for query params and second parameter is for request payload.
    apiClient.createPost({}, payLoad).toJSON().then(function (actualResponse) {
        expect(actualResponse).toEqual(expectedResponse);
        done();
    });
});

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 = new apiResource("https://jsonplaceholder.typicode.com/").withBasicAuth(username,password);
Token based authentication.
var apiClient = new apiResource("https://jsonplaceholder.typicode.com/").withTokenAuthentication(token);

You can also modify the authencation type any time inside the tests using apiClient .withTokenAuthentication(token)

Reference

https://github.com/sudharsan-selvaraj/protractor-api-resource/blob/master/test/spec/apiWithNoAuthentication.spec.js