如何从量角器测试中发出POST请求?

时间:2021-02-15 18:10:50

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?

我希望在运行pro拖拉机测试之前,向数据库服务器发送一个POST请求(JSON有效负载),以便注入测试数据。如果可能的话,我该怎么做呢?

3 个解决方案

#1


5  

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:

我在Andres d的帮助下找到了一种方法,其要点是通过浏览器在浏览器中运行一个脚本。executeAsyncScript并将$http服务注入其中。然后通知$http服务发出POST请求。以下是它是如何完成的范例:

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

#2


5  

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 superagent in your beforeEach like so:

例如,你可以在你喜欢的before - each中使用superagent:

var request = require( "superagent" );

describe( "Something", function() {

  beforeEach( function( done ) {
    request
      .post( "http://localhost/api/foo" )
      .send( {data : "something"} )
      .end( done );
  } );

} );

#3


3  

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函数中运行一些异步设置代码。您需要明确地告诉pro拖拉机等待您的请求完成。这可以用流程来完成。

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.

**根据pro拖拉机1.1.0 on prepare可以返回一个承诺,所以使用flow来明确地等待承诺的解决是不必要的。

See: https://github.com/angular/protractor/blob/master/CHANGELOG.md

参见:https://github.com/angular/protractor/blob/master/CHANGELOG.md

#1


5  

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:

我在Andres d的帮助下找到了一种方法,其要点是通过浏览器在浏览器中运行一个脚本。executeAsyncScript并将$http服务注入其中。然后通知$http服务发出POST请求。以下是它是如何完成的范例:

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

#2


5  

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 superagent in your beforeEach like so:

例如,你可以在你喜欢的before - each中使用superagent:

var request = require( "superagent" );

describe( "Something", function() {

  beforeEach( function( done ) {
    request
      .post( "http://localhost/api/foo" )
      .send( {data : "something"} )
      .end( done );
  } );

} );

#3


3  

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函数中运行一些异步设置代码。您需要明确地告诉pro拖拉机等待您的请求完成。这可以用流程来完成。

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.

**根据pro拖拉机1.1.0 on prepare可以返回一个承诺,所以使用flow来明确地等待承诺的解决是不必要的。

See: https://github.com/angular/protractor/blob/master/CHANGELOG.md

参见:https://github.com/angular/protractor/blob/master/CHANGELOG.md