Slide 1

Slide 1 text

Testing Advanced Cypress Testing Jeremy Fairbank @elpapapollo

Slide 2

Slide 2 text

@testdouble helps improves how the world build software. testdouble.com

Slide 3

Slide 3 text

Available in print or e-book programming-elm.com

Slide 4

Slide 4 text

Testing anti-patterns

Slide 5

Slide 5 text

Assumptions

Slide 6

Slide 6 text

Assumptions

Slide 7

Slide 7 text

Assumptions End-to-End

Slide 8

Slide 8 text

Assumptions End-to-End Real API

Slide 9

Slide 9 text

Assumptions End-to-End Real API Test Data

Slide 10

Slide 10 text

The App bit.ly/act-repo

Slide 11

Slide 11 text

Selector Syndrome

Slide 12

Slide 12 text

import albums from '../../../server/jazz-albums-test-pristine.json' describe('Home page', function () { it('albums can be viewed on home page', function () { cy.get('.album-list-item').should('have.length', albums.length) albums.forEach((album) => { cy.get('.album-list-item h2') .contains(album.title) .parent('.album-list-item') .should('exist') .find('h3') .should('contain', album.artists.join(' - ')) }) }) })

Slide 13

Slide 13 text

import albums from '../../../server/jazz-albums-test-pristine.json' describe('Home page', function () { it('albums can be viewed on home page', function () { cy.get('.album-list-item').should('have.length', albums.length) albums.forEach((album) => { cy.get('.album-list-item h2') .contains(album.title) .parent('.album-list-item') .should('exist') .find('h3') .should('contain', album.artists.join(' - ')) }) }) })

Slide 14

Slide 14 text

import albums from '../../../server/jazz-albums-test-pristine.json' describe('Home page', function () { it('albums can be viewed on home page', function () { cy.get('.album-list-item').should('have.length', albums.length) albums.forEach((album) => { cy.get('.album-list-item h2') .contains(album.title) .parent('.album-list-item') .should('exist') .find('h3') .should('contain', album.artists.join(' - ')) }) }) })

Slide 15

Slide 15 text

import albums from '../../../server/jazz-albums-test-pristine.json' describe('Home page', function () { it('albums can be viewed on home page', function () { cy.get('.album-list-item').should('have.length', albums.length) albums.forEach((album) => { cy.get('.album-list-item h2') .contains(album.title) .parent('.album-list-item') .should('exist') .find('h3') .should('contain', album.artists.join(' - ')) }) }) })

Slide 16

Slide 16 text

import albums from '../../../server/jazz-albums-test-pristine.json' describe('Home page', function () { it('albums can be viewed on home page', function () { cy.get('.album-list-item').should('have.length', albums.length) albums.forEach((album) => { cy.get('.album-list-item h2') .contains(album.title) .parent('.album-list-item') .should('exist') .find('h3') .should('contain', album.artists.join(' - ')) }) }) })

Slide 17

Slide 17 text

import albums from '../../../server/jazz-albums-test-pristine.json' describe('Home page', function () { it('albums can be viewed on home page', function () { cy.get('.album-list-item').should('have.length', albums.length) albums.forEach((album) => { cy.get('.album-list-item h2') .contains(album.title) .parent('.album-list-item') .should('exist') .find('h3') .should('contain', album.artists.join(' - ')) }) }) })

Slide 18

Slide 18 text

import albums from '../../../server/jazz-albums-test-pristine.json' describe('Home page', function () { it('albums can be viewed on home page', function () { cy.get('.album-list-item').should('have.length', albums.length) albums.forEach((album) => { cy.get('.album-list-item h2') .contains(album.title) .parent('.album-list-item') .should('exist') .find('h3') .should('contain', album.artists.join(' - ')) }) }) })

Slide 19

Slide 19 text

it('sorts albums', function () { function albumsSortedLike(albumList) { albumList.forEach((album, index) => { cy.get('.album-list-item') .eq(index) .find('h2') .should('contain', album.title) }) } cy.contains('.filter-section', 'Sort By').find('select').select('Title') albumsSortedLike(albumsSortedByTitle) cy.contains('.filter-section', 'Sort By').find('select').select('Artist') albumsSortedLike(albumsSortedByArtists) cy.contains('.filter-section', 'Sort By').find('select').select('Default') albumsSortedLike(albumsSortedById) })

Slide 20

Slide 20 text

it('sorts albums', function () { function albumsSortedLike(albumList) { albumList.forEach((album, index) => { cy.get('.album-list-item') .eq(index) .find('h2') .should('contain', album.title) }) } cy.contains('.filter-section', 'Sort By').find('select').select('Title') albumsSortedLike(albumsSortedByTitle) cy.contains('.filter-section', 'Sort By').find('select').select('Artist') albumsSortedLike(albumsSortedByArtists) cy.contains('.filter-section', 'Sort By').find('select').select('Default') albumsSortedLike(albumsSortedById) })

Slide 21

Slide 21 text

it('sorts albums', function () { function albumsSortedLike(albumList) { albumList.forEach((album, index) => { cy.get('.album-list-item') .eq(index) .find('h2') .should('contain', album.title) }) } cy.contains('.filter-section', 'Sort By').find('select').select('Title') albumsSortedLike(albumsSortedByTitle) cy.contains('.filter-section', 'Sort By').find('select').select('Artist') albumsSortedLike(albumsSortedByArtists) cy.contains('.filter-section', 'Sort By').find('select').select('Default') albumsSortedLike(albumsSortedById) })

Slide 22

Slide 22 text

it('sorts albums', function () { function albumsSortedLike(albumList) { albumList.forEach((album, index) => { cy.get('.album-list-item') .eq(index) .find('h2') .should('contain', album.title) }) } cy.contains('.filter-section', 'Sort By').find('select').select('Title') albumsSortedLike(albumsSortedByTitle) cy.contains('.filter-section', 'Sort By').find('select').select('Artist') albumsSortedLike(albumsSortedByArtists) cy.contains('.filter-section', 'Sort By').find('select').select('Default') albumsSortedLike(albumsSortedById) })

Slide 23

Slide 23 text

it('sorts albums', function () { function albumsSortedLike(albumList) { albumList.forEach((album, index) => { cy.get('.album-list-item') .eq(index) .find('h2') .should('contain', album.title) }) } cy.contains('.filter-section', 'Sort By').find('select').select('Title') albumsSortedLike(albumsSortedByTitle) cy.contains('.filter-section', 'Sort By').find('select').select('Artist') albumsSortedLike(albumsSortedByArtists) cy.contains('.filter-section', 'Sort By').find('select').select('Default') albumsSortedLike(albumsSortedById) })

Slide 24

Slide 24 text

it('sorts albums', function () { function albumsSortedLike(albumList) { albumList.forEach((album, index) => { cy.get('.album-list-item') .eq(index) .find('h2') .should('contain', album.title) }) } cy.contains('.filter-section', 'Sort By').find('select').select('Title') albumsSortedLike(albumsSortedByTitle) cy.contains('.filter-section', 'Sort By').find('select').select('Artist') albumsSortedLike(albumsSortedByArtists) cy.contains('.filter-section', 'Sort By').find('select').select('Default') albumsSortedLike(albumsSortedById) })

Slide 25

Slide 25 text

it('sorts albums', function () { function albumsSortedLike(albumList) { albumList.forEach((album, index) => { cy.get('.album-list-item') .eq(index) .find('h2') .should('contain', album.title) }) } cy.contains('.filter-section', 'Sort By').find('select').select('Title') albumsSortedLike(albumsSortedByTitle) cy.contains('.filter-section', 'Sort By').find('select').select('Artist') albumsSortedLike(albumsSortedByArtists) cy.contains('.filter-section', 'Sort By').find('select').select('Default') albumsSortedLike(albumsSortedById) })

Slide 26

Slide 26 text

it('searches for artists', function () { cy.contains('.filter-section', 'Search Artists') .find('input') .type('John Coltrane') cy.get('.album-list-item') .should('have.length', 2) .find('h2') .should('contain', 'A Love Supreme') .and('contain', 'Blue Train') .parent('.album-list-item') .find('h3') .should('contain', 'John Coltrane') })

Slide 27

Slide 27 text

it('searches for artists', function () { cy.contains('.filter-section', 'Search Artists') .find('input') .type('John Coltrane') cy.get('.album-list-item') .should('have.length', 2) .find('h2') .should('contain', 'A Love Supreme') .and('contain', 'Blue Train') .parent('.album-list-item') .find('h3') .should('contain', 'John Coltrane') })

Slide 28

Slide 28 text

it('searches for artists', function () { cy.contains('.filter-section', 'Search Artists') .find('input') .type('John Coltrane') cy.get('.album-list-item') .should('have.length', 2) .find('h2') .should('contain', 'A Love Supreme') .and('contain', 'Blue Train') .parent('.album-list-item') .find('h3') .should('contain', 'John Coltrane') })

Slide 29

Slide 29 text

it('searches for artists', function () { cy.contains('.filter-section', 'Search Artists') .find('input') .type('John Coltrane') cy.get('.album-list-item') .should('have.length', 2) .find('h2') .should('contain', 'A Love Supreme') .and('contain', 'Blue Train') .parent('.album-list-item') .find('h3') .should('contain', 'John Coltrane') })

Slide 30

Slide 30 text

it('searches for artists', function () { cy.contains('.filter-section', 'Search Artists') .find('input') .type('John Coltrane') cy.get('.album-list-item') .should('have.length', 2) .find('h2') .should('contain', 'A Love Supreme') .and('contain', 'Blue Train') .parent('.album-list-item') .find('h3') .should('contain', 'John Coltrane') })

Slide 31

Slide 31 text

it('searches for artists', function () { cy.contains('.filter-section', 'Search Artists') .find('input') .type('John Coltrane') cy.get('.album-list-item') .should('have.length', 2) .find('h2') .should('contain', 'A Love Supreme') .and('contain', 'Blue Train') .parent('.album-list-item') .find('h3') .should('contain', 'John Coltrane') })

Slide 32

Slide 32 text

it('searches for artists', function () { cy.contains('.filter-section', 'Search Artists') .find('input') .type('John Coltrane') cy.get('.album-list-item') .should('have.length', 2) .find('h2') .should('contain', 'A Love Supreme') .and('contain', 'Blue Train') .parent('.album-list-item') .find('h3') .should('contain', 'John Coltrane') })

Slide 33

Slide 33 text

Markup or CSS changes?

Slide 34

Slide 34 text

const AlbumListItem = ({ album }) => (

{album.title}

{album.artists.join(' - ')}

)

Slide 35

Slide 35 text

const AlbumListItem = ({ album }) => (

{album.title}

{album.artists.join(' - ')}

)

Slide 36

Slide 36 text

const AlbumListItem = ({ album }) => (

{album.title}

{album.artists.join(' - ')}

)

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

const AlbumListItem = ({ album }) => (

{album.title}

{album.artists.join(' - ')}

)

Slide 39

Slide 39 text

const AlbumListItemSelectors = ({ album }) => (

{album.title}

{album.artists.join(' - ')}

)

Slide 40

Slide 40 text

const AlbumListItemSelectors = ({ album }) => (

{album.title}

{album.artists.join(' - ')}

)

Slide 41

Slide 41 text

it('albums can be viewed on home page', function () { cy.get('[data-test="album-list-item"]') .should('have.length', albums.length) albums.forEach((album) => { cy.get('[data-test="title"]') .contains(album.title) .parent('[data-test="album-list-item"]') .should('exist') .find('[data-test="artists"]') .should('contain', album.artists.join(' - ')) }) })

Slide 42

Slide 42 text

it('albums can be viewed on home page', function () { cy.get('[data-test="album-list-item"]') .should('have.length', albums.length) albums.forEach((album) => { cy.get('[data-test="title"]') .contains(album.title) .parent('[data-test="album-list-item"]') .should('exist') .find('[data-test="artists"]') .should('contain', album.artists.join(' - ')) }) })

Slide 43

Slide 43 text

it('albums can be viewed on home page', function () { cy.get('[data-test="album-list-item"]') .should('have.length', albums.length) albums.forEach((album) => { cy.get('[data-test="title"]') .contains(album.title) .parent('[data-test="album-list-item"]') .should('exist') .find('[data-test="artists"]') .should('contain', album.artists.join(' - ')) }) })

Slide 44

Slide 44 text

it('albums can be viewed on home page', function () { cy.get('[data-test="album-list-item"]') .should('have.length', albums.length) albums.forEach((album) => { cy.get('[data-test="title"]') .contains(album.title) .parent('[data-test="album-list-item"]') .should('exist') .find('[data-test="artists"]') .should('contain', album.artists.join(' - ')) }) })

Slide 45

Slide 45 text

it('albums can be viewed on home page', function () { cy.get('[data-test="album-list-item"]') .should('have.length', albums.length) albums.forEach((album) => { cy.get('[data-test="title"]') .contains(album.title) .parent('[data-test="album-list-item"]') .should('exist') .find('[data-test="artists"]') .should('contain', album.artists.join(' - ')) }) })

Slide 46

Slide 46 text

it('albums can be viewed on home page', function () { cy.get('[data-test="album-list-item"]') .should('have.length', albums.length) albums.forEach((album) => { cy.contains('[data-test="album-list-item"]', album.title) .should('contain', album.artists.join(' - ')) }) })

Slide 47

Slide 47 text

setSorter(e.target.value)} data-test="sort-by" > Default Title Artist

Slide 48

Slide 48 text

cy.get('[data-test="sort-by"]').select('Title') albumsSortedLike(albumsSortedByTitle) cy.get('[data-test="sort-by"]').select('Artist') albumsSortedLike(albumsSortedByArtists) cy.get('[data-test="sort-by"]').select('Default') albumsSortedLike(albumsSortedById)

Slide 49

Slide 49 text

it('searches for artists', function () { cy.get('[data-test="search-artists"]') .type('John Coltrane') cy.get('[data-test="album-list-item"]') .should('have.length', 2) .and('contain', 'A Love Supreme') .and('contain', 'Blue Train') .and('contain', 'John Coltrane') })

Slide 50

Slide 50 text

it('searches for artists', function () { cy.get('[data-test="search-artists"]') .type('John Coltrane') cy.get('[data-test="album-list-item"]') .should('have.length', 2) .and('contain', 'A Love Supreme') .and('contain', 'Blue Train') .and('contain', 'John Coltrane') })

Slide 51

Slide 51 text

it('searches for artists', function () { cy.get('[data-test="search-artists"]') .type('John Coltrane') cy.get('[data-test="album-list-item"]') .should('have.length', 2) .and('contain', 'A Love Supreme') .and('contain', 'Blue Train') .and('contain', 'John Coltrane') })

Slide 52

Slide 52 text

it('albums can be viewed on home page', function () { cy.get('[data-test="album-list-item"]') .should('have.length', albums.length) albums.forEach((album) => { cy.get('[data-test="title"]') .contains(album.title) .parent('[data-test="album-list-item"]') .should('exist') .find('[data-test="artists"]') .should('contain', album.artists.join(' - ')) }) })

Slide 53

Slide 53 text

it('albums can be viewed on home page', function () { cy.get('[data-test="album-list-item"]') .should('have.length', albums.length) albums.forEach((album) => { cy.get('[data-test="title"]') .contains(album.title) .parent('[data-test="album-list-item"]') .should('exist') .find('[data-test="artists"]') .should('contain', album.artists.join(' - ')) }) })

Slide 54

Slide 54 text

Page Objects

Slide 55

Slide 55 text

// cypress/support/pages/home.js export const albums = () => cy.get('[data-test="album-list-item"]') export const album = (title) => cy.contains('[data-test="album-list-item"]', title) export const sortBy = () => cy.get('[data-test="sort-by"]') export const searchArtists = () => cy.get('[data-test="search-artists"]')

Slide 56

Slide 56 text

// cypress/support/pages/home.js export const albums = () => cy.get('[data-test="album-list-item"]') export const album = (title) => cy.contains('[data-test="album-list-item"]', title) export const sortBy = () => cy.get('[data-test="sort-by"]') export const searchArtists = () => cy.get('[data-test="search-artists"]')

Slide 57

Slide 57 text

// cypress/support/pages/home.js export const albums = () => cy.get('[data-test="album-list-item"]') export const album = (title) => cy.contains('[data-test="album-list-item"]', title) export const sortBy = () => cy.get('[data-test="sort-by"]') export const searchArtists = () => cy.get('[data-test="search-artists"]')

Slide 58

Slide 58 text

import * as homePage from '../support/pages/home' it('albums can be viewed on home page', function () { homePage .albums() .should('have.length', albums.length) albums.forEach((album) => { homePage .album(album.title) .should('exist') .and('contain', album.artists.join(' - ')) }) })

Slide 59

Slide 59 text

import * as homePage from '../support/pages/home' it('albums can be viewed on home page', function () { homePage .albums() .should('have.length', albums.length) albums.forEach((album) => { homePage .album(album.title) .should('exist') .and('contain', album.artists.join(' - ')) }) })

Slide 60

Slide 60 text

import * as homePage from '../support/pages/home' it('albums can be viewed on home page', function () { homePage .albums() .should('have.length', albums.length) albums.forEach((album) => { homePage .album(album.title) .should('exist') .and('contain', album.artists.join(' - ')) }) })

Slide 61

Slide 61 text

import * as homePage from '../support/pages/home' it('albums can be viewed on home page', function () { homePage .albums() .should('have.length', albums.length) albums.forEach((album) => { homePage .album(album.title) .should('exist') .and('contain', album.artists.join(' - ')) }) })

Slide 62

Slide 62 text

// cypress/support/pages/home.js export const albums = () => cy.get('[data-test="album-list-item"]') export const album = (title) => cy.contains('[data-test="album-list-item"]', title) export const sortBy = () => cy.get('[data-test="sort-by"]') export const searchArtists = () => cy.get('[data-test="search-artists"]')

Slide 63

Slide 63 text

// cypress/support/pages/home.js export const albums = () => cy.get('[data-test="album-list-item"]') export const album = (title) => cy.contains('[data-test="album-list-item"]', title) export const sortBy = () => cy.get('[data-test="sort-by"]') export const searchArtists = () => cy.get('[data-test="search-artists"]')

Slide 64

Slide 64 text

// cypress/support/pages/home.js export const albums = () => cy.get('[data-test="album-list-item"]') export const album = (title) => cy.contains('[data-test="album-list-item"]', title) export const sortBy = () => cy.get('[data-test="sort-by"]') export const searchArtists = () => cy.get('[data-test="search-artists"]')

Slide 65

Slide 65 text

it('sorts albums', function () { function albumsSortedLike(albumList) { albumList.forEach((album, index) => { homePage.albums().eq(index).should('contain', album.title) }) } homePage.sortBy().select('Title') albumsSortedLike(albumsSortedByTitle) homePage.sortBy().select('Artist') albumsSortedLike(albumsSortedByArtists) homePage.sortBy().select('Default') albumsSortedLike(albumsSortedById) })

Slide 66

Slide 66 text

it('sorts albums', function () { function albumsSortedLike(albumList) { albumList.forEach((album, index) => { homePage.albums().eq(index).should('contain', album.title) }) } homePage.sortBy().select('Title') albumsSortedLike(albumsSortedByTitle) homePage.sortBy().select('Artist') albumsSortedLike(albumsSortedByArtists) homePage.sortBy().select('Default') albumsSortedLike(albumsSortedById) })

Slide 67

Slide 67 text

it('sorts albums', function () { function albumsSortedLike(albumList) { albumList.forEach((album, index) => { homePage.albums().eq(index).should('contain', album.title) }) } homePage.sortBy().select('Title') albumsSortedLike(albumsSortedByTitle) homePage.sortBy().select('Artist') albumsSortedLike(albumsSortedByArtists) homePage.sortBy().select('Default') albumsSortedLike(albumsSortedById) })

Slide 68

Slide 68 text

it('searches for artists', function () { homePage.searchArtists().type('John Coltrane') homePage .albums() .should('have.length', 2) .and('contain', 'A Love Supreme') .and('contain', 'Blue Train') .and('contain', 'John Coltrane') })

Slide 69

Slide 69 text

it('searches for artists', function () { homePage.searchArtists().type('John Coltrane') homePage .albums() .should('have.length', 2) .and('contain', 'A Love Supreme') .and('contain', 'Blue Train') .and('contain', 'John Coltrane') })

Slide 70

Slide 70 text

it('searches for artists', function () { homePage.searchArtists().type('John Coltrane') homePage .albums() .should('have.length', 2) .and('contain', 'A Love Supreme') .and('contain', 'Blue Train') .and('contain', 'John Coltrane') })

Slide 71

Slide 71 text

Repetition

Slide 72

Slide 72 text

export const albums = () => cy.get('[data-test="album-list-item"]') export const album = (title) => cy.contains('[data-test="album-list-item"]', title) export const sortBy = () => cy.get('[data-test="sort-by"]') export const searchArtists = () => cy.get('[data-test="search-artists"]')

Slide 73

Slide 73 text

export const albums = () => cy.get('[data-test="album-list-item"]') export const album = (title) => cy.contains('[data-test="album-list-ite"]', title) export const sortBy = () => cy.get('[data-test=sort-by"]') export const searchArtists = () => cy.get('[data-test="search-artists"')

Slide 74

Slide 74 text

export const albums = () => cy.get('[data-test="album-list-item"]') export const album = (title) => cy.contains('[data-test="album-list-ite"]', title) export const sortBy = () => cy.get('[data-test=sort-by"]') export const searchArtists = () => cy.get('[data-test="search-artists"')

Slide 75

Slide 75 text

export const albums = () => cy.get('[data-test="album-list-item"]') export const album = (title) => cy.contains('[data-test="album-list-ite"]', title) export const sortBy = () => cy.get('[data-test=sort-by"]') export const searchArtists = () => cy.get('[data-test="search-artists"')

Slide 76

Slide 76 text

Commands

Slide 77

Slide 77 text

// cypress/support/commands.js Cypress.Commands.add( 'getByDataTest', (key) => cy.get(`[data-test="${key}"]`) ) // cypress/support/index.js import './commands'

Slide 78

Slide 78 text

// cypress/support/commands.js Cypress.Commands.add( 'getByDataTest', (key) => cy.get(`[data-test="${key}"]`) ) // cypress/support/index.js import './commands'

Slide 79

Slide 79 text

// cypress/support/commands.js Cypress.Commands.add( 'getByDataTest', (key) => cy.get(`[data-test="${key}"]`) ) // cypress/support/index.js import './commands'

Slide 80

Slide 80 text

// cypress/support/commands.js Cypress.Commands.add( 'getByDataTest', (key) => cy.get(`[data-test="${key}"]`) ) // cypress/support/index.js import './commands'

Slide 81

Slide 81 text

// cypress/support/commands.js Cypress.Commands.add( 'getByDataTest', (key) => cy.get(`[data-test="${key}"]`) ) // cypress/support/index.js import './commands'

Slide 82

Slide 82 text

// cypress/support/commands.js Cypress.Commands.add( 'getByDataTest', (key) => cy.get(`[data-test="${key}"]`) ) // cypress/support/index.js import './commands'

Slide 83

Slide 83 text

export const albums = () => cy.get('[data-test="album-list-item"]') export const album = (title) => cy.contains('[data-test="album-list-item"]', title) export const sortBy = () => cy.get('[data-test="sort-by"]') export const searchArtists = () => cy.get('[data-test="search-artists"]')

Slide 84

Slide 84 text

export const albums = () => cy.getByDataTest('album-list-item') export const album = (title) => cy.contains('[data-test="album-list-item"]', title) export const sortBy = () => cy.getByDataTest('sort-by') export const searchArtists = () => cy.getByDataTest('search-artists')

Slide 85

Slide 85 text

export const albums = () => cy.getByDataTest('album-list-item') export const album = (title) => cy.contains('[data-test="album-list-item"]', title) export const sortBy = () => cy.getByDataTest('sort-by') export const searchArtists = () => cy.getByDataTest('search-artists')

Slide 86

Slide 86 text

Cypress.Commands.add( 'containsByDataTest', (key, content) => cy.contains(`[data-test="${key}"]`, content) )

Slide 87

Slide 87 text

Cypress.Commands.add( 'containsByDataTest', (key, content) => cy.contains(`[data-test="${key}"]`, content) )

Slide 88

Slide 88 text

export const album = (title) => cy.containsByDataTest('album-list-item', title)

Slide 89

Slide 89 text

API Timeouts

Slide 90

Slide 90 text

describe('Album page', function () { // ... it('can be viewed', function () { albumPage.title().should('contain', this.album.title) albumPage.artists().should('contain', this.album.artists) }) // ... })

Slide 91

Slide 91 text

describe('Album page', function () { // ... it('can be viewed', function () { albumPage.title().should('contain', this.album.title) albumPage.artists().should('contain', this.album.artists) }) // ... })

Slide 92

Slide 92 text

Wait

Slide 93

Slide 93 text

it('can be viewed', function () { cy.wait(6000) albumPage.title().should('contain', this.album.title) albumPage.artists().should('contain', this.album.artists) })

Slide 94

Slide 94 text

it('can be viewed', function () { cy.wait(6000) albumPage.title().should('contain', this.album.title) albumPage.artists().should('contain', this.album.artists) })

Slide 95

Slide 95 text

it('can be viewed', function () { cy.wait(6000) albumPage.title().should('contain', this.album.title) albumPage.artists().should('contain', this.album.artists) })

Slide 96

Slide 96 text

beforeEach(function () { cy.server() cy.route(buildApiUrl('/albums/*')).as('getAlbum') // ... }) Routes and Aliases

Slide 97

Slide 97 text

beforeEach(function () { cy.server() cy.route(buildApiUrl('/albums/*')).as('getAlbum') // ... }) Routes and Aliases

Slide 98

Slide 98 text

beforeEach(function () { cy.server() cy.route(buildApiUrl('/albums/*')).as('getAlbum') // ... }) Routes and Aliases

Slide 99

Slide 99 text

beforeEach(function () { cy.server() cy.route(buildApiUrl('/albums/*')).as('getAlbum') // ... }) Routes and Aliases

Slide 100

Slide 100 text

beforeEach(function () { cy.server() cy.route(buildApiUrl('/albums/*')).as('getAlbum') // ... }) Routes and Aliases

Slide 101

Slide 101 text

it('can be viewed', function () { cy.wait('@getAlbum') albumPage.title().should('contain', this.album.title) albumPage.artists().should('contain', this.album.artists) })

Slide 102

Slide 102 text

Api Testing

Slide 103

Slide 103 text

describe('REST API: albums', function () { it('can fetch all albums', function () { cy.request({ url: buildApiUrl('/albums'), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('have.length', this.albums.length) .each((album) => { expect(this.albumTitles).to.include(album.title) }) }) })

Slide 104

Slide 104 text

describe('REST API: albums', function () { it('can fetch all albums', function () { cy.request({ url: buildApiUrl('/albums'), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('have.length', this.albums.length) .each((album) => { expect(this.albumTitles).to.include(album.title) }) }) })

Slide 105

Slide 105 text

describe('REST API: albums', function () { it('can fetch all albums', function () { cy.request({ url: buildApiUrl('/albums'), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('have.length', this.albums.length) .each((album) => { expect(this.albumTitles).to.include(album.title) }) }) })

Slide 106

Slide 106 text

describe('REST API: albums', function () { it('can fetch all albums', function () { cy.request({ url: buildApiUrl('/albums'), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('have.length', this.albums.length) .each((album) => { expect(this.albumTitles).to.include(album.title) }) }) })

Slide 107

Slide 107 text

describe('REST API: albums', function () { it('can fetch all albums', function () { cy.request({ url: buildApiUrl('/albums'), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('have.length', this.albums.length) .each((album) => { expect(this.albumTitles).to.include(album.title) }) }) })

Slide 108

Slide 108 text

describe('REST API: albums', function () { it('can fetch all albums', function () { cy.request({ url: buildApiUrl('/albums'), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('have.length', this.albums.length) .each((album) => { expect(this.albumTitles).to.include(album.title) }) }) })

Slide 109

Slide 109 text

describe('REST API: albums', function () { it('can fetch all albums', function () { cy.request({ url: buildApiUrl('/albums'), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('have.length', this.albums.length) .each((album) => { expect(this.albumTitles).to.include(album.title) }) }) })

Slide 110

Slide 110 text

describe('REST API: albums', function () { it('can fetch all albums', function () { cy.request({ url: buildApiUrl('/albums'), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('have.length', this.albums.length) .each((album) => { expect(this.albumTitles).to.include(album.title) }) }) })

Slide 111

Slide 111 text

it('can fetch an album', function () { const album = this.albums[0] cy.request({ url: buildApiUrl(`/albums/${album.id}`), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('containSubset', { id: album.id, title: album.title, artists: album.artists, rating: 'NOT_RATED', userReviews: [], }) })

Slide 112

Slide 112 text

it('can fetch an album', function () { const album = this.albums[0] cy.request({ url: buildApiUrl(`/albums/${album.id}`), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('containSubset', { id: album.id, title: album.title, artists: album.artists, rating: 'NOT_RATED', userReviews: [], }) })

Slide 113

Slide 113 text

it('can fetch an album', function () { const album = this.albums[0] cy.request({ url: buildApiUrl(`/albums/${album.id}`), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('containSubset', { // chai-subset plugin id: album.id, title: album.title, artists: album.artists, rating: 'NOT_RATED', userReviews: [], }) })

Slide 114

Slide 114 text

it('can fetch an album', function () { const album = this.albums[0] cy.request({ url: buildApiUrl(`/albums/${album.id}`), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('containSubset', { id: album.id, title: album.title, artists: album.artists, rating: 'NOT_RATED', userReviews: [], }) })

Slide 115

Slide 115 text

it('can fetch an album', function () { const album = this.albums[0] cy.request({ url: buildApiUrl(`/albums/${album.id}`), headers: { 'Content-Type': 'application/json', }, auth: { bearer: this.token }, }) .its('body') .should('containSubset', { id: album.id, title: album.title, artists: album.artists, rating: 'NOT_RATED', userReviews: [], }) })

Slide 116

Slide 116 text

Cypress.Commands.add('apiRequest', ({ body, url, ...options }) => { const token = localStorage.getItem('jwt') return cy.request({ ...options, failOnStatusCode: false, url: buildApiUrl(url), headers: { 'Content-Type': 'application/json', }, auth: { bearer: token }, ...(body && { body: JSON.stringify(body) }), }) })

Slide 117

Slide 117 text

Cypress.Commands.add('apiRequest', ({ body, url, ...options }) => { const token = localStorage.getItem('jwt') return cy.request({ ...options, failOnStatusCode: false, url: buildApiUrl(url), headers: { 'Content-Type': 'application/json', }, auth: { bearer: token }, ...(body && { body: JSON.stringify(body) }), }) })

Slide 118

Slide 118 text

Cypress.Commands.add('apiRequest', ({ body, url, ...options }) => { const token = localStorage.getItem('jwt') return cy.request({ ...options, failOnStatusCode: false, url: buildApiUrl(url), headers: { 'Content-Type': 'application/json', }, auth: { bearer: token }, ...(body && { body: JSON.stringify(body) }), }) })

Slide 119

Slide 119 text

Cypress.Commands.add('apiRequest', ({ body, url, ...options }) => { const token = localStorage.getItem('jwt') return cy.request({ ...options, failOnStatusCode: false, url: buildApiUrl(url), headers: { 'Content-Type': 'application/json', }, auth: { bearer: token }, ...(body && { body: JSON.stringify(body) }), }) })

Slide 120

Slide 120 text

Cypress.Commands.add('apiRequest', ({ body, url, ...options }) => { const token = localStorage.getItem('jwt') return cy.request({ ...options, failOnStatusCode: false, url: buildApiUrl(url), headers: { 'Content-Type': 'application/json', }, auth: { bearer: token }, ...(body && { body: JSON.stringify(body) }), }) })

Slide 121

Slide 121 text

Cypress.Commands.add('apiRequest', ({ body, url, ...options }) => { const token = localStorage.getItem('jwt') return cy.request({ ...options, failOnStatusCode: false, url: buildApiUrl(url), headers: { 'Content-Type': 'application/json', }, auth: { bearer: token }, ...(body && { body: JSON.stringify(body) }), }) })

Slide 122

Slide 122 text

Cypress.Commands.add('apiRequest', ({ body, url, ...options }) => { const token = localStorage.getItem('jwt') return cy.request({ ...options, failOnStatusCode: false, url: buildApiUrl(url), headers: { 'Content-Type': 'application/json', }, auth: { bearer: token }, ...(body && { body: JSON.stringify(body) }), }) })

Slide 123

Slide 123 text

Cypress.Commands.add('apiRequest', ({ body, url, ...options }) => { const token = localStorage.getItem('jwt') return cy.request({ ...options, failOnStatusCode: false, url: buildApiUrl(url), headers: { 'Content-Type': 'application/json', }, auth: { bearer: token }, ...(body && { body: JSON.stringify(body) }), }) })

Slide 124

Slide 124 text

Cypress.Commands.add('apiRequest', ({ body, url, ...options }) => { const token = localStorage.getItem('jwt') return cy.request({ ...options, failOnStatusCode: false, url: buildApiUrl(url), headers: { 'Content-Type': 'application/json', }, auth: { bearer: token }, ...(body && { body: JSON.stringify(body) }), }) })

Slide 125

Slide 125 text

Cypress.Commands.add('apiRequest', ({ body, url, ...options }) => { const token = localStorage.getItem('jwt') return cy.request({ ...options, failOnStatusCode: false, url: buildApiUrl(url), headers: { 'Content-Type': 'application/json', }, auth: { bearer: token }, ...(body && { body: JSON.stringify(body) }), }) })

Slide 126

Slide 126 text

Cypress.Commands.add('apiGet', (url) => cy.apiRequest({ url, method: 'GET' }) ) Cypress.Commands.add('apiPost', (url, body = null) => cy.apiRequest({ body, url, method: 'POST' }) )

Slide 127

Slide 127 text

Cypress.Commands.add('apiGet', (url) => cy.apiRequest({ url, method: 'GET' }) ) Cypress.Commands.add('apiPost', (url, body = null) => cy.apiRequest({ body, url, method: 'POST' }) )

Slide 128

Slide 128 text

Cypress.Commands.add('apiGet', (url) => cy.apiRequest({ url, method: 'GET' }) ) Cypress.Commands.add('apiPost', (url, body = null) => cy.apiRequest({ body, url, method: 'POST' }) )

Slide 129

Slide 129 text

it('can fetch all albums', function () { cy.apiGet('/albums') .its('body') .should('have.length', this.albums.length) .each((album) => { expect(this.albumTitles).to.include(album.title) }) })

Slide 130

Slide 130 text

it('can review an album and remove the review', function () { const album = this.albums[0] const review = 'Great album!' const expectedReview = { review, user: { name: 'Emmett Brown', email: '[email protected]' }, } cy.apiPost(`/albums/${album.id}/review`, { review }) .its('body') .should('deep.equal', expectedReview) cy.apiGet(`/albums/${album.id}`) .its('body.userReviews') .should('deep.equal', [expectedReview]) cy.apiPost(`/albums/${album.id}/remove-review`) .its('body') .should('deep.equal', { status: 'Removed review' }) cy.apiGet(`/albums/${album.id}`) .its('body.userReviews') .should('deep.equal', []) })

Slide 131

Slide 131 text

it('can review an album and remove the review', function () { const album = this.albums[0] const review = 'Great album!' const expectedReview = { review, user: { name: 'Emmett Brown', email: '[email protected]' }, } cy.apiPost(`/albums/${album.id}/review`, { review }) .its('body') .should('deep.equal', expectedReview) cy.apiGet(`/albums/${album.id}`) .its('body.userReviews') .should('deep.equal', [expectedReview]) cy.apiPost(`/albums/${album.id}/remove-review`) .its('body') .should('deep.equal', { status: 'Removed review' }) cy.apiGet(`/albums/${album.id}`) .its('body.userReviews') .should('deep.equal', []) })

Slide 132

Slide 132 text

it('can review an album and remove the review', function () { const album = this.albums[0] const review = 'Great album!' const expectedReview = { review, user: { name: 'Emmett Brown', email: '[email protected]' }, } cy.apiPost(`/albums/${album.id}/review`, { review }) .its('body') .should('deep.equal', expectedReview) cy.apiGet(`/albums/${album.id}`) .its('body.userReviews') .should('deep.equal', [expectedReview]) cy.apiPost(`/albums/${album.id}/remove-review`) .its('body') .should('deep.equal', { status: 'Removed review' }) cy.apiGet(`/albums/${album.id}`) .its('body.userReviews') .should('deep.equal', []) })

Slide 133

Slide 133 text

it('can review an album and remove the review', function () { const album = this.albums[0] const review = 'Great album!' const expectedReview = { review, user: { name: 'Emmett Brown', email: '[email protected]' }, } cy.apiPost(`/albums/${album.id}/review`, { review }) .its('body') .should('deep.equal', expectedReview) cy.apiGet(`/albums/${album.id}`) .its('body.userReviews') .should('deep.equal', [expectedReview]) cy.apiPost(`/albums/${album.id}/remove-review`) .its('body') .should('deep.equal', { status: 'Removed review' }) cy.apiGet(`/albums/${album.id}`) .its('body.userReviews') .should('deep.equal', []) })

Slide 134

Slide 134 text

it('can review an album and remove the review', function () { const album = this.albums[0] const review = 'Great album!' const expectedReview = { review, user: { name: 'Emmett Brown', email: '[email protected]' }, } cy.apiPost(`/albums/${album.id}/review`, { review }) .its('body') .should('deep.equal', expectedReview) cy.apiGet(`/albums/${album.id}`) .its('body.userReviews') .should('deep.equal', [expectedReview]) cy.apiPost(`/albums/${album.id}/remove-review`) .its('body') .should('deep.equal', { status: 'Removed review' }) cy.apiGet(`/albums/${album.id}`) .its('body.userReviews') .should('deep.equal', []) })

Slide 135

Slide 135 text

GraphQL

Slide 136

Slide 136 text

Cypress.Commands.add('graphQL', (query, variables = {}) => cy.apiPost('/graphql', { query, variables }) )

Slide 137

Slide 137 text

Cypress.Commands.add('graphQL', (query, variables = {}) => cy.apiPost('/graphql', { query, variables }) )

Slide 138

Slide 138 text

Cypress.Commands.add('graphQL', (query, variables = {}) => cy.apiPost('/graphql', { query, variables }) )

Slide 139

Slide 139 text

it('can fetch an album', function () { const album = this.albums[0] const query = ` query Album($id: ID!) { album(id: $id) { title artists rating userReviews { review } } } ` cy.graphQL(query, { id: album.id }) .its('body.data.album') .should('deep.equal', { title: album.title, artists: album.artists, rating: 'NOT_RATED', userReviews: [], }) })

Slide 140

Slide 140 text

it('can fetch an album', function () { const album = this.albums[0] const query = ` query Album($id: ID!) { album(id: $id) { title artists rating userReviews { review } } } ` cy.graphQL(query, { id: album.id }) .its('body.data.album') .should('deep.equal', { title: album.title, artists: album.artists, rating: 'NOT_RATED', userReviews: [], }) })

Slide 141

Slide 141 text

it('can fetch an album', function () { const album = this.albums[0] const query = ` query Album($id: ID!) { album(id: $id) { title artists rating userReviews { review } } } ` cy.graphQL(query, { id: album.id }) .its('body.data.album') .should('deep.equal', { title: album.title, artists: album.artists, rating: 'NOT_RATED', userReviews: [], }) })

Slide 142

Slide 142 text

it('can fetch an album', function () { const album = this.albums[0] const query = ` query Album($id: ID!) { album(id: $id) { title artists rating userReviews { review } } } ` cy.graphQL(query, { id: album.id }) .its('body.data.album') .should('deep.equal', { title: album.title, artists: album.artists, rating: 'NOT_RATED', userReviews: [], }) })

Slide 143

Slide 143 text

it('can fetch an album', function () { const album = this.albums[0] const query = ` query Album($id: ID!) { album(id: $id) { title artists rating userReviews { review } } } ` cy.graphQL(query, { id: album.id }) .its('body.data.album') .should('deep.equal', { title: album.title, artists: album.artists, rating: 'NOT_RATED', userReviews: [], }) })

Slide 144

Slide 144 text

Authentication?

Slide 145

Slide 145 text

Cypress.Commands.add('login', (email, password) => { cy.visit('/login') loginPage.email().type(email) loginPage.password().type(password) loginPage.logIn().click() })

Slide 146

Slide 146 text

Cypress.Commands.add('login', (email, password) => { cy.visit('/login') loginPage.email().type(email) loginPage.password().type(password) loginPage.logIn().click() })

Slide 147

Slide 147 text

Cypress.Commands.add( 'login', ( email = Cypress.env('USER_EMAIL'), password = Cypress.env('USER_PASSWORD') ) => cy .request('POST', buildApiUrl('/login'), { email, password }) .its('body.token') .then((token) => { localStorage.setItem('jwt', token) }) )

Slide 148

Slide 148 text

Cypress.Commands.add( 'login', ( email = Cypress.env('USER_EMAIL'), password = Cypress.env('USER_PASSWORD') ) => cy .request('POST', buildApiUrl('/login'), { email, password }) .its('body.token') .then((token) => { localStorage.setItem('jwt', token) }) )

Slide 149

Slide 149 text

Cypress.Commands.add( 'login', ( email = Cypress.env('USER_EMAIL'), password = Cypress.env('USER_PASSWORD') ) => cy .request('POST', buildApiUrl('/login'), { email, password }) .its('body.token') .then((token) => { localStorage.setItem('jwt', token) }) )

Slide 150

Slide 150 text

Cypress.Commands.add( 'login', ( email = Cypress.env('USER_EMAIL'), password = Cypress.env('USER_PASSWORD') ) => cy .request('POST', buildApiUrl('/login'), { email, password }) .its('body.token') .then((token) => { localStorage.setItem('jwt', token) }) )

Slide 151

Slide 151 text

Cypress.Commands.add( 'login', ( email = Cypress.env('USER_EMAIL'), password = Cypress.env('USER_PASSWORD') ) => cy .request('POST', buildApiUrl('/login'), { email, password }) .its('body.token') .then((token) => { localStorage.setItem('jwt', token) }) )

Slide 152

Slide 152 text

beforeEach(function () { cy.login() })

Slide 153

Slide 153 text

Selector Syndrome

Slide 154

Slide 154 text

Selector Syndrome data-test Attributes and Page Objects

Slide 155

Slide 155 text

Repetitive Code

Slide 156

Slide 156 text

Repetitive Code Custom Commands

Slide 157

Slide 157 text

Timeouts/Arbitrary Waiting

Slide 158

Slide 158 text

Timeouts/Arbitrary Waiting Routes and Aliases

Slide 159

Slide 159 text

Untested APIs

Slide 160

Slide 160 text

Untested APIs cy.request and Commands

Slide 161

Slide 161 text

Authenticating via UI

Slide 162

Slide 162 text

Authenticating via UI API, localStorage/cookies, commands, and beforeEach

Slide 163

Slide 163 text

Thanks! Thanks! Jeremy Fairbank @elpapapollo Slides: bit.ly/ct-act Repo: bit.ly/act-repo