An automation testing framework based on Cypress for REST API and WebUI testing using Page Object Model, Mocha, Lighthouse and CI intergrated with Github Actions, CircleCI, GitlabCI and Bitbucket Pipelines. Running with spec file. No BDD.
.....
⭐ 28/08/2021 : Update the comparing key value of API test (which not correct if return a array of object)
⭐ 30/08/2021 : Add schema validator with ajv
⭐ 31/08/2021 : CI test with Github Actions, CircleCI, Travis CI, Bitbucket Pipeleines, Gitlab Pipelines
⚙️ Testing across REST API and Web UI applications
⚙️ Multiple browser automation
⚙️ Multiple reports (JSON, html) with failed screenshots attached
⚙️ CI integrated (CircleCI, Github Actions, GitlabCI, Bitbucket Pipelines)
⚙️ Performance testing with Lighthouse and Pa11y
⚙️ Visual regression testing supported
⚙️ Page Object Model
⚙️ Schema Validator with ajv
⚙️ Parallel runs with CI provider
⚙️ Parallel runs with Cypress Dashboard
⚙️ And other functions inherited from Cypress
Use npm to install the dependencies
npm install
npx cypress open
npx cypress run
npx cypress run --browser firefox
npx cypress run --headless
npx cypress run --spec cypress/integration/{spec.js file}
npx cypress run --spec "cypress/integration/atwt-api/*.spec.js" --browser chrome
More details: https://docs.cypress.io/guides/guides/command-line
- Head to cypress/support/pages and create new .page.js file
- List all locators of elements in the page
- Write methods that supports your test validations/actions
- Export the page object
- Head to cypress/integration/atwt-ui and create new .spec.js file
- Import the created page object that including the functions/validators you want to use
- "describe" tag for test suite
- "context" tag for group of test cases by condition
- "it" tag for test case
- Initialize page object and call methods/validators for testing purpose
- Head to cypress/integration/atwt-api and create new .spec.js file
- Import the api-action
- "describe" tag for test suite
- "context" tag for group of test cases by condition
- "it" tag for test case
- Some bilt-in functions: makeRequest(), validateResponseKeyValue(), getResponseBodyValue(), setHeader(), ...
- Create new schema file inside support/schema as following
- Import the schema configuration in your test spec.js file
- Call cy.validateSchema(, apiAction.getResponseBody()) to validate schema of response
- Store the run-time parameter by adding key and value: cy.addRuntimeVariable(, )
- Get the stored parameter by cy.getRuntimeVariable()
- Remmeber to .then() since Cypress command is asynchonous
- Install cypress-multi-reporters
npm install --save-dev cypress-multi-reporters
- Install cypress-mochawesome-reporter and mocha-junit-reporter
npm install --save-dev cypress-mochawesome-reporter,mocha-junit-reporter
- Config reporter in cypress.json
- Define the reports we want to use by "reporterEnabled" tag
- For Mocha Junit Report:
- "mochaFile" to define the output report structure
- [hash] to generate hash string which helps distinquish report files
- "jenkinsMode" to generate more beautiful report displayed on Jenkins
- For Mochawesome Report:
- "chart" to include chart counts
- "reportPageTitle" to specify report title
- "reportFilename" and "reportDir" to specify name and locations of output reports
- "embeddedScreenshots" to attach screenshots for failed cases
- "timestamp" to append timestamp to report name, prevent replacement
- After executions, reports are located in cypress/reports
- Head to cypress-multi-reporters for more configurations
- Install cypress-pugin-snapshots
npm install --save-dev cypress-plugin-snapshots
- Add this config to cypress.json file
"ignoreTestFiles": [
"**/__snapshots__/*",
"**/__image_snapshots__/*"
]
- Modify /plugin/index.js with
const { initPlugin } = require('cypress-plugin-snapshots/plugin');
module.exports = (on, config) => {
initPlugin(on, config);
}
- Import this into /support/index.js
import 'cypress-plugin-snapshots/commands';
- Config the settings
- Take snapshot
cy.toMatchSnapshot();
cy.get(<element>).toMatchSnapshot();
-
Run the test the first time to get the base snapshots
-
To update snapshots when there are changes where expected
npx cypress run --env updateSnapshots=true --spec cypress/integration/atwt-ui/ui-regression.spec.js --browser chrome
- Head to cypress-plugin-snapshots for more configurations
- Install cypress-audit
npm install --save-dev cypress-audit
- Set up prepareAudit when brower launch in /plugin/index.js
on('before:browser:launch', (browser = {}, launchOptions) => {
prepareAudit(launchOptions); });
- Set up task to write test result into report files
- Use cy.lighthouse() or cy.pa11y() to run performance testing
- After executions, reports are located in cypress/reports
- Install ajv
npm install --save-dev ajv
- Add new command vlidateSchema in /support/validate-schema-command.js
export const validateSchema = (schema, response) => {
const ajv = new Ajv();
const validate = ajv.compile(schema);
const valid = validate(response);
if (!valid) {
getSchemaError(validate.errors).then((schemaError) => {
throw new Error(schemaError);
});
} else {
cy.log("Schema validated!");
}
};
- Add the defined command in support/commands.js of Cypress
import { validateSchema } from "./validate-schema-command";
Cypress.Commands.add("validateSchema", validateSchema);
- Create schema file in support/schema
- Import schema file and call cy.validateSchema() to validate response schema
- Head to AJV for more configurations
CI | Build status | Config File | Set up Cypress Dashboard |
---|---|---|---|
CircleCI | config.yml | Already set up with CYPRESS_RECORD_KEY as enviroment variable in CircleCI | |
CircleCI - v2 Config without Orbs | Not activated | .circleci/config-without-orbs.yml | Already set up with CYPRESS_RECORD_KEY as enviroment variable in CircleCI |
TravisCI | .travis.yml | Comment out code lines with record on Cypress Dashboard | |
GitlabCI | .gitlab-ci.yml | Comment out code lines with record on Cypress Dashboard | |
Github Actions | main.yml | Comment out code lines with record on Cypress Dashboard | |
Github Actions - Sync Bitbucket | sync-bitbucket-https.yml | To sync code to Bitbucket | |
Github Actions - Sync Gitlab | sync-gitlab-https.yml | To sync code to Gitlab |
Copyright 2021 Tuyen Nguyen
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.