【问题标题】:How do I run a beforeEach in Cypress?如何在 Cypress 中运行 beforeEach?
【发布时间】:2021-07-08 00:27:33
【问题描述】:

我目前正在构建一个测试,似乎在第二次和第三次测试之后,beforeEach 没有得到我需要的信息,并告诉我我遇到了以下问题:

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

我的文件如下:


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)
        

    })
    
  })

我的问题是在扩展业务测试之后开始的。我不知道是否必须在 beforeEach 语句下传递任何其他函数,或者是否需要进行不同的设置。

我应该如何使用 beforeEach?该文件以前可以工作,但由于某种原因开始失败。可以请教吗?

【问题讨论】:

    标签: javascript testing automation cypress qa


    【解决方案1】:

    错误信息cy.type() 只能接受字符串或数字。你传入:undefined 表示路径的最后一部分是undefined,例如 this.data.fullName 或 this.info.userGoodCredit.company

    如果this.data 未定义,您的错误将是cannot get property "fullName" of undefined,所以您的 beforeEach() 可能没问题。

    测试中使用的属性或夹具对象中似乎存在拼写错误。检查console.log(this.info.userGoodCredit)console.log(this.data)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      • 2018-12-25
      相关资源
      最近更新 更多