【问题标题】:How to wait for the response of http request from Protractor side如何等待量角器端的http请求响应
【发布时间】:2014-11-06 19:32:37
【问题描述】:

我使用了答案https://stackoverflow.com/a/25149395/3330910中的代码。

接下来我做:

it('HTTP request', function () {
    var BackRequest = require('../helper/backRequest');
    var request = new BackRequest();

    page.visitPage();

    request.setBaseUrl('http://localhost:8081');

    // Step #1
    request.get('/api/v1/one')
        .then(function(result){
        expect(result.statusCode).toBe(100); // An error #1
        expect(result.bodyString).toContain('Some text');
    });

    // Step #2
    expect(1).toBe(2); // an error #2
});

我按顺序得到错误:

  • 错误 #2
  • 错误 #1

如何强制量角器等待第 1 步,然后执行第 2 步。

目前我唯一能做的就是链接 then() 函数:

request.get('/api/v1/one')
    .then(function(result){
        expect(result.statusCode).toBe(100); // An error #1
        expect(result.bodyString).toContain('Some text')
    .then(function(result){
        expect(1).toBe(2);
        }); 

更新

所以,它以下一个方法结束:

describe('Scenarios', function () {

    beforeEach(function () {
        page.visitPage();
    });

    var chain = function () {
        var defer = protractor.promise.defer();
        defer.fulfill(true);
        return defer.promise;
    };

    it('HTTP request', function () {
        var BackRequest = require('../helper/backRequest');
        var request = new BackRequest();
        request.setBaseUrl('http://localhost:8081');

        chain()
            .then(function () {
                // Save data
            })

            .then(function () {
                request.get('/api/v1/one')
                    .then(function (result) {
                        expect(result.statusCode).toBe(200);
                        expect(result.bodyString).toContain('text');
                    });
            })

            .then(function () {
                // Change and Save again
            })

            .then(function () {
                request.get('/api/v1/one')
                    .then(function (result) {
                        expect(result.statusCode).toBe(200);
                        expect(result.bodyString).toContain('new text');
                        expect(result.bodyString).not.toContain('text');
                    });
            });
    });

});

感谢Leo Gallucci 的帮助。

【问题讨论】:

    标签: angularjs protractor


    【解决方案1】:

    第 2 步立即得到解决,因为无需等待,没有 webdriver 承诺,您只是将绝对数字与 expect(1).toBe(2); 进行比较

    您可以像以前一样坚持使用链接 then(),或者我更喜欢将 it() 块分开:

    it('HTTP request', function () {
        // Step #1 code ...
    });
    
    it('keeps testing other things in this step #2', function () {
        expect(1).toBe(2);
    });
    

    顺便说一句,我很高兴你发现我的另一个 answer 有用!

    【讨论】:

    • 感谢您的回复。我使用了单独的 It() 方法,但我想在一个测试中完成所有事情。有没有办法将任务添加到测试的控制流中?
    猜你喜欢
    • 2017-08-08
    • 2016-04-01
    • 1970-01-01
    • 2018-06-03
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    相关资源
    最近更新 更多