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?
Protractor runs on top of nodejs, and under the hood is calling Selenium API. You can use all of the node libraries, including request . Choose between import/require: import * as request from 'request'; var request = require('request');
Yes, you can use karma and protractor together. Karma is used for unit testing the component you created using angular command you can test those components using karma. Protractor is used for end to end test.
You can just use another library to run the POST request if you just want to populate your database.
For example, you can use superagent in your beforeEach
like so:
var request = require( "superagent" );
describe( "Something", function() {
beforeEach( function( done ) {
request
.post( "http://localhost/api/foo" )
.send( {data : "something"} )
.end( done );
} );
} );
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)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With