💚 Add HTTP interception Cypress commands & intercept demande-mobilité

pull/1713/head
Alexandre Hajjar 2021-09-07 19:23:59 +02:00
parent 78e4eb6bc6
commit 2e6216b722
4 changed files with 196 additions and 154 deletions

View File

@ -38,7 +38,9 @@ jobs:
install: false
working-directory: mon-entreprise
# List here all the specs that are making external API calls:
spec: cypress/integration/mon-entreprise/english/gérer.js
spec: |
cypress/integration/mon-entreprise/english/gérer.js
cypress/integration/mon-entreprise/demande-mobilité.js
record: true
tag: external-mon-entreprise-e2e
config: integrationFolder=cypress/integration/mon-entreprise,baseUrl=https://mon-entreprise.fr

View File

@ -1,113 +1,131 @@
const fr = Cypress.env('language') === 'fr'
describe(
'Formulaire demande mobilité',
{
retries: {
runMode: 3,
openMode: 0,
},
},
function () {
if (!fr) {
return
}
before(() => cy.visit(encodeURI('/gérer/demande-mobilité')))
it('should not crash', () => {
cy.contains('Demande de mobilité internationale')
})
it('should allow to complete and download', () => {
// "coordonnées" section
cy.contains('SIRET').click()
cy.focused()
.type('684 064 0011')
.tab()
.type('Deaux')
.tab()
.type('Jean')
.tab()
.type('Française')
cy.contains('sécurité sociale').click()
cy.focused().type('1 91 07 468 054 75').tab().type('1991-07-25')
const FIXTURES_FOLDER = 'cypress/fixtures'
const DEMANDE_MOBILITE_FIXTURES_FOLDER = `${FIXTURES_FOLDER}/demande-mobilité`
cy.get(
"input[name='coordonnées assuré . commune de naissance . étranger'][value='non']"
)
.next()
.click()
.wait(500)
const writeFixtures = Cypress.env('record_http') !== undefined
cy.focused().tab().type('Pouts').wait(1500).type('{enter}')
cy.get(
"input[name='coordonnées assuré . domicile personnel . commune . étranger'][value='non']"
)
.next()
.click()
cy.focused()
.tab()
.type('3 rue de la Rhumerie')
.tab()
.type('Brest')
.wait(1500)
.type('{enter}')
.tab()
.type('jean.deaux@gmail.com')
.tab()
.type('06 85 69 78 54')
.tab()
// "activité en France" section
cy.focused()
.type('Deaux & Fils')
.tab()
.type('14 chemin des Docks')
.tab()
.type('Bre')
.wait(1500)
cy.contains('29240').click()
cy.contains('Organisme Urssaf').click()
cy.focused().type('Bretagne').tab().tab().type('Boulangerie')
// "votre demande" section
cy.get("input[name='demande . pays unique'][value='oui']").next().click()
cy.get("input[name='demande . infrastructure sauvegardée'][value='oui']")
.next()
.click()
cy.get("input[name='demande . activité semblable'][value='oui']")
.next()
.click()
cy.get("input[name='demande . date de fin connue'][value='oui']")
.next()
.click()
cy.get('label[for="détachement . pays"]').wait(1500).click()
cy.focused()
.select('Irlande')
.tab()
.type('2020-11-06')
.tab()
.type('2021-04-09')
.tab()
.tab()
.type('Fabrications de gateaux bretons')
cy.get("input[name='détachement . base fixe'][value='non']")
.next()
.click()
cy.get(
"input[name='commentaires additionnels . commentaires'][value='non']"
)
.next()
.click()
// download PDF
cy.contains(
'Je certifie lexactitude des informations communiquées ci-dessus'
).click()
cy.contains('Fait à').click()
cy.focused().type('Plougastel')
cy.contains('Générer la demande').click()
cy.contains('Télécharger le fichier').click()
})
describe(`Formulaire demande mobilité (${
writeFixtures ? 'record mode' : 'stubbed mode'
})`, function () {
if (!fr) {
return
}
)
let pendingRequests = new Set()
let responses = {}
const hostnamesToRecord = ['geo.api.gouv.fr']
beforeEach(() => {
pendingRequests = new Set()
responses = {}
cy.setInterceptResponses(
pendingRequests,
responses,
hostnamesToRecord,
DEMANDE_MOBILITE_FIXTURES_FOLDER
)
cy.visit(encodeURI('/gérer/demande-mobilité'))
})
afterEach(() => {
cy.writeInterceptResponses(
pendingRequests,
responses,
DEMANDE_MOBILITE_FIXTURES_FOLDER
)
})
it('should not crash', () => {
cy.contains('Demande de mobilité internationale')
})
it('should allow to complete and download', () => {
// "coordonnées" section
cy.contains('SIRET').click()
cy.focused()
.type('684 064 0011')
.tab()
.type('Deaux')
.tab()
.type('Jean')
.tab()
.type('Française')
cy.contains('sécurité sociale').click()
cy.focused().type('1 91 07 468 054 75').tab().type('1991-07-25')
cy.get(
"input[name='coordonnées assuré . commune de naissance . étranger'][value='non']"
)
.next()
.click()
.wait(500)
cy.focused().tab().type('Pouts').wait(1500).type('{enter}')
cy.get(
"input[name='coordonnées assuré . domicile personnel . commune . étranger'][value='non']"
)
.next()
.click()
cy.focused()
.tab()
.type('3 rue de la Rhumerie')
.tab()
.type('Brest')
.wait(1500)
.type('{enter}')
.tab()
.type('jean.deaux@gmail.com')
.tab()
.type('06 85 69 78 54')
.tab()
// "activité en France" section
cy.focused()
.type('Deaux & Fils')
.tab()
.type('14 chemin des Docks')
.tab()
.type('Bre')
.wait(1500)
cy.contains('29240').click()
cy.contains('Organisme Urssaf').click()
cy.focused().type('Bretagne').tab().tab().type('Boulangerie')
// "votre demande" section
cy.get("input[name='demande . pays unique'][value='oui']").next().click()
cy.get("input[name='demande . infrastructure sauvegardée'][value='oui']")
.next()
.click()
cy.get("input[name='demande . activité semblable'][value='oui']")
.next()
.click()
cy.get("input[name='demande . date de fin connue'][value='oui']")
.next()
.click()
cy.get('label[for="détachement . pays"]').wait(1500).click()
cy.focused()
.select('Irlande')
.tab()
.type('2020-11-06')
.tab()
.type('2021-04-09')
.tab()
.tab()
.type('Fabrications de gateaux bretons')
cy.get("input[name='détachement . base fixe'][value='non']").next().click()
cy.get(
"input[name='commentaires additionnels . commentaires'][value='non']"
)
.next()
.click()
// download PDF
cy.contains(
'Je certifie lexactitude des informations communiquées ci-dessus'
).click()
cy.contains('Fait à').click()
cy.focused().type('Plougastel')
cy.contains('Générer la demande').click()
cy.contains('Télécharger le fichier').click()
})
})

View File

@ -2,50 +2,8 @@ const fr = Cypress.env('language') === 'fr'
const FIXTURES_FOLDER = 'cypress/fixtures'
const GERER_FIXTURES_FOLDER = `${FIXTURES_FOLDER}/gérer`
const writeFixtures = Cypress.env('record_http') !== undefined
const stubFixtures = !writeFixtures
const setInterceptResponses = (pendingRequests, responses, hostnames) => {
if (writeFixtures) {
cy.intercept('*', (req) => {
if (!hostnames.includes(new URL(req.url).hostname)) return
pendingRequests.add(req.url)
req.on('after:response', (res) => {
pendingRequests.delete(req.url)
responses[res.url] = res.body
})
})
} else if (stubFixtures) {
const urlOfFilepath = (filename) => {
return atob(filename.slice(0, -'.json'.length))
}
cy.exec(`find ${GERER_FIXTURES_FOLDER} -type f`)
.then((result) => {
return result.stdout.split('\n')
})
.then((filepaths) => {
filepaths.forEach((filepath) => {
const shortPath = filepath.slice(FIXTURES_FOLDER.length + 1)
const filename = filepath.slice(GERER_FIXTURES_FOLDER.length + 1)
cy.intercept(urlOfFilepath(filename), { fixture: shortPath })
})
})
}
}
const writeResponses = (pendingRequests, responses) => {
if (writeFixtures) {
// We need to wait on all catched requests to be fulfilled and recorded,
// otherwise the stubbed cy run might error when a request is not stubbed.
// Caveat: we assume request.url to be unique amongst recorded requests.
cy.waitUntil(() => pendingRequests.size === 0)
Object.keys(responses).map((url) => {
if (responses[url] === undefined) return
cy.writeFile(
`${GERER_FIXTURES_FOLDER}/${btoa(url)}.json`,
JSON.stringify(responses[url], null, 2)
)
})
}
}
describe(`Manage page test (${
writeFixtures ? 'record mode' : 'stubbed mode'
@ -56,11 +14,20 @@ describe(`Manage page test (${
beforeEach(() => {
pendingRequests = new Set()
responses = {}
setInterceptResponses(pendingRequests, responses, hostnamesToRecord)
cy.setInterceptResponses(
pendingRequests,
responses,
hostnamesToRecord,
GERER_FIXTURES_FOLDER
)
cy.visit(fr ? encodeURI('/gérer') : '/manage')
})
afterEach(() => {
writeResponses(pendingRequests, responses)
cy.writeInterceptResponses(
pendingRequests,
responses,
GERER_FIXTURES_FOLDER
)
})
it('should not crash', function () {
cy.contains(fr ? 'Gérer mon activité' : 'Manage my business')

View File

@ -29,3 +29,58 @@ Cypress.Commands.add('iframe', { prevSubject: 'element' }, ($iframe) => {
})
})
import 'cypress-wait-until'
Cypress.Commands.add(
'setInterceptResponses',
(pendingRequests, responses, hostnames, specFixturesFolder) => {
const FIXTURES_FOLDER = 'cypress/fixtures'
const writeFixtures = Cypress.env('record_http') !== undefined
const stubFixtures = !writeFixtures
if (writeFixtures) {
cy.intercept('*', (req) => {
if (!hostnames.includes(new URL(req.url).hostname)) return
pendingRequests.add(req.url)
req.on('after:response', (res) => {
pendingRequests.delete(req.url)
responses[res.url] = res.body
})
})
} else if (stubFixtures) {
const urlOfFilepath = (filename) => {
return atob(filename.slice(0, -'.json'.length))
}
cy.exec(`find ${specFixturesFolder} -type f`)
.then((result) => {
return result.stdout.split('\n')
})
.then((filepaths) => {
filepaths.forEach((filepath) => {
const shortPath = filepath.slice(FIXTURES_FOLDER.length + 1)
const filename = filepath.slice(specFixturesFolder.length + 1)
cy.intercept(urlOfFilepath(filename), { fixture: shortPath })
})
})
}
}
)
Cypress.Commands.add(
'writeInterceptResponses',
(pendingRequests, responses, specFixturesFolder) => {
const writeFixtures = Cypress.env('record_http') !== undefined
if (writeFixtures) {
// We need to wait on all catched requests to be fulfilled and recorded,
// otherwise the stubbed cy run might error when a request is not stubbed.
// Caveat: we assume request.url to be unique amongst recorded requests.
cy.waitUntil(() => pendingRequests.size === 0)
Object.keys(responses).map((url) => {
if (responses[url] === undefined) return
cy.writeFile(
`${specFixturesFolder}/${btoa(url)}.json`,
JSON.stringify(responses[url], null, 2)
)
})
}
}
)