微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从夹具错误中提取数据:无法读取未定义的属性“键”

如何解决从夹具错误中提取数据:无法读取未定义的属性“键”

当我尝试将图片中的数据提取到我的测试用例中时,我得到了未定义的无法读取属性 'key' 错误信息。

代码: describe('我的第一次测试',() => {

beforeEach(function() {
    cy.fixture("DataFile").then((data) => {
      this.key = data
      
    })
  })

it('Does not do much!',() => {
    cy.visit('https://rahulshettyacademy.com/angularpractice/')
    cy.get("form input[name='name']").type(this.key.Name)
    cy.get("form input[name='email']").type(this.key.Email)
   cy.get("#exampleInputPassword1").type(this.login.key.Password)
   cy.get('select').select(this.key.Gender)

})

错误: 无法读取未定义的属性“key”

enter image description here

解决方法

要访问 this 的属性,请使用函数而不是箭头函数

it('Does not do much!',function() {              // function here gets correct 'this'
  cy.visit('https://rahulshettyacademy.com/angularpractice/')
  cy.get("form input[name='name']").type(this.key.Name)
  cy.get("form input[name='email']").type(this.key.Email)
  cy.get("#exampleInputPassword1").type(this.login.key.Password)
  cy.get('select').select(this.key.Gender)
})

或者您可以将夹具移近测试(不太理想,但总体上相差不大)

it('Does not do much!',() => {
  cy.fixture("DataFile").then((data) => {
    cy.visit('https://rahulshettyacademy.com/angularpractice/')
    cy.get("form input[name='name']").type(data.key.Name)
    cy.get("form input[name='email']").type(data.key.Email)
    cy.get("#exampleInputPassword1").type(data.login.key.Password)
    cy.get('select').select(data.key.Gender)
  })     
})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。