如何在Protractor中进行HTTP GET + POST请求 [英] How to make HTTP GET+POST request in Protractor

查看:111
本文介绍了如何在Protractor中进行HTTP GET + POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Protractor中发送HTTP get请求时遇到问题。实际上,我需要在UI中执行某些操作后检查数据库中的数据。

I am facing problem to send HTTP get request in Protractor. Actually, I need to check data in DB after perform some action in UI.

如果我能够使用JQuery执行此操作将非常有用,但我是无法找到如何在量角器中使用JQuery的方法。

It will be very helpful if I will be able to do it using JQuery, but I am not able to find a way how to use JQuery inside Protractor.

需要帮助!!

实际上,我们确实尝试使用Node.js lib,如下所示,但面临问题。

Actually, we did try to use the Node.js lib as shown below, but facing problems in it.

var http = require('http');

var json_data;

http.get('SiteUrl', function(response) {
    var bodyString = '';
    response.setEncoding('utf8');

    response.on("data", function(chunk) {
        bodyString += chunk;
    });

    response.on('end', function() {

        json_data = bodyString;
        console.log("1---->" + json_data);
    });

}).on('error', function(e) {
    console.log("There is an error in GET request");
});
console.log("2---->" + json_data);

调试后,我们发现问题是Protractor没有等待HTTP请求完成,只是传递。我们首先在控制台中获得 2 ----> 然后 1 ---->

After Debugging, we have found that the problem is Protractor is not waiting for HTTP request to be complete and just pass on. We are getting 2----> first in console and then 1---->.

推荐答案

我还使用 http 模块(用于不同目的,用于重新初始化数据库)。

I also use http module (for different purpose, for reinitialization of the database).

要使量角器等待结束请求,请使用promises

To make protractor wait for ending the request, use promises

var http = require('http');

var json_data;

http.get('SiteUrl', function(response) {
    var bodyString = '';
    response.setEncoding('utf8');

    response.on("data", function(chunk) {
        bodyString += chunk;
    });

    response.on('end', function() {
        json_data = bodyString;
        console.log("1---->"+json_data);
        // All the processing and Angular code should be here
        console.log("2---->"+json_data);
    });

}).on('error', function(e) {
    console.log("There is an error in GET request");
});

如果您不喜欢将所有数据处理放入 response.on('end ')回调,然后将回调作为一个单独的函数。

If you don't like putting all the data processing into response.on('end') callback, then make callback a separate function.

此外,我必须说Protractor不是用来直接检查数据库。它用于端到端测试。您应该更好地构建一个复杂的场景,在一个页面上写入一些数据,转到另一个页面并期望数据更新。

Additionally I have to say that Protractor is not intended to be used to check the database directly. It is for end-to-end testing. You should better build a complex scenario which writes some data on one of the page, go to another page and expects that data is updated.

这篇关于如何在Protractor中进行HTTP GET + POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆