7

我正在尝试使用 TypScript 编写 Cucumber 测试,如下所示:

import { browser, $$ } from 'protractor';
import { Given, Then } from 'cucumber'
import { expect } from 'chai';

Given('I navigate to the homepage', function (callback) {
  browser.get('http://localhost:4200');
  callback();
});

Then('I want to see the welcome message {string}', function (message, callback) {
  expect($$('h1').first().getText()).to.eventually.equal(message).and.notify(callback);
});

然而,量角器抱怨:

错误:无效的 Chai 属性:最终

我怎样才能导入这个?我试过了:

import { eventual } from 'chai-as-promised';

但这不起作用。我怎样才能做到这一点?我也尝试Then使用 重写调用await,但编译器抱怨您不能将回调与异步函数混合使用。啊!

4

1 回答 1

9

onPrepare在量角器配置中,在函数末尾添加以下行:

onPrepare: function() {
 ...
 // Load chai assertions
 const chai = require('chai');
 const chaiAsPromised = require('chai-as-promised');

 // Load chai-as-promised support
 chai.use(chaiAsPromised);

 // Initialise should API (attaches as a property on Object)
 chai.should();
}

使用异步函数时,您应该从函数签名中删除回调。

Then('I want to see the welcome message {string}',
async function (message) {
  await chai.expect($$('h1').first().getText())
.to.eventually.equal(message);
});
于 2018-10-03T12:25:43.647 回答