Skip to content
Advertisement

How do I run a beforeEach in Cypress?

I’m currently building a test, and it seems that after the second and third test, the beforeEach is not getting the information that I need and is telling me that I’m having the following issue:

cy.type() can only accept a string or number. You passed in: undefinedLearn more

My file is the following:

describe('Test all', function () {

    beforeEach(() => {
        cy.fixture('onlineApp.json').then(function (data) {
            this.data = data
        })

        cy.fixture('onlineAppUsers.json').then(function (info) {
            this.info = info
        })

    })


    // Getting into the site to start testing

    it('Build remodel ', function () {

        // Get into the producs page
        Cypress.config().baseUrl
        cy.visit(Cypress.env('url_products'))

        cy.buildRemodel()
        cy.onlineappCreateProfileForm()
        cy.businessInfo()
        cy.logOut()

    })

    it('Buy a Business', function () {

        // Get into the login page
        Cypress.config().baseUrl
        cy.visit(Cypress.env('url_login'))

        // Login | Use the information from the Fixture file created at the time that we create an account

        cy.get('#email').type(this.data.email)
        cy.get('#password').type('Abcd1234')
        cy.get('.app-submit-btn-text').should('be.visible').click()

        // Start a new Application

        cy.get(':nth-child(2) > .small-text > a').should('be.visible').click()

        // Start Loan Application

        cy.buyBusiness()

        // Fill out form

        cy.get('#full_name').type(this.info.userGoodCredit.name)
        cy.get('#company').type(this.info.userGoodCredit.company)
        cy.get('#phone_number').type(this.info.userGoodCredit.phone)
        cy.get('#email')
        cy.wait(10000)
        cy.get('.app-submit-btn-text', { delay: 100 }).should('be.visible').click()
        cy.businessInfo()
        cy.logOut()


    })

    it('Expand a Business', function () {
        // Get into the login page
        Cypress.config().baseUrl
        cy.visit(Cypress.env('url_login'))

        // Login | Use the information from the Fixture file created at the time that we create an account

        cy.get('#email').type(this.data.email)
        cy.get('#password').type('Abcd1234')
        cy.get('.app-submit-btn-text', { time: 100000 }).should('be.visible').click()

        // Start a new Application

        cy.get(':nth-child(2) > .small-text > a').should('be.visible').click()

        // Start Loan Application

        cy.expandBusiness()

        // Fill out form

        cy.get('#full_name').type(this.data.fullName)
        cy.get('#company').type(this.info.userGoodCredit.company)
        cy.get('#phone_number').type(this.info.userGoodCredit.phone)
        

    })
    
  })

My issue starts after the Expand a Business test. I don’t know if I have to pass any other function under the beforeEach statement or if needs to be set up differently.

How should I use the beforeEach? This file was working before, but for some reason started to fail. Could you please advise?

Advertisement

Answer

The error message cy.type() can only accept a string or number. You passed in: undefined says the last part of the path is undefined, e.g this.data.fullName or this.info.userGoodCredit.company.

If this.data was undefined, you error would be cannot get property “fullName” of undefined, so your beforeEach() is probably ok.

It looks like there’s a typo in the property used in the test, or in the fixture object. Check with console.log(this.info.userGoodCredit) and console.log(this.data).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement