我正在尝试使用TypScript编写黄瓜测试,如下所示:
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);
});
但是,量角器抱怨:
错误:无效的柴属性:最终
如何导入?我尝试过:
import { eventual } from 'chai-as-promised';
但这不起作用。我该怎么做?我也尝试过使用Then
重写await
调用,但是编译器抱怨您不能将回调函数与异步函数混合使用。啊!
答案 0 :(得分:2)
在量角器配置中,在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);
});