我正在尝试使用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);
});

但是, Protractor 提示:



我该如何导入?我试过了:
import { eventual } from 'chai-as-promised';

但这是行不通的。我怎样才能做到这一点?我也尝试过使用Then重写await调用,但是编译器提示您无法将回调函数与异步函数混合使用。啊!

最佳答案

在 Protractor 配置中,在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);
});

关于angular - TypeScript和chai-promsied : eventually is an invalid property,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52626679/

10-14 17:19