Cypress: Difference between revisions
No edit summary |
|||
Line 72: | Line 72: | ||
==Test== | ==Test== | ||
We use MSAL oauth to for authentication. The '''getOptions''' gets the token and puts the bearer token in the request. Then the validateSwaggerSchema validates the schema against the result | |||
<syntaxhighlight lang="ts"> | |||
import { getOptions, swaggerFile } from './helper' | |||
describe('User Test', () => { | |||
before(() => { | |||
cy.getOauthAccessToken().then(oAuthResp => { | |||
Cypress.env('token', oAuthResp.body.access_token) | |||
// cy.task('log', `Token is ${oAuthResp.body.access_token}`) | |||
}) | |||
}) | |||
it('User Get Payload', () => { | |||
cy.request(getOptions('users')).then($response => { | |||
// Check the swagger schema: | |||
cy.task('validateSwaggerSchema', { | |||
file: swaggerFile, // optional path or full URL, see below | |||
endpoint: `/users`, | |||
method: 'get', | |||
statusCode: 200, | |||
responseSchema: $response.body, | |||
verbose: true, // optional, default: false | |||
}).should('equal', null) | |||
}) | |||
}) | |||
}) | |||
</syntaxhighlight> | |||
==Configuration== | ==Configuration== |
Revision as of 04:44, 5 June 2023
Introduction
This is notes of my usage of cypress
Configuration
When I installed Cypress it was v12 of the product. This has changed slightly from most of the tutorials as the name of the tests no longer have spec and but cy and reside in e2e rather than the integration folder. The other change was that Cypress has all tests isolated by default. I.E. it resets the browser in between tests
import { defineConfig } from 'cypress'
export default defineConfig({
testIsolation: false,
})
~
~
Cypress REST API
Perhaps an unusual usage but I use the to validate my OpenAPI 3.0 swagger files used in Node.
Swagger File
Here is the user swagger file used with swagger and swagger-ui. Cypress will be used to validate the schema is correct.
import { standardResponses } from './standardResponses'
export const userPaths = {
'/users': {
get: {
summary: 'Get all Users',
operationId: 'listUsers',
description: 'Get all Users',
produces: ['application/json'],
parameters: [],
responses: {
'200': {
description: 'Array of User',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Users',
},
},
},
},
...standardResponses,
},
},
},
}
export const userSchemas = {
User: {
required: ['USER_SECURITY_ID', 'USER_NAME', 'USER_FULL_NAME', 'USER_EMAIL'],
properties: {
USER_SECURITY_ID: {
type: 'string',
},
USER_NAME: {
type: 'string',
},
USER_FULL_NAME: {
type: 'string',
},
USER_EMAIL: {
type: 'string',
},
},
},
Users: {
type: 'array',
items: {
$ref: '#/components/schemas/User',
},
},
}
Test
We use MSAL oauth to for authentication. The getOptions gets the token and puts the bearer token in the request. Then the validateSwaggerSchema validates the schema against the result
import { getOptions, swaggerFile } from './helper'
describe('User Test', () => {
before(() => {
cy.getOauthAccessToken().then(oAuthResp => {
Cypress.env('token', oAuthResp.body.access_token)
// cy.task('log', `Token is ${oAuthResp.body.access_token}`)
})
})
it('User Get Payload', () => {
cy.request(getOptions('users')).then($response => {
// Check the swagger schema:
cy.task('validateSwaggerSchema', {
file: swaggerFile, // optional path or full URL, see below
endpoint: `/users`,
method: 'get',
statusCode: 200,
responseSchema: $response.body,
verbose: true, // optional, default: false
}).should('equal', null)
})
})
})