首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cypress夹具- TypeError:无法读取未定义的属性“travelData”

Cypress夹具- TypeError:无法读取未定义的属性“travelData”
EN

Stack Overflow用户
提问于 2021-03-06 15:26:34
回答 2查看 2.6K关注 0票数 1

Cypress test execution with fixture - "TypeError: Cannot read property 'travelData' of undefined"中的误差获取

以下是几个步骤:

1-创建一个travelData.json文件并将其放入fixtures文件夹中:

代码语言:javascript
复制
{
  "FIRSTNAME": "TESTTRAVEL",
  "LASTNAME": "USER",
  "MOBILE": "581234567",
  "EMAIL": "test@test.com",
  "PASSPORT": "AB1234",
  "CURRENCY": "AED"
}

2-编写一个Cypress测试并放入integration文件夹。代码如下:

代码语言:javascript
复制
context('Travel Portal Regression Test', () => {
  beforeEach(function () {
    cy.fixture('travel.testData').then((travelData) => {
      this.travelData = travelData;
    });
  });
  beforeEach(() => {
    // cy.viewport('iphone-6')
    cy.viewport(360, 640);
    cy.visit('https://thoughtcoders.com/');
  });

  it('Testing ThoughtCoders Homepage', () => {
    cy.log('Testing ThoughtCoders Homepage');
    cy.log(this.travelData.FIRSTNAME);
  });
});

当我运行Cypress测试时,我得到错误"TypeError:无法读取未定义的“的属性'travelData‘。

请纠正我。

EN

回答 2

Stack Overflow用户

发布于 2021-03-06 15:37:01

解决方案1:

如果wrap()方法的解析方式如下所示,则可以使用该方法生成作为param传递的object

代码语言:javascript
复制
context('Travel Portal Regression Test', () => {
  beforeEach(() => {
    cy.fixture('travelData').then((data) => cy.wrap(data).as('getTravelData'));
    // cy.viewport('iphone-6')
    cy.viewport(360, 640);
    cy.visit('https://thoughtcoders.com/');
  });

  it('Testing ThoughtCoders Homepage', () => {
    cy.log('Testing ThoughtCoders Homepage');
    cy.get('@getTravelData').then((res) => cy.log(res.FIRSTNAME));
  });
});

解决方案2:

您可以在测试规范的上限上声明一个名为travelData的变量&将其值重新分配到从json文件接收到的data,如下所示:

代码语言:javascript
复制
context('Travel Portal Regression Test', () => {
  let travelData;

  beforeEach(() => {
    cy.fixture('travelData').then((data) => {
      travelData = data;
      return travelData;
    });
    // cy.viewport('iphone-6')
    cy.viewport(360, 640);
    cy.visit('https://thoughtcoders.com/');
  });

  it('Testing ThoughtCoders Homepage', () => {
    cy.log('Testing ThoughtCoders Homepage');
    cy.log(travelData.FIRSTNAME);
  });
});

这是带有测试结果的截图:

票数 2
EN

Stack Overflow用户

发布于 2021-03-06 19:35:48

看看夹具示例配方,它展示了一种简单的方法,只加载一次,然后在规范中的每个测试中持久化

模式是在您的beforeEach()和当前beforeEach()之间添加一个额外的beforeEach()

示例配方

代码语言:javascript
复制
context('loading once and using @', () => {
  let city
  let country

  before(() => {
    // load fixtures just once, need to store in
    // closure variables because Mocha context is cleared
    // before each test
    cy.fixture('city').then((c) => {
      city = c
    })

    cy.fixture('country').then((c) => {
      country = c
    })
  })

  beforeEach(() => {
    // we can put data back into the empty Mocha context before each test
    // by the time this callback executes, "before" hook has finished
    cy.wrap(city).as('city')
    cy.wrap(country).as('country')
  })

  it('has loaded fixtures', function () {
    // again, the test has to use "function" callback
    // to make sure "this" points at the Mocha context
    expect(this.city).to.deep.equal({ name: 'Atlanta' })
    expect(this.country).to.deep.equal({ name: 'United States' })
  })
})

你需要的三个要点是

  1. 使用闭包变量
  2. 清除后使用beforeEach()将变量“放回”
  3. 使用"function () {}“样式测试

在您的测试中

代码语言:javascript
复制
context('loading once and persisting the fixture', () => {

  let travelData                                                 // key point #1

  before(() => {
    cy.fixture('travelData').then((data) => {                  
      travelData = data;
    });
  })

  beforeEach(() => {                                             // key point #2
    cy.wrap(travelData).as('travelData')
  })

  beforeEach(() => {
    cy.viewport(360, 640);
    cy.visit('https://thoughtcoders.com/');
  });

  it('Testing ThoughtCoders Homepage', function () {             // key point #3 
    expect(this.travelData."FIRSTNAME").to.equal("TESTTRAVEL")   // passes
  })
})
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66507331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档