forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.js
90 lines (80 loc) · 2.82 KB
/
spec.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/// <reference types="cypress" />
import { userService } from '../../src/_services/user.service'
// log in using application code
it('logs in by using application service', () => {
cy.log('user service login')
// see https://on.cypress.io/wrap
// cy.wrap(user promise) forces the test commands to wait until
// the user promise resolves. We also don't want to log empty "wrap {}"
// to the command log, since we already logged a good message right above
cy.wrap(userService.login(Cypress.env('username'), Cypress.env('password')), {
log: false
}).then(user => {
// the userService.login resolves with "user" object
// and we can assert its values inside .then()
// confirm general shape of the object
expect(user).to.be.an('object')
expect(user).to.have.keys([
'firstName',
'lastName',
'username',
'id',
'token'
])
// we don't know the token or id, but we know the expected names
expect(user).to.contain({
username: 'test',
firstName: 'Test',
lastName: 'User'
})
})
// cy.visit command will wait for the promise returned from
// the "userService.login" to resolve. Then local storage item is set
// and the visit will immediately be authenticated and logged in
cy.visit('/')
// we should be logged in
cy.contains('Hi Test!').should('be.visible')
})
it('can assert against resolved object using .should', () => {
cy.log('user service login')
// same login promise
cy.wrap(userService.login(Cypress.env('username'), Cypress.env('password')), {
log: false
})
// but resolved value checked using implicit assertions
// that can be easier to read
.should('be.an', 'object')
.and('have.keys', ['firstName', 'lastName', 'username', 'id', 'token'])
.and('contain', {
username: 'test',
firstName: 'Test',
lastName: 'User'
})
// cy.visit command will wait for the promise returned from
// the "userService.login" to resolve. Then local storage item is set
// and the visit will immediately be authenticated and logged in
cy.visit('/')
// we should be logged in
cy.contains('Hi Test!').should('be.visible')
})
/**
* Custom command to log in using application method.
* Commands are automatically waited on, thus we don't need extra "cy.wrap"
* around the returned promise.
*
* @example cy.login()
*/
Cypress.Commands.add(
'login',
(username = Cypress.env('username'), password = Cypress.env('password')) => {
return userService.login(username, password)
}
)
it('log in by wrapping application code in custom command', () => {
// custom commands are automatically chained
cy.login()
// thus the visit will not start until the promise returned
// by the application code inside the custom command "login" resolves
cy.visit('/')
cy.contains('Hi Test!').should('be.visible')
})