js-automation-tools

A collection of scripts for javascript test automation

Usage no npm install needed!

<script type="module">
  import jsAutomationTools from 'https://cdn.skypack.dev/js-automation-tools';
</script>

README

js-automation-tools

A collection of scripts for JavaScript test automation

Actions Status npm version NPM License

Supported versions

Node.js: 8.x, 9.x, 10.x, 11.x, 12.x, 13.x, 14.x

Table of contents

Installation

To install js-automation-tools and save it to your package.json just run:

npm install js-automation-tools --save-dev

Generate timestamp or random digits

There ususally is a need to generate random names. Timestamp can be used to generate a unique string of 13+ digits:

const { stamp } = require('js-automation-tools');

const randomDigits = stamp.getTimestamp(); // '1588556993141'
const newTestName = `My new test ${randomDigits}`; // 'My new test 1588556993141'

It will also write generated digits to a global environment variable process.env.TIMESTAMP that can be easily accessed in any place of your tests:

console.log(process.env.TIMESTAMP); // '1588556993141'

To get new timestamp:

const newRandomDigits = stamp.resetTimestamp(); // '1588558255810'

console.log(process.env.TIMESTAMP); // '1588558255810'

Send GET, POST and other requests

Send request to any URL and get response - sendRequest function accepts 5 arguments:

  1. Method - string (for example: 'GET' or 'POST' or 'DELETE' or any other)
  2. Request URL - string (for example: 'https://www.google.com/')
  3. Headers - string (for example: '{ "Content-Type": "application/json", "Authorization": "Bearer aBcD1234" }')
  4. Body - string (for example: '{ "test1": 1, "test2": 2 }')
  5. Log level - number (for example: 0 or 1 or 2)

Or just call sendRequest function with empty string ('') instead of any argument if it's not needed in your request:

const { sendRequest } = require('js-automation-tools');

const responseGet = await sendRequest(
    'GET',
    'https://www.google.com/',
    '',
    '',
    2
);

const responsePost = await sendRequest(
    'POST',
    'http://httpbin.org/post',
    '{ "Content-Type": "application/json", "Authorization": "Bearer aBcD1234" }',
    '{ "test1": 1, "test2": 2 }',
    1
);

OR you can specify the arguments inside the object as key: value pairs:

const { sendRequest } = require('js-automation-tools');

const responseGet = await sendRequest({
    method: 'GET',
    requestUrl: 'https://www.google.com/'
});

const responsePost = await sendRequest({
    method: 'POST',
    requestUrl: 'http://httpbin.org/post',
    headersString: '{ "Content-Type": "application/json", "Authorization": "Bearer aBcD1234" }',
    bodyString: '{ "test1": 1, "test2": 2 }',
    logLevel: 1
});

Note: you can also use createRequest function - it is an alias and works exactly the same as sendRequest, for example:

const { createRequest } = require('js-automation-tools');

const responseGet = await createRequest({
    method: 'GET',
    requestUrl: 'https://www.google.com/'
});

const responsePost = await createRequest({
    method: 'POST',
    requestUrl: 'http://httpbin.org/post',
    headersString: '{ "Content-Type": "application/json", "Authorization": "Bearer aBcD1234" }',
    bodyString: '{ "test1": 1, "test2": 2 }',
    logLevel: 1
});

By default logs are disabled (logLevel set to 0). You can set logging output to one of 3 levels:

  • 0 - logs disabled (by default)
  • 1 - partial logs are enabled - prints out:
    • Response status code
    • Response body
  • 2 - full logs are enabled - prints out:
    • Response status code
    • Response headers
    • Response body

Read directories

Read the array of directories and get the array of files from this directories:

const { readDirectories } = require('js-automation-tools');

const pathToDirectory1 = path.join(__dirname, 'directory1');
const pathToDirectory2 = path.join(__dirname, '..', '..', 'directory2');

const allFiles = await readDirectories([pathToDirectory1, pathToDirectory2]);

Contributing

You are welcome to contribute to this repository - please see CONTRIBUTING.md to help you get started. It is not mandatory, so you can just create a pull request and we will help you refine it along the way.

Thanks

If this package was helpful to you, please give it a ★ Star on GitHub.