forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-command-spec.cy.js
57 lines (49 loc) · 1.41 KB
/
custom-command-spec.cy.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/// <reference types="cypress" />
// see https://on.cypress.io/custom-commands
//
// typically we'd put this in cypress/support/commands.js
// but because this custom command is specific to this example
// we'll keep it here
Cypress.Commands.add('loginByJSON', (username, password) => {
Cypress.log({
name: 'login by JSON',
message: `${username} | ${password}`,
})
return cy.request({
method: 'POST',
url: '/login',
body: {
username,
password,
},
})
})
context('Reusable "login" custom command', function () {
const username = 'jane.lane'
const password = 'password123'
beforeEach(function () {
// login before each test
cy.loginByJSON(username, password)
})
it('can visit /dashboard', function () {
cy.visit('/dashboard')
cy.get('h1').should('contain', 'jane.lane')
})
it('can visit /users', function () {
cy.visit('/users')
cy.get('h1').should('contain', 'Users')
})
it('can simply request other authenticated pages', function () {
// instead of visiting each page and waiting for all
// the associated resources to load, we can instead
// just issue a simple HTTP request and make an
// assertion about the response body
cy.request('/admin')
.its('body')
.should('include', '<h1>Admin</h1>')
})
it('can visit other authenticated pages', function () {
cy.visit('/admin')
cy.contains('h1', 'Admin')
})
})