javascript 如何从量角器测试中发出 POST 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21689089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make a POST request from a Protractor test?
提问by aknuds1
I would like to make a POST request (with JSON payload) to a database server prior to running a Protractortest, in order to inject test data. How can I do this, if at all possible?
我想在运行量角器测试之前向数据库服务器发出 POST 请求(带有 JSON 有效负载),以便注入测试数据。如果可能的话,我该怎么做?
采纳答案by aknuds1
I found a way to do it, with the help of Andres D. The gist of it is to run a script in the browser via browser.executeAsyncScript
and inject the $http servicein there. The $http service is then told to make a POST request. Here's example CoffeeScript of how it's done:
在 Andres D 的帮助下,我找到了一种方法。它的要点是通过在浏览器中运行脚本并在其中browser.executeAsyncScript
注入$http 服务。然后告诉 $http 服务发出一个 POST 请求。这是如何完成的示例 CoffeeScript:
browser.get('http://your-angular-app.com')
browser.executeAsyncScript((callback) ->
$http = angular.injector(["ng"]).get("$http")
$http(
url: "http://yourservice.com"
method: "post"
data: yourData
dataType: "json"
)
.success(->
callback([true])
).error((data, status) ->
callback([false, data, status])
)
)
.then((data) ->
[success, response] = data
if success
console.log("Browser async finished without errors")
else
console.log("Browser async finished with errors", response)
)
回答by Der Hochstapler
You can just use another library to run the POST request if you just want to populate your database.
如果您只想填充数据库,则可以使用另一个库来运行 POST 请求。
For example, you can use superagentin your beforeEach
like so:
例如,你可以使用的SuperAgent在你beforeEach
像这样:
var request = require( "superagent" );
describe( "Something", function() {
beforeEach( function( done ) {
request
.post( "http://localhost/api/foo" )
.send( {data : "something"} )
.end( done );
} );
} );
回答by rjferguson21
It is possible to run some async setup code in your onPrepare function of your protractor config. You need to explicitly tell protractor to wait for your request to finish. This can be done with flow.await() which plays nice with promises.
可以在量角器配置的 onPrepare 函数中运行一些异步设置代码。您需要明确告诉量角器等待您的请求完成。这可以通过 flow.await() 来完成,它与 Promise 配合得很好。
onPrepare: function() {
flow = protractor.promise.controlFlow()
flow.await(setup_data({data: 'test'})).then( function(result) {
console.log(result);
})
}
** As of protractor 1.1.0 on prepare can return a promise, so the use of flow
to explictly wait for the promise to resolve is unnecessary.
** 由于protractor 1.1.0 on prepare 可以返回一个promise,所以使用flow
显式等待promise 解析是不必要的。
See: https://github.com/angular/protractor/blob/master/CHANGELOG.md
请参阅:https: //github.com/angular/protractor/blob/master/CHANGELOG.md
回答by snaik
Another way of doing POST request from protractor is using "http"
从量角器执行 POST 请求的另一种方法是使用“http”
import http = require('http');
const data = yourData;
const options = {
port: portnumber,
hostname: hostname, // without http
path: '/api/path/',
method: 'POST'
headers: {
"content-type": "application/json"
}
};
const request = http.request(options, function (result) {
var body = '';
result.on("data", function (chunk) {
body = body + chunk;
});
result.on("end", function () {
console.log(body);
});
});
request.write(JSON.stringify(data));
request.end();