Validation step definitions for MAF
npm install @ln-maf/validationsThis module allows other projects to easily validate JSON using a set of Cucumber step definitions.
[![npm package][npm-image]][npm-url]

[![Dependencies][dep-image]][dep-url]
- Setup
- Step Definitions
- Data Setting Steps
- Assertion Steps
- JSON Manipulation Steps
- File Operation Steps
- Encoding and Crypto Steps
- XML Processing Steps
- Blob Operation Steps
- Set Comparison Steps
- Utility Steps
[![npm package][npm-image]][npm-url]

[![Dependencies][dep-image]][dep-url]
Install:
``sh`
npm i @ln-maf/validations
Add a step file with:
`js`
require('cucumber-validations');
Add the following to .vscode/settings.json for step autocompletion:
`json`
{
"cucumberautocomplete.stepsInvariants": true,
"cucumberautocomplete.smartSnippets": true,
"cucumberautocomplete.customParameters": [
{
"parameter": "{jsonObject}",
"value": "(it|item {string}|file {string}|{string})"
}
],
"cucumberautocomplete.steps": [
"node_modules/cucumber-validations/features/stepDefinitions/steps.js"
]
}
This library implements step definitions and adheres to the global cucumber implementation for various internals.
{jsonObject}Sets an item to a JSON object. The object is stored in this.results.${itemName} and accessible in other global Cucumber steps as ${itemName}.
Example:
config.json contains:
`json`
{
"url" : "http://google.com",
"meh": "Test"
}
After running one of the following:
`feature`
When set "hello" to file "config.json"
When set "hello" to '{"url":"http://google.com", "meh":"Test"}'
When set "lastRun" to:
"""
{
"url": "http://google.com",
"meh": "Test"
}
"""
And set "hello" to it
When set "jsonItem" to:
"""
{
"url": "http://google.com",
"meh": "Test"
}
"""
And set "hello" to item "jsonItem"
Then ${hello} will equal:
`json`
{
"url": "http://google.com",
"meh": "Test"
}
Sets an item to the given integer. The value is stored in this.results.${itemName} and accessible as ${itemName}.
Example:
`feature`
When set "hi" to 3
Then item "hi" is equal to 3
Sets an item to a doc string value.
Example:
`feature`
When set "a" to "3"
And set "item" to:
"""
{
"a": ${a}
}
"""
Then item "item" is equal to:
"""
{
"a": 3
}
"""
Sets config from a JSON object. If the object contains keys, all keys are stored as individual variables.
Example:
`feature`
When set config from json file "config.json"
Then "${meh}" is equal to "Test"
Where config.json is:
`json`
{
"url": "http://google.com",
"meh": "Test"
}
Takes in a datatable object and sets all values. If the datatable has multiple values, it is treated as an array; otherwise, as a single object.
Example:
`feature`
When set:
|username|pass|
|User|Pass|
|User2|2Pass|
Then "${username[0]}" is equal to "User"
Then "${username[1]}" is equal to "User2"
Or:
`feature`
|username|pass|
|User|Pass|
Then "${username}" is equal to "User"
Sets parameters from a docString for testing command line arguments.
Example:
`feature`
When parameters are:
"""
{
"testParam": "testValue"
}
"""
Compares two numeric values using comparison operators like is greater than, is less than, etc.
Example:
`feature`
When set "a" to 5
And set "b" to 3
Then item "a" is greater than item "b"
Checks if the string is before or after the current time. The when parameter supports before or after. The string value accepts UTC timestamp or a date string and uses Date.parse.
Checks if one date string is before or after another.
Example:
`feature`
Then "11/11/2019" is before "11/12/2019"
Equivalent to Then "${lastRun}" is equal to {string}.
Checks if an item is equal to a docString.
Example:
`feature`
When set "a" to "3"
And set "item" to:
"""
{
"a": ${a}
}
"""
Then item "item" is equal to:
"""
{
"a": 3
}
"""
Confirms that two strings are equal.
Example:
`feature`
Given set "item" to "hi"
Then "${item}" is equal to "hi"
Then "hi" is equal to "hi"
You can also use "it" to reference the item: ${lastRun}.
Checks if a JSON object is not equal to a docString.
Example:
`feature`
When set "item" to "different value"
Then item "item" is not equal to:
"""
expected value
"""
Checks if a JSON object is equal to a docString.
Example:
`feature`
When set "item" to "expected value"
Then item "item" is equal to:
"""
expected value
"""
Confirms that a JSON item is equal to the specified integer.
Example:
`feature`
When set "a" to "3"
Then item "a" is equal to 3
When set "a" to 3
Then item "a" is equal to 3
Checks that lastRun is not equal to the supplied item.
Checks that the JSON object is null.
Example:
`feature`
Then item "emptyItem" is null
Checks that the JSON object is not null.
Example:
`feature`
Then item "dataItem" is not null
Checks that the supplied item is not null.
Checks that two items are not equal.
Runs a JSONPath on a defined variable and stores the result in this.results.lastRun.
Removes the JSON key/value from the provided JSON object.
Example:
`feature`
When set "Data" to
"""
{
"a":"apple",
"b":"banana"
}
"""
When JSON key "a" is removed from "Data"
Then "${Data.b}" is equal to "banana"
And element "a" does not exist in item "Data"
Removes the JSON key from the JSON object in lastRun. Similar to above, but uses lastRun.
Extracts the JSON key from a variable and stores it in lastRun.
Example:
`feature`
When set "Data" to
"""
{
"a":"apple",
"b":"banana"
}
"""
When JSON key "a" is extracted from "Data"
Then it is equal to "apple"
Extracts the JSON key from the JSON object in lastRun and stores it in lastRun.
Extracts multiple JSON keys from a variable and stores them in lastRun. Keys are supplied as a JSON array of strings. Scoping into arrays is not supported (e.g., ["arrayTest[0]"] will not work).
Example:
`feature`
When set "TestJSON" to
"""
{
"url": "http://google.com",
"meh": "Test",
"meh2": "Another Test",
"deepMeh": {
"deep1": "Testing1",
"deep2": "Testing2"
},
"deepMeh2": {
"deep3": "Testing3",
"deep4": "Testing4"
},
"arrayTest": [
"Testing1",
"Testing2",
"Testing3"
]
}
"""
And JSON keys '["meh","deepMeh","deepMeh2.deep3", "arrayTest"]' are extracted from "TestJSON"
And set "expected" to:
"""
{
"meh": "Test",
"deepMeh": {
"deep1": "Testing1",
"deep2": "Testing2"
},
"deepMeh2": {
"deep3": "Testing3"
},
"arrayTest": [
"Testing1",
"Testing2",
"Testing3"
]
}
"""
Then item "expected" is equal to item "lastRun"
Extracts multiple JSON keys from the JSON object in lastRun and stores them in lastRun.
Validates that the element exists in the item.
Example:
`feature`
When set "TestJSON" to
"""
{
"a":"apple",
"b":"banana"
}
"""
Then element "a" exists in item "TestJSON"
Validates that the element does not exist in the item (opposite of above).
Validates that all elements exist in the item. Elements should be comma-separated; brackets ([]) are optional.
Example:
`feature`
When set "TestJSON" to
"""
{
"a":"apple",
"b":"banana",
"c":"cherry",
}
"""
Then elements "[a,b,c]" exist in item "TestJSON"
Passes if all elements do not exist in the item (opposite of above).
Reads binary data (blob) from a file.
Example:
`feature`
When blob is read from file "image.png"
And set "imageData" to it
Writes blob data to a file.
Example:
`feature`
When blob item "imageData" is written to file "output.png"
Attaches blob data to the test report (for visual validation).
Example:
`feature`
When blob item "screenshot" is attached
Compares blob data with the contents of a file.
Example:
`feature`
Then blob item "actualImage" is equal to file "expected.png"
Equivalent to Then the set "lastRun" matches the set {string}.
Validates that two arrays have the same values (any order, duplicates removed).
Equivalent to Then the set "lastRun" matches the set from file {string}.
Validates that two arrays have the same values (any order, duplicates removed). One array is loaded from a file.
Sets the xPath namespace to the provided string.
Example:
`feature`
Given xPath namespace is '{ "soap": "http://schemas.xmlsoap.org/soap/envelope/", "ln":"http://ln-maf.com" }'
Same as above, but uses a doc string.
Example:
`feature`
Given xPath namespace is
"""
{ "soap": "http://schemas.xmlsoap.org/soap/envelope/", "ln":"http://ln-maf.com" }
"""
Adds a single xPath namespace mapping.
Example:
`feature`
When add xPath namespace "custom" = "http://example.com/namespace"
Runs an xPath on the defined item using the specified xPath namespaces. Stores the result in this.results.lastRun.
Replaces the values of all found JSON keys in an item, using the JSON path to identify the keys.
Example:
`feature`
Given set "meh" to:
"""
{
"url": "http://google.com",
"arrayTest": [
"Testing1",
"Testing2",
"Testing3"
]
}
"""
And set "expected" to:
"""
{
"url": null,
"arrayTest": [
"Testing1",
"Testing2",
"Testing3"
]
}
"""
When "null" is applied to item "meh" on JSON path "$.url"
Then item "expected" is equal to item "meh"
Replaces the values of all found JSON keys in a file, using the JSON path to identify the keys.
Example:
file.json contains:
`json`
{
"url": "http://google.com",
"meh": "Test"
}
After running: When "SomethingElse" is written to file "file.json" on JSON path "$.meh"
file.json will now contain
`json`
{
"url": "http://google.com",
"meh": "SomethingElse"
}
Writes an item to a file.
Writes a JSON object to a file. If the object is a JavaScript object, it will be stringified.
Example:
`feature`
When set "data" to:
"""
{
"name": "test",
"value": 123
}
"""
When item "data" is written to file "output.json"
Writes a JSON array to a file in JSON Line Delimited format (each array item on a separate line).
Example:
`feature`
When set "data" to:
"""
[
{"name": "item1"},
{"name": "item2"}
]
"""
When item "data" is written in json line delimited format to file "output.jsonl"
Compresses a file using gzip compression.
Example:
`feature`
When the file "large-data.txt" is gzipped
Decompresses a gzipped file to a new file.
Example:
`feature`
When file "compressed.gz" is gzip unzipped to file "uncompressed.txt"
Equivalent to When item "lastRun" is written to file {string}.
Sets config from a JSON file. If the file contains a JSON object, all keys are stored.
Example:
`feature`
When set config from json file "config.json"
Then "${meh}" is equal to "Test"
Where config.json is:
`json`
{
"url": "http://google.com",
"meh": "Test"
}
Encodes a JSON object or string to base64 format.
Example:
`feature`
When set "data" to "Hello World"
When item "data" is base64 encoded
And set "encodedData" to it
Decodes a base64 encoded string back to its original format.
Example:
`feature`
When item "encodedData" is base64 decoded
And set "decodedData" to it
Decodes a base64 value and saves it back to the same variable.
Example:
`feature`
When set "encoded" to "SGVsbG8gV29ybGQ="
When the value "encoded" is base64 decoded and resaved
Generates an RSA private key for JWT signing.
Example:
`feature`
When generate rsa key
And set "privateKey" to it
Requires a header and private key to be set (unless using an algorithm that doesn't require a private key).
Example:
`feature`
When generate rsa key
And set "privateKey" to it
And set "header" to
"""
{
"alg": "RS256",
"ver": "GTP-1.0",
"keyId": 1
}
"""
When sign using jwt:
"""
{ "soap": "http://schemas.xmlsoap.org/soap/envelope/", "repo": "MAF" }
"""
Encrypts a provided item using JWT. Same as above, but operates on the item.
Makes all JSON keys in the item lowercase.
Example:
`feature`
When set "data" to:
"""
{
"Alpha": "apple",
"BETA": "banana",
"Charley": "coconut",
}
"""
When make json keys for item "data" lower case
Then item "data" is equal to:
"""
{
"alpha": "apple",
"beta": "banana",
"charley": "coconut",
}
"""
Flattens the JSON object, removing root keys without losing any values.
Example:
`feature`
When set "data" to:
"""
{
"Alpha": {
"Alpha_2": "Apple"
},
"Beta": "Banana",
"Charley": {
"Charley_2": {
"Charley_3": "Coconut"
}
},
"Delta": {
"Delta_21" : "Durian1",
"Delta_22" : "Durian2"
}
}
"""
When json item "data" is flattened
Then item "data" is equal to:
"""
{
"Alpha_2": "Apple",
"Beta": "Banana",
"Charley_3": "Coconut",
"Delta_21": "Durian1",
"Delta_22": "Durian2"
}
"""
Converts any numbers in string format into number format.
Example:
`feature`
When set "data" to:
"""
{
"Alpha": "123",
"Beta": {
"Beta_21": "456",
"Beta_22": "some_word"
},
"Charley": {
"Charley_21": "45.6",
"Charley_22": 24.9
},
"Delta": "1.2.3"
}
"""
When json item "data" is numberifyed
Then item "data" is equal to:
"""
{
"Alpha": 123,
"Beta": {
"Beta_21": 456,
"Beta_22": "some_word"
},
"Charley": {
"Charley_21": 45.6,
"Charley_22": 24.9
},
"Delta": "1.2.3"
}
"""
Trims all string values in the JSON (removes whitespace and newlines).
Example:
`feature`
When set "data" to:
"""
{
"Alpha": {
"Alpha_2": "Apple "
},
"Beta": "_Banana_",
"Charley": "Spaces are kept in between words",
"Delta": {
"Delta_21" : " But spaces at the beginning and end are removed ",
"Delta_22" : "\nSo are new lines\n"
}
}
"""
When json item "data" is trimmed
Then item "data" is equal to:
"""
{
"Alpha": {
"Alpha_2": "Apple"
},
"Beta": "_Banana_",
"Charley": "Spaces are kept in between words",
"Delta": {
"Delta_21": "But spaces at the beginning and end are removed",
"Delta_22": "So are new lines"
}
}
"""
Waits the provided number of milliseconds.
Example:
`feature`
When wait 5000 milliseconds
Allows usage of the cucumber-js CLI option --world-parameters. See docs.
Sets the examples from a scenario outline. Items using other examples cannot be set. Reads the feature file each time (may be slow for large files).
Example:
`feature
Scenario Outline: Testing
When set examples
Then "
@phone
Examples:
| Phone | Expected | ExpectedResult |
| 1 | ${Phone} | 1 |
@next
Examples:
| Next | Expected | ExpectedResult |
| 2 | ${Next} | 2 |
`
Result:
`feature
@phone
Scenario: Testing
When set examples
"""
{
"Phone": "1",
"ExpectedResult": "1"
}
"""
Then "${Phone}" is equal to "1"
@next
Scenario: Testing
When set examples
"""
{
"Next": "2",
"ExpectedResult": "2"
}
"""
Then "${Next}" is equal to "2"
`
Checks if the jsonObject contains the string anywhere. The object is converted to a string and checked for inclusion.
Example:
`feature`
Given set "test1" to "the quick brown fox jumped over the lazy dog"
Then item "test1" contains "quick brown"
Given set "test2" to:
"""
{
"firstname": "Robert",
"lastname": "Paulson"
}
"""
Then item "test2" contains "Robert"
And item "test2" contains "lastname"
Given set "test3" to:
"""
[
"Apple",
"Banana",
"Orange"
]
"""
Then item "test3" contains "Oran"
Checks if the jsonObject` does not contain the string (opposite of above).
[npm-image]: https://img.shields.io/npm/v/@ln-maf/validations.svg
[npm-url]: https://www.npmjs.com/package/@ln-maf/validations