赛普拉斯黄瓜-如何使我的步骤按顺序运行?

时间:2018-08-23 16:50:48

标签: javascript node.js cucumber cucumberjs cypress

此处是Javascript /赛普拉斯的相对新手。我正在使用Cypress Cucumber.js插件运行一些测试。问题是我无法使我的步骤按顺序运行-由于js的异步特性,因此'Then'步骤必须在'Given等之前运行。显然,这将成为问题,因为测试将失败!

我的问题:

1)如何使用异步代码使黄瓜步骤始终按顺序运行?我在这里看到了一个类似的问题:How to wait for a JavaScript Promise to resolve before resuming function?,并且基于对我的Given块应用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"

步骤:

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

当前,这注销了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');
      });
    });
  });

但是可惜,没有运气... Given仍然记录在最后。

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

1 个答案:

答案 0 :(得分:0)

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

相关问题