2

I am using protractor to run my cucumber tests. Inside my test I have the following assertion:

label.getText().then(
                function(labelText){
                    labelText = labelText.trim();
                    console.log('label text: ' + labelText);
                    chai.expect(labelText).to.equal(arg1);
                    callback();
                },
                function() {
                    callback.fail('Could not get page label text');
                });

When the Assertion is correct there is no problem. However when my labelText is different from arg1 I would like to still keep running it but I don't know how to add the exception or a fail callback in that. At the moment my application just exits. I know that is because I am not using a fail callback (I would like to know where I should have it).

I am also not sure if I should put the callback(); where it is now.

I am looking for solutions online and all I can find are examples using Mocha. I am not using Mocha or Jasmine. I am just using Cucumber framework with protractor. Since Cucumberjs does not have an assertion library, I added chai-as-promised for that. Thanks!

4

2 回答 2

2

expect()当回调中的调用失败时,Cucumber.js 似乎有问题。由于您已安装 chai-as-promised,请尝试执行以下操作:

var labelText = label.getText().then(
  function(labelText){
    labelText = labelText.trim();
    console.log('label text: ' + labelText);
    return labelText;
  });
chai.expect(labelText).to.eventually.equal(arg1).then(callback);

我从这个评论中得到了这个解决方法,它对我来说效果很好。

于 2015-04-13T23:47:19.923 回答
0

如果你有chai-as-promised那么你可以断言异步代码是这样的:

this.When(/^I assert async code$/, function(callback) {
    expect(asyncMethod()).to.eventually.equal(true).and.notify(callback);
});
于 2015-10-29T10:49:39.460 回答