如何使从量角器测试POST请求?

问题描述:

我想作一个POST请求(使用JSON的有效载荷)到一个数据库服务器中运行的量角器测试之前,在为了注入测试数据。我怎样才能做到这一点,如果在所有可能的?

I would like to make a POST request (with JSON payload) to a database server prior to running a Protractor test, in order to inject test data. How can I do this, if at all possible?

我发现了一个办法做到这一点,与安德烈D的帮助,它的要点是通过 browser.executeAsyncScript 和注入 $ HTTP服务那里。然后,$ http服务被告知做一个POST请求。下面是它是如何做的例子CoffeeScript的:

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 service in there. The $http service is then told to make a POST request. Here's example CoffeeScript of how it's done:

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)
)