我想在运行量角器测试之前向数据库服务器发出POST请求(带有JSON有效负载),以便注入测试数据。如果可能的话,我该怎么做?
发布于 2014-02-15 22:09:05
我找到了一种方法,在Andres的帮助下,它的要点是通过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)
)发布于 2014-05-20 10:37:18
如果您只想填充数据库,可以使用另一个库来运行POST请求。
例如,您可以在超剂中使用beforeEach,如下所示:
var request = require( "superagent" );
describe( "Something", function() {
beforeEach( function( done ) {
request
.post( "http://localhost/api/foo" )
.send( {data : "something"} )
.end( done );
} );
} );发布于 2014-02-11 16:51:46
可以在量角器配置的onPrepare函数中运行一些异步设置代码。您需要显式告诉量角器等待您的请求完成。这可以用flow.await()来完成,它对承诺很好。
onPrepare: function() {
flow = protractor.promise.controlFlow()
flow.await(setup_data({data: 'test'})).then( function(result) {
console.log(result);
})
}**当量角器1.1.0上的准备可以返回一个承诺,所以使用flow明确等待承诺解决是不必要的。
请参阅:https://github.com/angular/protractor/blob/master/CHANGELOG.md
https://stackoverflow.com/questions/21689089
复制相似问题