Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  19] [ 5]  / answers: 1 / hits: 18366  / 11 Years ago, mon, february 10, 2014, 12:00:00

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?


More From » angularjs

 Answers
14

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

[#72602] Sunday, February 9, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zaynerogerb

Total Points: 454
Total Questions: 109
Total Answers: 97

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
zaynerogerb questions
;