The following is an example of the Cypress api plugin created by Filip Hric. The plugin adds the ability to see the API information in the Cypress UI runner.
This demo will use the API restful-booker - https://restful-booker.herokuapp.com/apidoc/index.html
The repo gives examples of the following features
- The Cypress API plugin - https://www.npmjs.com/package/cypress-plugin-api
- hideCredentials - https://www.npmjs.com/package/cypress-plugin-api#hiding-credentials
- Cypress environmental variable management - https://docs.cypress.io/guides/guides/environment-variables
It's also written in TypeScript but this is not a requierment to use the plugin or any of its features.
You will need to create a cypress.env.json
file that contains the following information...
{
"apiUsername: "",
"apiPassword": ""
}
The credentials for the API can be found here - https://restful-booker.herokuapp.com/apidoc/index.html#api-Auth-CreateToken
After this run npm i
to install all dependencies.
- You can either run the commands
npm start
ornpx cypress open
to open the Cypress runner UI. - Select
E2E Testing
thenStart E2E Testing in Chrome
- From here you'll have two options
- The
nonPlugin-restfulBooker.herokuApp.cy.js
spec which uses the standardcy.request()
method - The
plugin-restfulBooker.herokuApp.cy.js
spec which uses thecy.api()
method andhideCredentials
feature
- The