How can I make a POST request from a Protractor test?

后端 未结 4 1320
情深已故
情深已故 2020-12-15 07:57

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 p

相关标签:
4条回答
  • 2020-12-15 08:36

    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: 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.

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

    0 讨论(0)
  • 2020-12-15 08:45

    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 );
      } );
    
    } );
    
    0 讨论(0)
  • 2020-12-15 08:51

    Another way of doing POST request from protractor is using "http"

    const http = require('http');
    
       const data = yourData; 
       const options = {
            port: portnumber,
            hostname: hostname,  // without http
            path: '/api/path/',
            method: 'POST',
            headers: {
                "content-type": "application/json"                
            }
        };
    
        const request = http.request(options, function (result) {
            var body = '';
    
            result.on("data", function (chunk) {
                body = body + chunk;
            });
    
            result.on("end", function () {
              console.log(body);
            });
        });
    
        request.write(JSON.stringify(data));
        request.end(); 
    
    0 讨论(0)
  • 2020-12-15 09:00

    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)
    )
    
    0 讨论(0)
提交回复
热议问题