加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何等待Protractor端的http请求响应

发布时间:2020-12-17 10:20:29 所属栏目:安全 来源:网络整理
导读:我使用了答案 https://stackoverflow.com/a/25149395/3330910中的代码. 我做下一个: it('HTTP request',function () { var BackRequest = require('../helper/backRequest'); var request = new BackRequest(); page.visitPage(); request.setBaseUrl('http
我使用了答案 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的帮助.

步骤#2立即得到解决,因为没有什么可以等待的,没有webdriver承诺,你只是将绝对数字与expect(1).toBe(2)进行比较;

您可以坚持使用chaining then(),或者我喜欢的方式是单独的it()块:

it('HTTP request',function () {
    // Step #1 code ...
});

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读