首页 > 解决方案 > 在 CucumberJs 和 Cypress 中是否可以使上下文独立并允许具有相同描述的步骤?

问题描述

我有一个基于 typescript 的 Cypress 和 CucumberJs 设置,用于我正在处理的项目中的端到端测试。

它恰好有两个不同的功能文件bus.featurecar.feature以及它们的步骤定义文件bus.spec.tscar.spec.ts

我有两个不同的步骤定义:

Then(
  'I {string} the Destination page', (operation: operation) => {
    cy.location().should(location => {
      switch (operation) {
        case 'visit':
          expect(location.pathname).to.eq('/e2e/destination')
          break

        case `can't visit`:
          expect(location.pathname).to.eq('/e2e/bus')
          break
      }
    })
  }
)

Then(
  'I {string} the Destination page', (operation: operation) => {
    cy.location().should(location => {
      switch (operation) {
        case 'visit':
          expect(location.pathname).to.eq('/e2e/destination')
          break

        case `can't visit`:
          expect(location.pathname).to.eq('/e2e/car')
          break
      }
    })
  }
)

它们的识别字符串相同,'I {string} the Destination page'但在实现上略有不同(例如 case can't visit)。

当我运行测试时,bus它完全完美地执行。

一个有一个问题,car因为两个测试的识别字符串相同,Cypress+CucumberJs 套件只检测第一个bus定义,忽略car和正确的定义。

我明白为什么,第一个被检测到,就是这样。问题是,有没有办法分离不同文件的上下文,所以能够有相同的定义名称和不同的实现?

提前致谢

标签: cucumbercypressdefinitioncucumberjscypress-cucumber-preprocessor

解决方案


为什么不

'I {string} the Destination page for {string}', (operation: operation, transportMode) => {
  ...
  expect(location.pathname).to.eq(`/e2e/car${transportMode}`)

或者这会破坏特征和步骤之间的匹配吗?


推荐阅读