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

jaffamonkey

v1.0.0

Published

cucumberjs api

Downloads

3

Readme

apickli - REST API integration testing framework with cucumber.js

NPM version

NPM

Apickli is a REST API integration testing framework based on cucumber.js.

It provides a gherkin framework and a collection of utility functions to make API testing easy and less time consuming.

Apickli is also available as an NPM package.

Cucumber.js is JavaScript & Node.js implementation of Behaviour Driven Development test framework - Cucumber. Cucumber.js is using Gherkin language for describing the test scenarios in BDD manner.

How to start - a simple tutorial

Apickli depends on cucumber.js being installed on your system. You can do this by installing cucumber.js globally:

$ npm install -g cucumber

Start new project

Below steps will help you start a new test project from scratch.

1. Folder structure

Let's start a new integration testing project for an API called myapi. The folder structure will need to match the structure expected by cucumber.js:

test/
---- features/
--------- myapi.feature
--------- step_definitions/
-------------- myapi.js
---- package.json

Features directory contains cucumber feature files written in gherkin syntax. step_definitions contains the JavaScript implementation of gherkin test cases. Check out the GitHub repository for example implementations covering most used testing scenarios.

2. Package.json

This can be an example package.json file for our project:

{
	"name": "myapi-test",
	"version": "1.0.0",
	"description": "Integration testing for myapi v1",
	"dependencies": {
		"apickli": "latest"
	}
}

3. Install dependencies

Now we can get the project dependencies installed:

$ npm install

4. Scenario definitions

Let's start with the scenario file called myapi.feature. For more examples of feature and scenario definitions, check out test folder.

Feature:
	Httpbin.org exposes various resources for HTTP request testing
	As Httpbin client I want to verify that all API resources are working as they should

	Scenario: Setting headers in GET request
		Given I set User-Agent header to apickli
		When I GET /get
		Then response body path $.headers.User-Agent should be apickli

5. Get apickli-gherkin steps

We now need the corresponding step definitions that implement the steps in our scenario. Apickli has a collection of steps already implemented - ready to be included in your project: source/apickli/apickli-gherkin.js. It is included in the NPM package so you can symlink to it from under your local node_modules/apickli folder - see gherkin expressions for more information and help on symlink.

6. Step_definitions for this project

Now we need a step definition file specific for this project, let's call it myapi.js:

/* jslint node: true */
'use strict';

var apickli = require('apickli');

module.exports = function() {
	// cleanup before every scenario
	this.Before(function(scenario, callback) {
		this.apickli = new apickli.Apickli('http', 'httpbin.org');
		callback();
	});
};

7. Run tests with cucumber.js

The following will run our scenario (in the project directory):

$ cucumber-js features/httpbin.feature
....

1 scenario (1 passed)
3 steps (3 passed)

Step timeout

Cucumber.js default step timeout is 5000ms. Follow this guide to change it for your steps.

Grunt integration

You can also use Grunt task runner to run the tests.

1. Start by adding a Gruntfile.js to the project root:

'use strict';

module.exports = function(grunt) {
	grunt.initConfig({
		cucumberjs: {
			src: 'features',
			options: {
				format: 'pretty',
				steps: 'features/step_definitions'
			}
		}
	});

	grunt.loadNpmTasks('grunt-cucumber');
	grunt.registerTask('tests', ['cucumberjs']);
}

2. Add grunt and grunt-cucumber dependencies to package.json:

	...
	"dependencies": {
		"apickli": "latest",
		"grunt": "latest",
		"grunt-cucumber": "latest"
	}
	...

3. Install the new dependencies:

npm install

4. Now you can run the same tests using grunt:

$ grunt tests
Running "cucumberjs:src" (cucumberjs) task

Feature:
  Httpbin.org exposes various resources for HTTP request testing
  As Httpbin client I want to verify that all API resources are working as they should


  Scenario: Setting headers in GET request                         # features/httpbin.feature:5
    Given I set User-Agent header to apickli                       # features/httpbin.feature:6
    When I GET /get                                                # features/httpbin.feature:7
    Then response body path $.headers.User-Agent should be apickli # features/httpbin.feature:8


1 scenario (1 passed)
3 steps (3 passed)

Done, without errors.

Gulp Integration

You can also use Gulp to run the tests.

1. Start by adding a Gulpfile.js to the project root:

var gulp = require('gulp');
var cucumber = require('gulp-cucumber');
 
gulp.task('test', function() {
    return gulp.src('features/*')
			.pipe(cucumber({
				'steps': 'features/step_definitions/*.js',
				'format': 'pretty'
			}));
});

2. Add gulp and gulp-cucumber dependencies to package.json:

...
	"gulp": "latest",
	"gulp-cucumber": "latest"
...

3. Install local dependencies

$ npm install

4. Install gulp globally

$ npm install -g gulp

See https://github.com/gulpjs/gulp/blob/master/docs/getting-started.md.

5. Run tests using gulp

$ gulp test

Gherkin Expressions

The following gherkin expressions are implemented in apickli source code source/apickli/apickli-gherkin.js:

GIVEN:
	I set (.*) header to (.*)
	I set body to (.*)
	I pipe contents of file (.*) to body
	I have basic authentication credentials (.*) and (.*)
	I set bearer token
	I set query parameters to (data table with headers |parameter|value|)
	I set headers to (data table with headers |name|value|)
	
WHEN:
	I GET $resource
	I POST to $resource
	I PUT $resource
	I DELETE $resource
	I PATCH $resource
    I request OPTIONS for $resource
	
THEN:
	response code should be (\d+)
	response code should not be (\d+)
	response header (.*) should exist
	response header (.*) should not exist
	response header (.*) should be (.*)
	response header (.*) should not be (.*)
	response body should be valid (xml|json)
	response body should contain (.*)
	response body should not contain (.*)
	response body path (.*) should be (.*)
	response body path (.*) should not be (.*)
    response body path (.*) should be of type array
    response body path (.*) should be of type array with length (\d+)
    response body should be valid according to schema file (.*)
	I store the value of body path (.*) as access token
	I store the value of response header (.*) as (.*) in scenario scope
	I store the value of body path (.*) as (.*) in scenario scope
	value of scenario variable (.*) should be (.*)
	I store the value of response header (.*) as (.*) in global scope
	I store the value of body path (.*) as (.*) in global scope

The simplest way to adopt these expressions is to create a file named apickli-gherkin.js in features/step_definitions and extend the apickli/gherkin.js module.

add the following to test/features/step_definitions/apickli-gherkin.js

module.exports = require('apickli/apickli-gherkin');

If using Windows, follow this guide to create a symlink: How-To Geek Guide.

Setting Proxy Server

apickli uses node.js request module for HTTP communications which supports setting proxy servers via the following environment variables:

  • HTTP_PROXY / http_proxy
  • HTTPS_PROXY / https_proxy
  • NO_PROXY / no_proxy

For more information, see https://github.com/request/request#controlling-proxy-behaviour-using-environment-variables

Variable Injection

It is possible to use Scenario Variables in a Feature file, that will have values injected when the tests are run. Whilst defining values explicitly provides better clarity to those reading a feature file, there are some configuration values such as Client Id which it is easier to externalise.

By default, backticks are use to indicate a variable in a feature file. When instantiating Apickli, a different character can be passed as a parameter. In order to follow BDD best practices, global variables should not be used in the way. Each Scenario should be independent, and as such if you would like to define configurable variables it should be done using the Before hook:

/* jslint node: true */
'use strict';

var apickli = require('apickli');

module.exports = function() {
	// cleanup before every scenario
	this.Before(function(scenario, callback) {
		this.apickli = new apickli.Apickli('http', 'httpbin.org');
        this.apickli.storeValueInScenarioScope("BasicAuthValue", "Basic abc123");
		callback();
	});
};
Feature:
  Httpbin.org exposes various resources for HTTP request testing
  As Httpbin client I want to verify that all API resources are working as they should


  Scenario: Setting authorization headers in GET request                         
    Given I set Authorization header to `BasicAuthValue`                       
    When I GET /get                                                
    Then response body path $.headers.Authorization should be Basic abc123 

For more examples, please see source/test/features/injecting-variables.feature

Contributing

If you have any comments or suggestions, feel free to raise an issue or fork the project and issue a pull request with suggested improvements.