我如何在量角器测试中解决两个不同的承诺?

How can i resolve two distinct promises in a protractor test?

本文关键字:两个 承诺 量角器 测试 解决      更新时间:2023-09-26

我是Node.js的初学者。我有一个承诺,从服务器下载一个文件,然后解析成json对象,并返回它。另一个承诺返回一个网页元素()。这两个承诺必须一个接一个地解决:首先是返回json对象的承诺,这个工作很好,然后是获取page元素的promise。使用json对象中的一个键,我必须测试元素是否包含相同的文本。

代码:

var menuItems = element(by.id('menu')).all(by.tagName('li'));
it('should contain', function (done) {
  jsonPromise.then(function () { // work
    console.log('Inside jsonPromise then');
    menuItems.then(function () { //------> not step into
      console.log('Inside menuItems then');
      expect(menuItems.get(0).getText()).toEqual(jsonData.home);
      done();
    });
  });
});

用这段代码量角器返回:1个测试,0个断言,0个失败为什么呢?我做错了什么?

注意:两个控制台命令都执行

您需要jsonPromise置于protractor的控制流:

browser.controlFlow().await(jsonPromise).then(function (data) {
    expect(menuItems.first().getText()).toEqual(data.home);
});

与protractor 2.0.0 webbelements element不返回一个Promise

应该可以

menuItems = element(by.id('menu')).all(by.tagName('li'));
describe('my tests', function(){
  it('should contain', function(done) {
    jsonPromise.then(function(jsonData) { 
      console.log('Inside jsonPromise then');
      expect(menuItems.get(0).getText()).toEqual(jsonData.home);
    })
    .then(done)
    .catch(done);
    // if using jasmine2 it will be .catch(done.fail)
  });
});