We use Cypress for our end-to-end tests. This page references our custom commands and the best practices that we try to follow.
We should try to stick with https://docs.cypress.io/guides/references/best-practices.html as much as possible. In short, we should currently enforce:
Don’t target elements based on CSS attributes such as: id, class, tag Add data-* attributes to make it easy to target elements
Tests should not rely on each others results and should be repeatable (we must be able to run it multiple times consecutively). You can use commands like cy.signup()
to ensure that you start from a fresh context.
To improve the testing experience and the readability of our tests, we have defined a set of custom commands in cypress/support/commands.js
.
Both the cy.login
and cy.signup
commands accept a redirect parameter. You can use it to be redirected to a specific page as soon as the command succeed.
cy.login({email, redirect} = params)
Login with an existing account. If not provided in params
, the email used for authentication will be testuser+admin@opencollective.com
.
Note that addresses formatted like test*@opencollective.com
are a special case that let you login directly without the need to confirm your email. You can use the randomEmail
helper to generate these.
cy.signup({user, redirect} = params)
Create an account and login with it. If no params is provided, the account will be created with a random email.
cy.createCollective({ type
= 'ORGANIZATION', email =
defaultTestUserEmail})
Helper to quickly create a collective that use designated by email
will be an admin of.
cy.addCreditCardToCollective({ collectiveSlug })
Adds a default test credit card to the collective referenced by collectiveSlug
cy.fillStripeInput(container, cardParams)
Fills a stripe input.
container
(optional) the DOM that contains the input
cardParams
(optional) the credit card info. Defaults to a valid card. Keys:
creditCardNumber
expirationDate
cvcCode
postalCode
cy.openEmail(filterFunc)
This function is used to open an email in Cypress. If the command succeed, a page with the email is loaded and you'll be able to run all the usual cypress commands (cy.get
, cy.contains
...) to test it.
fiterFunc
is a function used to filter the list of emails. As soon as it returns true
, command will start to open the email. For a complete list of the fields you can use to filter the emails, see https://github.com/djfarrelly/MailDev/blob/master/docs/rest.md
Examples
cy.getInbox()
Return the full inbox as a list of email objects. cy.openEmail
should be privileged, but this one can be useful if you need to do more advanced verification like counting the number of emails or who the email was sent to.
cy.clearInbox()
Clears the inbox. It is a good practice to run it in before
to ensure that your test cannot be influenced by the emails sent in previous tests.