如何在Cypress中使用不同的夹具运行相同的测试?

如何在Cypress中使用不同的夹具运行相同的测试?,cypress,Cypress,我试图对显示在两个单独页面上的组件运行完全相同的Cypress测试。为了实现这一点,我想我应该使用forEach语句 使相同的代码在每个“状态”下运行(参见下面的代码) 问题是before语句中的代码块开始运行状态#2,before状态#1的测试已经完成。这导致状态#1的测试失败(因为它有状态#2装置) 如何使状态#2的部分之前的等待状态#1中的所有测试完成 const states = [ { "startPath": "/path1", "fixture": "fix

我试图对显示在两个单独页面上的组件运行完全相同的Cypress测试。为了实现这一点,我想我应该使用
forEach
语句 使相同的代码在每个“状态”下运行(参见下面的代码)

问题是
before
语句中的代码块开始运行状态#2,before状态#1的测试已经完成。这导致状态#1的测试失败(因为它有状态#2装置)

如何使状态#2的部分之前的等待状态#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() {

      })
    })
  })
})

我可以建议一个稍微不同的方法吗?您可以将测试放在自定义命令中,并在测试中重用它。您将得到以下结果:

const states=[
{ 
“起始路径”:“/path1”,
“固定装置”:“固定装置1”
},
{ 
“起始路径”:“/path2”,
“固定装置”:“固定装置2”
}
]
add('testcase',function()){
//在两种情况下都要执行的步骤
})
描述('启动测试',功能(){
states.forEach((state)=>{
它('第一个场景',函数(){
cy.flushDB()
cy.fixture(state.fixture)
。然后(fixtureData=>{
新柏树。承诺((决心,拒绝)=>
cy.createCollections(fixtureData、resolve、reject)
)
})
.然后(()=>cy.visit(state.startPath))
cy.testcase()
})
})
})

找到了一个解决方案:我不得不将它包装到另一个
描述
代码块中,然后它工作了:

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() {

        })
      })
    })
  })
})

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

集成/示例_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
        });
    });
});
fixtures/examples.json

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

请参阅:

在具有多个数据的单一装置JSON中非常适合我,而不是存根API

描述('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)      
}) 
});

这确实解决了问题,但使用嵌套测试(it语句中的it语句)会导致其他不必要的复杂性。我提出了一个解决方案。你的解决方案是什么?你可以发布它,除了你自己的答案。这对社区非常有用