-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
using-ui-spec.cy.js
64 lines (54 loc) · 1.75 KB
/
using-ui-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
58
59
60
61
62
63
64
/// <reference types="cypress" />
describe('logs in', () => {
it('using UI', () => {
cy.visit('/')
cy.location('pathname').should('equal', '/login')
// enter valid username and password
cy.get('[name=username]').type(Cypress.env('username'))
cy.get('[name=password]').type(Cypress.env('password'))
cy.contains('button', 'Login').click()
// confirm we have logged in successfully
cy.location('pathname').should('equal', '/')
cy.contains('Hi Test!')
.should('be.visible')
.then(() => {
/* global window */
const userString = window.localStorage.getItem('user')
expect(userString).to.be.a('string')
const user = JSON.parse(userString)
expect(user).to.be.an('object')
expect(user).to.have.keys([
'id',
'username',
'firstName',
'lastName',
'token',
])
expect(user.token).to.be.a('string')
})
// now we can log out
cy.contains('a', 'Logout').click()
cy.location('pathname').should('equal', '/login')
})
it('fails to access protected resource', () => {
cy.request({
url: 'http://localhost:4000/users',
failOnStatusCode: false,
})
.its('status')
.should('equal', 401)
})
it('Does not log in with invalid password', () => {
cy.visit('/')
cy.location('pathname').should('equal', '/login')
// try logging in with invalid password
cy.get('[name=username]').type('username')
cy.get('[name=password]').type('wrong-password')
cy.contains('button', 'Login').click()
// still on /login page plus an error is displayed
cy.location('pathname').should('equal', '/login')
cy.contains('.alert-danger', 'Username or password is incorrect').should(
'be.visible'
)
})
})