【问题标题】:Protractor wait for rest call finish量角器等待休息呼叫完成
【发布时间】:2016-10-10 14:51:08
【问题描述】:

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

1通过休息添加一些独特的对象(不使用浏览器,我不想在每个/全部之前使用,因为它只针对这个测试)

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

如您所见,在我将执行所有 webdriver 操作之后,将运行 rest 调用... 有什么想法吗?

编辑:

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

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

【问题讨论】:

    标签: angular protractor


    【解决方案1】:

    你创建promise的方式是绝对正确的

    唯一需要的更改是将其添加到Protractor Control Flow。它需要添加两个步骤,并且几乎不需要重构您的代码

    第 1 步:启动 Protractor 控制流程,然后使用 flow.execute() 插入任何 async function which returns promise into Control Flow

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

    这会将您的异步调用放入控制流中,并且浏览器命令将仅在 promise 解决后执行

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

    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');
            });
    
        });
    });
    

    【讨论】:

    • 你能给我看一下我的代码示例吗?我用带有控制流的代码示例编辑了第一篇文章,但它仍然不起作用
    • @user2771738 .. 如果您仍在尝试使用console.log() 语句,它将不会显示结果,因为console.log() 不是量角器控制流的一部分。有浏览器相关的语句来检查restCall 是否被插入。还要删除期望,因为它会调用 restCall 两次。我已经更新了答案
    • 好吧,我误解了控制流的概念。现在一切正常。
    猜你喜欢
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多