如何在赛普拉斯中使用不同的夹具运行相同的测试?

时间:2019-09-19 18:37:34

标签: cypress

我正在尝试对两个单独页面上显示的组件运行完全相同的赛普拉斯测试。为此,我认为我会使用forEach语句 这样,每个“状态”都将运行相同的代码(请参见下面的代码)。

问题是before语句中的代码块开始在状态#2上运行,在对状态#1的测试完成之前。这导致针对状态1的测试失败(因为它具有状态2的灯具)。

如何使状态2的before部分等待状态1的所有测试完成?

const states = [
  { 
    "startPath": "/path1", 
    "fixture": "fixture1"
  },
  { 
    "startPath": "/path2", 
    "fixture": "fixture2"
  }
]

describe('Start test', function() {

  // Loop through both test
  states.forEach((state) => {

    // In this before statement the fixtures are setup
    before(function () {
      cy.flushDB()
      cy.fixture(state.fixture)
        .then(fixtureData => {
          new Cypress.Promise((resolve, reject) => 
            cy.createCollections(fixtureData, resolve, reject)
          )
        })
        .then(() => cy.visit(state.startPath)) 
    })

    context('Within this context', function() {
      it(`Can run some test for fixture ${state.fixture}`, function() {

      })
    })
  })
})

4 个答案:

答案 0 :(得分:1)

找到一个解决方案:我必须将其包装到另一个describe代码块中,然后它才能工作:

const states = [
  { 
    "startPath": "/path1", 
    "fixture": "fixture1",
    "context: "1"
  },
  { 
    "startPath": "/path2", 
    "fixture": "fixture2",
    "context": "2"
  }
]

describe('Start test', function() {

  // Loop through both test
  states.forEach((state) => {
    // SOLUTION HERE
    describe(state.context, () => {

      // In this before statement the fixtures are setup
      before(function () {
        cy.flushDB()
        cy.fixture(state.fixture)
          .then(fixtureData => {
            new Cypress.Promise((resolve, reject) => 
              cy.createCollections(fixtureData, resolve, reject)
            )
          })
          .then(() => cy.visit(state.startPath)) 
      })

      context('Within this context', function() {
        it(`Can run some test for fixture ${state.fixture}`, function() {

        })
      })
    })
  })
})

答案 1 :(得分:0)

我可以建议一种稍微不同的方法吗?您可以将测试放在自定义命令中,然后在测试中重用它。您最终将得到的是:

const states = [
  { 
    "startPath": "/path1", 
    "fixture": "fixture1"
  },
  { 
    "startPath": "/path2", 
    "fixture": "fixture2"
  }
]

Cypress.Commands.add('testcase', function() {
  // steps which you want to perform in both scenarios
})

describe('Start test', function() {
  states.forEach((state) => {
    it('first scenario', function() {
      cy.flushDB()
      cy.fixture(state.fixture)
        .then(fixtureData => {
          new Cypress.Promise((resolve, reject) => 
          cy.createCollections(fixtureData, resolve, reject)
          )
        })
        .then(() => cy.visit(state.startPath))
        cy.testcase()
    })
  })
})

答案 2 :(得分:0)

第三种方法可以使给定灯具的数据集针对一个测试进行循环。灯具中的每个数据集都将注册为自己的测试。

integration / example_spec.js

const examples = require('../../fixtures/examples');
// `examples` contains the full contents of the fixture

describe('Using `require`', function () {
    examples.forEach((example) => {
        it('gets its data from a fixture', function () {
            // Do something with each example
        });
    });
});

灯具/examples.json

[
    {
        "name": "Iteration 1"
    },
    {
        "name": "Iteration 2"
    }
]

请参阅:https://github.com/cypress-io/cypress/issues/3963#issuecomment-483581934

答案 3 :(得分:0)

在具有多个数据而不是存根 API 的单个夹具 JSON 中对我来说完美

describe('Test Single Input Field Form', function()之前声明:

 const testData = require("../../fixtures/multipleInputFields.json")

然后

testData.forEach((data) => {  
const message = data.message 
it('Test Case', function(){      
cy.log("data is:" + data) 
cy.get('#user-message').type(message).should('have.value', message) 
cy.get('#get-input > button').click()  
cy.wait(200) 
cy.get('span#display').should('have.text', message)      
}) 
});