5
votes

I'm creating some integration tests in cypress.

I have currently the registration process down. - this is one file test.

registration.spec.js

Now, I want to run a login.spec.js test. How do I use the dyanmic email that was created in registration.spec.js and have that email now be used in "login.spec.js"?

registration.spec.js
----> create a dynamic email address
----> do some tests
----> end registration test
login.spec.js
---> login with email/pw I created in the registration.spec.js file?

How is this achieved. Is there an injectGlobal type of deal in cypress?

Also, If I run login.spec.js as a standalone, is there a way to use the "supplied" credentials OR ones that are saved via the Global parameters from running all tests? Does that make sense?

I want to run all tests...

registration.spec.js (create dynamic email/pw)
login.spec.js (use dynamic values from registration process)

or 

login.spec.js (standalone, use supplied credentials)
1
You might try saving to local storage from within a then command: .then(email => {window.localStorage.setItem('registration-email', email) })user8745435
Thanks for your response, but that seems such a heavy handed way. Like setting cookies.. I guess I could do either. I was hoping cypress had something that allowed this. Thanks bud.james emanon
Indeed, some effort required. I can see the other side though, Cypress wants to ensure no pollution between tests.user8745435

1 Answers

3
votes

Yes, Cypress supports the ability to create and reuse actions and state in your UI, such as registering and logging in before a test.

However, Cypress, through cy.request() allows you to control the state of the browser more powerfully than a user would, making your tests simpler, faster, more reliable

Check out this example below where cy.request is used to create/read state on your server.

In commands/index.js:

Cypress.Commands.add('login', (user) => {
    cy.request('POST', `${apiUrl}/users/login`, user)
})

Cypress.Commands.add("register", (user) => {
    cy.request('POST', `${apiUrl}/users/register`, user)
})

Cypress.Commands.add('getUser', (username) => {
    return cy.request('GET', `${apiUrl}/users/${username}`)
})

in register.spec.js:

it ('can register', () => {
    const user = {
      name: 'jake',
      email: '[email protected]',
      password: '12345'
    }

    cy.visit('/register')
    cy.get('input[name="name"]').type(user.name)
    cy.get('input[name="email"]').type(user.email)
    cy.get('input[name="password"]').type(user.password)
    cy.get('input[name="password-confirm"]').type(user.password)
    cy.get('input[type="submit"]').click()

    // ensure register page sends you /home after register
    cy.url().should('contain', '/home')

    // expect user from server to match user from test
    cy.getUser(user.name)
    .then((dbUser) => expect(dbUser).to.deep.eql(user))
})

in login.spec.js:

it('can log in', () => {
    const user = {
        name: 'jane',
        email: '[email protected]',
        password: '12345'
    }

    // register w/out UI
    cy.register(user)

    cy.visit('/login')
    cy.get('input[name="name"]').type(user.name)
    cy.get('input[name="password"]').type(user.password)
    cy.get('input[type="submit"]').click()

    // ensure the login page sends you home after login
    cy.url().should('contain', '/home')
})

in userSettings.spec.js:

it('can change email', () => {
    const user = {
        name: 'jane',
        email: '[email protected]',
        password: '12345'
    }

    // register and login w/o UI
    cy.register(user)
    cy.login(user)

    cy.visit('/settings')
    cy.get('input[name="email"]').type('[email protected]')
    cy.get('input[type="submit"]').click()

    cy.getUser(user.name)
    .then((dbUser) => expect(dbUser.email).to.eql('[email protected]'))
})