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

js-automation-tools

v3.1.3

Published

A collection of scripts for JavaScript test automation

Downloads

12,065

Readme

js-automation-tools

A collection of scripts for JavaScript test automation

Actions Status npm version NPM License

Supported versions

Node.js: 8.x-22.x.

Note: if you are choosing a version of Node.js to run with - check release schedule.

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'

Generate current date and time

Sometimes you need to generate current date and time. It can easily be done with generateDateTime function:

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

const currentDateTime = dateTime.generateDateTime(); // '2024-09-23T19:26:35'

Also you can use generateDateTimePlus function that generates current (if no initial date is provided) date and time plus number of days, hours, minutes, seconds if they are provided. This function accepts configuration object with 5 arguments:

  1. days - number of days that will be added to current (if no initial date is provided) date and time (optional, default value: 0).
  2. hours - number of hours that will be added to current (if no initial date is provided) date and time (optional, default value: 0).
  3. minutes - number of minutes that will be added to current (if no initial date is provided) date and time (optional, default value: 0).
  4. seconds - number of seconds that will be added to current (if no initial date is provided) date and time (optional, default value: 0).
  5. initialDate - string with initial date and time that you want to add a number of days, hours, minutes, seconds to (for example: '2024-03-14T00:14:25', optional, default value: current date and time).
const { dateTime } = require('js-automation-tools');

const currentDateTimePlusDay = dateTime.generateDateTimePlus({ days: 1 }); // '2024-09-24T19:26:35'
const currentDateTimePlusHour = dateTime.generateDateTimePlus({ hours: 1 }); // '2024-09-23T20:26:35'
const currentDateTimePlusMinute = dateTime.generateDateTimePlus({ minutes: 1 }); // '2024-09-23T19:27:35'
const currentDateTimePlusSecond = dateTime.generateDateTimePlus({ seconds: 1 }); // '2024-09-23T19:26:36'

const currentDateTimeMinusHour = dateTime.generateDateTimePlus({ hours: -1 }); // '2024-09-23T18:26:35'

const pastDateTimePlusDay = dateTime.generateDateTimePlus({ days: 1, initialDate: '2024-03-14T00:14:25' }); // '2024-03-15T00:14:25'
const pastDateTimePlusHour = dateTime.generateDateTimePlus({ hours: 1, initialDate: '2024-03-14T00:14:25' }); // '2024-03-14T01:14:25'
const pastDateTimePlusMinute = dateTime.generateDateTimePlus({ minutes: 1, initialDate: '2024-03-14T00:14:25' }); // '2024-03-14T00:15:25'
const pastDateTimePlusSecond = dateTime.generateDateTimePlus({ seconds: 1, initialDate: '2024-03-14T00:14:25' }); // '2024-03-14T00:14:26'

const pastDateTimePlusDayHourMinuteSecond = dateTime.generateDateTimePlus({
    days: 1,
    hours: 1,
    minutes: 1,
    seconds: 1,
    initialDate: '2024-03-14T00:14:25'
}); // '2024-03-15T01:15:26'

It will also write generated date and time to a global environment variable process.env.DATETIME and process.env.NEW_DATETIME that can be easily accessed in any place of your tests.

Send GET, POST or any other requests

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

  1. method - string (for example: 'GET' or 'POST' or 'DELETE' or any other).
  2. requestUrl - string with URL of endpoint to send request to (for example: 'https://www.google.com/').
  3. headersString - string that contains request headers (for example: '{ "Content-Type": "application/json", "Authorization": "Bearer aBcD1234" }', optional).
  4. bodyString - string that contains request body (for example: '{ "test1": 1, "test2": 2 }', optional).
  5. logLevel - number (for example: 0 or 1 or 2, optional, default value: 0 - no logs).

Also you can just call sendRequest function without headersString, bodyString, logLevel arguments if they are not needed - for example in GET request:

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
});

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

Retry executing function

Retry executing a provided function once per a provided amount of milliseconds until this function will return a value that upon passing a functionCheck check will be true or the amount of provided attempts will be exceeded - retryIfFalse function accepts configuration object with 5 arguments:

  1. functionToExecute - function to execute. For example an API request:
    async function () {
        const response = await sendRequest({ method: 'GET', requestUrl: 'https://www.google.com/' });
        return response.body;
    }
  2. functionCheck - function to execute to check the result of functionToExecute (if successful - should return true, for example: (responseBody) => responseBody.length > 0, optional).
  3. attempts - number of attempts to retry (optional, default value: 10).
  4. waitTime - time to wait between retries in milliseconds (optional, default value: 1000).
  5. logLevel - number (for example: 0 or 1 or 2, optional, default value: 0 - no logs).

Note: you have to specify the retryIfFalse arguments inside the configuration object as key: value pairs:

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

const myFunction = async function () {
    const response = await sendRequest({ method: 'GET', requestUrl: 'https://www.google.com/' });
    return response;
};

const result = await retryIfFalse({
    functionToExecute: myFunction,
    attempts: 10,
    waitTime: 3000,
    logLevel: 2
}); // myFunction will be executed every 3 seconds up to 10 times until its result will be truthy
console.log('result:', result); // result: { statusCode: 200, headers: { 'content-type': 'application/json' }, body: 'Some data' }

OR if you want to provide a custom functionCheck function to check that the result of functionToExecute (or any of its properties) is truthy:

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

const myFunction = async function () {
    const response = await sendRequest({ method: 'GET', requestUrl: 'https://www.google.com/' });
    return response;
};
const checkFunction = function (result) {
    return result.statusCode === 200;
};

const result = await retryIfFalse({
    functionToExecute: myFunction,
    functionCheck: checkFunction,
    attempts: 20,
    waitTime: 5000,
    logLevel: 2
}); // myFunction will be executed every 5 seconds up to 20 times until its result statusCode will be 200
console.log('result:', result); // result: { statusCode: 200, headers: { 'content-type': 'application/json' }, body: 'Some data' }

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:
    • Each attempt number
  • 2 - full logs are enabled - prints out:
    • Each attempt number
    • Response from functionToExecute after each attempt

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.