Skip to main content
Version: 2x

@qavajs/steps-api

Step library to work with web API

Demo projects:

Installation​

npm install @qavajs/steps-api

Configuration​

export default {
require: [
'node_modules/@qavajs/steps-api/index.js'
]
}

Parameter Types​

gqlRequestProperty​

Used to initialize one of two possible GraphQl body properties

  • query
  • variables

bodyParsingType​

Used for parsing responses body

  • arrayBuffer
  • formData
  • blob
  • json
  • text

Action Steps​

I send {method} request to {url} and save response as {key}​

Send request to the endpoint

paramtypedescription
methodstringrequest method
urlstringendpoint url
keystringkey that should be used for saving response
When I send "GET" request to "$BASE_API_URL" and save response as "response"

I send {method} request to {url} with headers {headers} and save response as {key}​

Send request to the endpoint

paramtypedescription
methodstringrequest method
urlstringendpoint url
headersstringmemory key with headers
keystringkey that should be used for saving response
When I send "GET" request to "$BASE_API_URL" with headers "$headers" and save response as "response"

I send {method} request to {url} with qs {params} and save response as {key}​

Send request to the endpoint with query string

paramtypedescription
methodstringrequest method
urlstringendpoint url
paramsstringquery string parameters
keystringkey that should be used for saving response
When I send "GET" request to "$BASE_API_URL" with qs "?category=HR&name=test" and save response as "response"

I send {method} request to {url} with headers {headers} with qs {params} and save response as {key}​

Send request to the endpoint with query string

paramtypedescription
methodstringrequest method
urlstringendpoint url
headersstringmemory key with headers
paramsstringquery string parameters
keystringkey that should be used for saving response
When I send "GET" request to "$BASE_API_URL" with headers "$headers" with qs "?category=HR&name=test" and save response as "response"

I send {method} request to {url} with Body {requestBody} and save response as {key}​

Send request with body

paramtypedescription
methodstringrequest method
urlstringendpoint url
requestBodyJSONrequest body
keystringkey that should be used for saving response
When I send "POST" request to "$BASE_API_URL" with Body "test_data_file.json" and save response as "response"

I send {method} request to {url} with headers {headers} with Body {requestBody} and save response as {key}​

Send request with body

paramtypedescription
methodstringrequest method
urlstringendpoint url
headersstringmemory key with headers
requestBodyJSONrequest body
keystringkey that should be used for saving response
When I send "POST" request to "$BASE_API_URL" with headers "$json('headers.json')" with Body "test_data_file.json" and save response as "response"

I send {method} request to {url} with qs {params} and Body {requestBody} and save response as {key}​

Send request with body and query string

paramtypedescription
methodstringrequest method
urlstringendpoint url
paramsstringquery string parameters
requestBodyJSONrequest body
keystringkey that should be used for saving response
When I send "PUT" request to "https://www.some_service.com/some_endpoint/" with qs "?category=HR&name=test" and Body "test_data_file.json" and save response as "response"

I send {method} request to {url} with headers {headers} with qs {params} and Body {requestBody} and save response as {key}​

Send request with body and query string

paramtypedescription
methodstringrequest method
urlstringendpoint url
headersstringmemory key with headers
paramsstringquery string parameters
requestBodyJSONrequest body
keystringkey that should be used for saving response
When I send "PUT" request to "https://www.some_service.com/some_endpoint/" with headers "$headers" with qs "?category=HR&name=test" and Body "test_data_file.json" and save response as "response"

I send {method} request and save response as {key} to {url} with Body: [requestBody]​

I send {method} request and save response as {key} to {url} with headers {headers} with Body: [requestBody]​

Send request with body that given as part of Cucumber step

paramtypedescription
methodstringrequest method
keystringkey that should be used for saving response
urlstringendpoint url
headersstring (optional)memory key of request headers
requestBodystring (Multiline)request body
When I send "POST" request and save response as "response" to "$BASE_API_URL" with Body:
"""
{
"title": "Test Post Request with Body passed as string"
}
"""
When I send "POST" request and save response as "response" to "$BASE_API_URL" with headers "$headers" with Body:
"""
{
"title": "Test Post Request with Body passed as string"
}
"""

I parse {response} body as {type}​

Parse body in needed way and adds payload property to response

paramtypedescription
responsestringresponse key in memory
typebodyParsingTypetype of payload
When I parse '$response' body as json
Then I expect '$response.payload.foo' to equal 'bar'

I parse {response} body as {parser}​

Parse body with provided parser and adds payload property to response

paramtypedescription
responsestringresponse key in memory
typestringfunction with parser logic
When I parse "$response" body as "$soap"
Then I expect "$response.payload['soap:envelope']" to equal "bar"

where soap is memory function

import { XMLParser } from 'fast-xml-parser';
const xml = new XMLParser();
class Data {
soap = async (response: Response) => {
const text = await response.text();
return xml.parse(text);
}
}

I clone/copy {response} response as {key}​

Clone response (e.g. to read body multiple times)

paramtypedescription
responsestringresponse key in memory
keystringmemory key to save clone
When I clone '$response' response as 'copiedResponse'
When I copy '$response' response as 'copiedResponse'

Construction API Steps​


I create {method} request {requestKey}​

Create request and save it to memory

paramtypedescription
methodstringAPI method
requestKeystringkey to save request
When I create 'GET' request 'request'

I add headers to {requestKey}: [headersDataTable]​

Add data table of headers to request

paramtypedescription
requestKeystringmemory key of request
headersDataTableDataTablekey value array of headers
When I create 'GET' request 'request'
And I add headers to '$request':
| Content-Type | application/json |

I add {requestKey} headers to {headersKey}​

Add headers to request

paramtypedescription
requestKeystringmemory key of request
headersKeystringmemory key of headers that resolves to JS object
When I create 'GET' request 'request'
And I add '$headers' headers to '$request'

I add body to {requestKey}: [body]​

Add body to request as multiline parameter

paramtypedescription
requestKeystringmemory key of request
bodystring (Multiline)body
When I create 'GET' request 'request'
And I add body to '$request':
"""
{
"message": "qavajs"
}
"""

I add {requestKey} body to {body}​

Add body to request

paramtypedescription
requestKeystringmemory key of request
bodystringbody
When I create 'GET' request 'request'
And I add '$body' body to '$request'

I add {requestKey} url to {url}​

Add url to request

paramtypedescription
requestKeystringmemory key of request
urlstringurl
When I create 'GET' request 'request'
And I add 'https://qavajs.github.io/' url to '$request'

I send {requestKey} request and save response as {responseKey}​

Send prepared request and save response

paramtypedescription
requestKeystringmemory key of request
responseKeystringmemory key to save response
When I create 'GET' request 'request'
And I add 'https://qavajs.github.io/' url to '$request'
And I send '$request' request and save response as 'response'

I add form data body to {requestKey}: [dataTable]​

Send prepared request and save response

paramtypedescription
requestKeystringmemory key of request
dataTableDataTablebody
When I add body to '$request':
| key | value | filename | contentType |
| formKey | formValue | | application/json |
| otherKey | otherValue | | text/plain |
| fileKey | $file('./path/file.png') | file.png | image/png |

I create GraphQL request {requestKey}​

Create GraphQL request and save it to memory

paramtypedescription
requestKeystringkey to save request
When I create GraphQL request 'request'

I add {property} to GraphQL {string}: [value]​

Add GraphQL property

paramtypedescription
propertygqlRequestPropertyquery or variables
requestKeystringmemory key of request
valuestring (Multiline)value multiline string
When I create GraphQL request 'request'
And I add query to GraphQL '$request':
"""
query {
characters(page: 2, filter: { name: "rick" }) {
results {
name
}
}
}
"""
And I add variables to GraphQL '$request':
"""
{
"value": 42
}
"""

Verification Steps​

note

Response body has to be parsed via I parse ... body as ... step before verification


Response {path} {validation} {expected}​

Verify that value of response property satisfies validation

paramtypedescription
pathstringpath to property of parsed response
validationstringvalidation
expectedstringexpected value
Then Response "$response.payload.data.items[0].title" to equal "TEST"

Response {path} size {validation} {expected}​

Verify that array size satisfies validation

paramtypedescription
pathstringpath to array in response
validationstringvalidation
expectedstringexpected array size
Then Response "$response.payload.data.items" size to be above "0"

Response type {path} {validation} {type}​

Verify that typeof of response property satisfies validation

paramtypedescription
pathstringpath to property of parsed response
validationstringvalidation
typestringexpected type (e.g. string, number, object...)
Then Response type "$response.payload.data.title" to equal "string"

Response {path} contains: [DataTable]​

Verify that each object in response contains all properties provided in data table

paramtypedescription
pathstringpath to object or array of objects
propsDataTablelist of property names
Then Response "$response.payload.data.items" contains:
| _id |
| appId |
| serviceCategory |

Websocket Steps​


I connect to {string} ws endpoint {string}​

Connect to websocket endpoint and save connection to memory

paramtypedescription
urlstringwebsocket endpoint
requestKeystringkey to save request
When I connect to 'ws://localhost:3000' ws endpoint 'ws'

I save message from {string} ws endpoint as {string}​

Save message from websocket connection to memory

paramtypedescription
connectionKeystringmemory key of saved connection
memoryKeystringmemory key to save message
When I connect to 'ws://localhost:3000' ws endpoint 'ws'
And I save message from '$ws' ws endpoint as 'message'

I save message matching {string} from {string} ws endpoint as {string}​

Save message matching regexp from websocket connection to memory

paramtypedescription
regexpstringregexp to filter messages
connectionKeystringmemory key of saved connection
memoryKeystringmemory key to save message
When I connect to 'ws://localhost:3000' ws endpoint 'ws'
And I save message matching 'qavajs test' from '$ws' ws endpoint as 'message'

I send {string} message to {string} ws endpoint​

Send message to websocket

paramtypedescription
messagestringmessage to send
connectionKeystringmemory key of saved connection
When I connect to 'ws://localhost:3000' ws endpoint 'ws'
And I send 'qavajs' message to '$ws' ws endpoint

I send message to {string} ws endpoint: [message]​

Send message to websocket

paramtypedescription
connectionKeystringmemory key of saved connection
messagestring (Multiline)message to send
When I connect to 'ws://localhost:3000' ws endpoint 'ws'
And I send message to '$ws' ws endpoint:
"""
qavajs
"""

I close {string} ws connection​

Close websocket connection

paramtypedescription
connectionKeystringmemory key of saved connection
When I connect to 'ws://localhost:3000' ws endpoint 'ws'
And I close '$ws' ws connection