Angular 量角器等待rest调用完成

Angular 量角器等待rest调用完成,angular,protractor,Angular,Protractor,我有一个简单的场景,我想测试我是否能够删除一些对象。所以这个场景看起来像: 1通过rest调用添加一些唯一的对象(不使用浏览器,我不想在每个/全部之前使用,因为它仅适用于此测试) 2在web应用程序中删除上述对象 因此,我编写了代码的模拟: it('test promise', function(done) { console.log('start'); d = protractor.promise.defer(); setTimeout(function() {

我有一个简单的场景,我想测试我是否能够删除一些对象。所以这个场景看起来像:

1通过rest调用添加一些唯一的对象(不使用浏览器,我不想在每个/全部之前使用,因为它仅适用于此测试)

2在web应用程序中删除上述对象

因此,我编写了代码的模拟:

it('test promise', function(done) {
    console.log('start');
    d = protractor.promise.defer();
    setTimeout(function() {
        console.log("rest call");
        d.fulfill('ok');
        done();
    }, 3000);
    console.log('before expect');
    expect(d).toBe('ok'); //code should be stoped here till rest above finish
    console.log('after expect');
    console.log('rest test based on deffer result');
    console.log('change tab... find elements... click to delete...');
});
此代码的输出为:

start
before expect
after expect
change tab... find elements... click to delete...
rest call
所以,正如你们所看到的,rest调用是在我执行所有webdriver操作之后运行的。。。 一些想法

编辑:

我添加了控制流,但它仍然不起作用。代码:

it('test promise', function(done) {
    console.log('start');
    flow = protractor.promise.controlFlow();
    var d = protractor.promise.defer();
    var restCall = function _makeRestCall() {
        setTimeout(function () {
            console.log("rest call");
            d.fulfill('ok');
        }, 3000);
        return d.promise
    };
    console.log('before expect');
    flow.execute(restCall);
    // expect(d).toBe('ok'); //version 1 not work
    expect(restCall).toBe('ok'); //version 2 not work
    console.log('after expect');
    console.log('rest test based on deffer result');
});
输出:

start
before expect
after expect
rest test based on deffer result
rest call

你创造承诺的方式是绝对正确的

唯一需要的更改是将其添加到。它需要添加两个步骤,并对代码进行少量重构

步骤1:启动量角器控制流,然后使用
Flow.execute()
插入任何

这将把异步调用放入控制流中,并且浏览器命令将仅在承诺得到解决后执行

更新:添加了测试用例的完整流程

describe('sample test', function(){
    it('test promise', function() {
        browser.get('')
        console.log('start');
        flow = protractor.promise.controlFlow();
        var d = protractor.promise.defer();
        var restCall = function _makeRestCall() {
            setTimeout(function () {
                console.log("rest call");
                d.fulfill('ok');
            }, 3000);
            return d.promise
        };
        console.log('before expect');

        // Can directly add expect here as flow.execute() returns promise 
        expect(flow.execute(restCall)).toBe('ok');

        // All subsequent browser command part of Protractor Control Flow will be executed only after the promise of restCall is resolved

        browser.getCurrentUrl().then(function(value) {
            console.log('after expect');
            console.log('rest test based on deffer result');
        });

    });
});

你能给我看看我的代码的例子吗。我用带有控制流的代码示例编辑了第一篇文章,但它仍然没有works@user2771738 .. 如果您仍在尝试使用
console.log()
语句,则不会显示结果,因为
console.log()
不是量角器控制流的一部分。使用与浏览器相关的语句检查是否插入了
restCall
。同时删除expect,因为它将调用restCall两次。我已经更新了答案,所以我误解了控制流的概念。现在一切正常。
describe('sample test', function(){
    it('test promise', function() {
        browser.get('')
        console.log('start');
        flow = protractor.promise.controlFlow();
        var d = protractor.promise.defer();
        var restCall = function _makeRestCall() {
            setTimeout(function () {
                console.log("rest call");
                d.fulfill('ok');
            }, 3000);
            return d.promise
        };
        console.log('before expect');

        // Can directly add expect here as flow.execute() returns promise 
        expect(flow.execute(restCall)).toBe('ok');

        // All subsequent browser command part of Protractor Control Flow will be executed only after the promise of restCall is resolved

        browser.getCurrentUrl().then(function(value) {
            console.log('after expect');
            console.log('rest test based on deffer result');
        });

    });
});