简体   繁体   English

解决量角器中的黄瓜前景

[英]Resolving cucumber promise in protractor

I am trying to write cucumber scenarios with protractor and using assert to verify that elements in the dom contain what they are supposed to. 我试图用量角器编写黄瓜方案,并使用assert来验证dom中的元素是否包含了它们应有的功能。 This is what I have tried so far: 到目前为止,这是我尝试过的:

Then('the title should be {string}', function(expectedTitle) {
  const browserTitle=browser.getTitle().then(function(title) {
    return title;
  })
  assert.equal(browserTitle, expectedTitle);
});

This is what I am getting. 这就是我得到的。 I read this as the promise not being resolved properly. 我读到这是因为诺言没有得到正确解决。

1) Scenario: Visit Homepage # e2e/features/homepage.feature:6
   ✔ Given I browse to "/" # e2e/definitions/navsteps.js:4
   ✖ Then the title should be "bla bla bla" # e2e/definitions/navsteps.js:9
       AssertionError [ERR_ASSERTION]: ManagedPromise {
         flow_:
          ControlFlow {
            propagateUnhandledRejections_: true,
            activeQueue_:
             TaskQueue {
            == 'bla bla bla'
           at World.<anonymous> (/Users/arnab/work/bla-bla/e2e/definitions/navsteps.js:13:10)
   ✔ After # node_modules/protractor-cucumber-framework/lib/resultsCapturer.js:25

1 scenario (1 failed)
2 steps (1 failed, 1 passed)

Shouldn't it be enough to resolve the promise using then ? 使用then解决诺言是否足够? What am I missing? 我想念什么? I took a look at this SO question , and the accepted solution resolves the title in the same way. 我看了一下这个SO问题被接受的解决方案以相同的方式解析标题。

Then('the title should be {string}', function(expectedTitle) {

  const browserTitle=browser.getTitle().then(function(title) {
    return title;
  })
  // actually, browserTitle here is still a promise, not the string of browser title
  // because Promise.then() will build a new promise

  assert.equal(browserTitle, expectedTitle);
  // Because the `assert` you used can't understand/respect promise, 
  // so it won't wait the promise (browserTitle) resolved/rejected before
  // compare to `expectedTitle`.

  // To fix your problem, you can use assertion library which respect promise,
  // like `chai` and `chai-as-promised`
});

Example of using chai and chai-as-promised 使用chaichai-as-promised示例

// protractor conf.js
onPrepare: function(){
  var chai = require('chai'),
      expect = chai.expect;

  chai.use(require('chai-as-promised'));
  global.expect = chai.expect;  
}
// add above code in `onPrepare` of protractor conf.js

Then('the title should be {string}', function(expectedTitle) {

  const browserTitle = browser.getTitle();

  return expect(browserTitle).to.eventually.equal(expectedTitle);
  // expect(A).to.eventually.xxx(B)
  // Only when A is promise-like object, you can use `eventually`.

  // Otherwise, you can't.
  return browser.getTitle().then(function(title){
     expect(title).to.equal(expectedTitle);
     // title at here is not a promise-like object
     // you can't use `eventually`
  });

});

For cucumber step definition, it require to return an promise-like object or invoke callback. 对于黄瓜步骤定义,它需要返回类似promise的对象或调用回调。 Otherwise the next step won't wait the previous step complete when self begin execute. 否则,自执行开始时,下一步将不会等待上一步完成。

Then('xxxx', function(){

  return a promise-like objct
});

Then('xxxx', function(callback){


  callback(); //it must be the last step of the function
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM