如何在量角器预期运行之前返回一个承诺?

时间:2016-04-21 08:59:39

标签: javascript protractor

Person.prototype.checkAssignmentIncomplete = Person.chain(function (data) {
var self = this;

var assignmentPane = self.browser.element(by.id(data.testId + '#' + data.deadline));
expect(assignmentPane.isPresent()).toBe(true);

assignmentPane.click();
self.browser.waitForAngular();

var foundName = false;
var promises = [];
element.all(by.binding('member.user.fullname')).each(function (nameColumn) {
    var row = nameColumn.element(by.xpath('..'));
    var promise = nameColumn.getText().then(function (name) {
        foundName = foundName || data.name === name;
        if (foundName) {
            var reviewButton = row.element(by.className('icon'));
            expect(reviewButton.isPresent()).toBe(false);
        }
    });
    promises.push(promise);
});

q.all(promises).then(function () {
    expect(foundName).toBe(true);
});

});

我在Protractor中进行自动化测试时遇到问题,似乎认为foundName是假的,而且我知道一个事实,当它运行时确实如此。我的结论是期望函数在承诺结束之前运行。

我想知道在检查expect函数中的foundName之前如何确定promise已经完成了?

2 个答案:

答案 0 :(得分:1)

    var self = this;

var assignmentPane = self.browser.element(by.id(data.testId + '#' + data.deadline));
expect(assignmentPane.isPresent()).toBe(true);

assignmentPane.click();
self.browser.waitForAngular();

var foundName = false;
element.all(by.binding('member.user.fullname')).each(function (nameColumn) {
    var row = nameColumn.element(by.xpath('..'));
    nameColumn.getText().then(function (name) {
        foundName = foundName || data.name === name;
        if (foundName) {
            var reviewButton = row.element(by.className('icon'));
            expect(reviewButton.isPresent()).toBe(false);
        }
    });
}).then(function () {
    expect(foundName).toBe(true);
});

这不是一个理想的解决方案,但在每个功能之后添加承诺似乎可以解决我遇到的问题。

答案 1 :(得分:0)

尝试使用protractor.promise.all(promises)函数在解析promise数组时进行回调 - https://angular.github.io/protractor/#/api?view=promise.all

q lib可能在这里不起作用