Cypress - Functional testing
Features
- Developer friendly.
- Open source (MIT).
- It can records snapshots and videos of your tests.
- It has a test runner console.
- It only currently supports Chrome variants and Firefox.
- It doesn’t have native iframe support, though there are workarounds.
- It has its own promise-based system that you have to use (it can’t use ES6 promises).
Functional E2E Testing
Set up
As dev dependency.
1
npm install cypress --save-dev
Just use it for execution.
1
2# launches the modal helper
npx cypress open
Write tests
- Create configuration.
Add it to
cypress.json
file.1
2
3{
"baseUrl": "http://localhost:8000/"
}
- Create the runner.
Starts the dev server, runs all tests fro
my_module
, viascripts/cypress.js
.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16return devServer.listen(port, HOST, err => {
if (err) {
return console.log(err);
}
if (isInteractive) {
clearConsole();
}
console.log(chalk.cyan('Starting the development server...\n'));
return cypress
.run({
spec: './cypress/integration/my_module/*.js',
})
.then(results => {
devServer.close();
});
});
- Create the test file.
cypress/integration/my_module/sidebar.js
.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28/* globals context cy */
/// <reference types="Cypress" />
context('Sidebar', () => {
beforeEach(() => {
cy.visit('/');
// example: review progress bar
cy.contains('Progress').should('exist');
});
// dummy example
it('does something', () => {
cy.contains('My_module Exercises');
});
it('closes when X is clicked and reopens when hamburger is clicked', () => {
cy.get('[data-testid=closeSidebar]').click();
cy.contains('Progress').should('not.exist');
cy.get('[data-testid=openSidebar]').click();
cy.contains('Progress').should('exist');
});
it('navigates to /up-going when Up & Going is picked', () => {
cy.contains(/Up & Going \(/).click({ force: true });
cy.url().should('include', '/up-going');
cy.contains('Chapter 1: Into Programming').should('exist');
cy.contains('Chapter 2: Into JavaScript').should('exist');
});
});
- Trigger tests.
Add command on scripts as via
package.json
.1
2
3"scripts": {
"cypress": "node scripts/cypress.js"
},
Code snippets
Visit a page
1 | describe('My First Test', () => { |
Query for an element
1 | describe('My First Test', () => { |
Click on an element
1 | describe('My First Test', () => { |
Make an assertion
1 | describe('My First Test', () => { |
Multi-command/multi-assertion
1 | describe('My First Test', () => { |