Cypress Cucumber - 如何让我的步骤按顺序运行?

KZN*_*ana 8 javascript cucumber node.js cucumberjs cypress

这里是 Javascript/Cypress 的相对新手。我正在使用 Cypress Cucumber.js 插件运行一些测试。问题是我无法让我的步骤按顺序运行 - 由于 js 的异步性质,“Then”步骤在“Given”等之前运行。显然,这会成为一个问题,因为测试会失败!

我的问题:

1)我们如何使用异步代码使黄瓜步骤始终按顺序运行?我在这里看到了类似的问题:How to wait for a JavaScript Promise to resolve before resuming function? ,并根据我对给定块应用 async/await 的响应,希望它会在我的步骤中强制执行命令,但这不起作用。

这是我的功能文件:

Given I get the data from CMS
Then I can verify that the title is the same as the CMA title in tab "0"
And I can verify that the link is the correct link in tab "0"
Run Code Online (Sandbox Code Playgroud)

脚步:

  Given('I get the data from CMS', async() => {

    let api = await Api.buildDevApi();
    expectedNav = await new NavExpected(api);
    console.log('1');
  });

  Then('I can verify that the title is the same as the CMA title in tab {string}', (index) => {

    cy.root().find(".primary-item").eq(index).children().first(".nav-link").as('nav');
    cy.get('@nav').should(async(text) => {
      let title = await expectedNav.expectedTitle(index);
      expect(text.get(0).innerText).to.eq(title);
    })
    console.log('2');
  });

  Then('I can verify that the link is the correct link in tab {string}', (index) => {

    cy.get('@nav').should(async(url) => {
      let link = await expectedNav.expectedLink(index);
      expect(url.attr('href')).to.eq(link);
    })
    console.log('3');
  });
Run Code Online (Sandbox Code Playgroud)

目前,这会输出 2,3,1。

经过一番搜索后,我尝试使用 Cypress.Promise 强制执行某些命令:

  Given('I get the data from Prismic T1', () => {
    return new Cypress.Promise((resolve) => {
      PrismicApi.buildDevApi().then(api => {
        expectedNav = new NavExpected(api);
        resolve();
        console.log('1');
      });
    });
  });
Run Code Online (Sandbox Code Playgroud)

但可惜的是,运气不佳……Given 仍然排在最后。

任何帮助将不胜感激,我很乐意提供进一步的说明。:)

小智 0

我将 Cypress 与 Typescript 和 Cucumber 预处理器一起使用,并且功能文件中的所有步骤同步运行,请在此处检查版本和示例:Test Repo Cypress-Cucumber-Typescript