Bare HTTP calls from Protractor tests

后端 未结 3 1867
萌比男神i
萌比男神i 2021-01-06 04:42

My Protractor tests need some data setup which I would like to implement by making a series of POSTs and PUTs to the running server.

So, the question is: How do you

相关标签:
3条回答
  • 2021-01-06 05:09

    For making bare http calls from protractor you need to use the HTTP module from node js... here is the simple solution that we had used in the situation

    1. protractor test script get the data from rest end point
    2. protractor test script get the data from web page
    3. protract test script validate this data against the data on web page

    So how to make the HTTP call to rest end point,

    use this documentation https://nodejs.org/api/http.html#http_http_get_options_callback

    and this is the code snippet

    you need to have

    var http=require('http');
    
    it('MAKEHTTPCALL', function() {
    
    var gotResponse=false;
            var myResponse={};
    
            //this function to wait the rest to respond back
            function waitForBackend(){
                browser.wait(function(){
                    //console.log(myResponse);
                    console.log(gotResponse);
                    return gotResponse;
                }, 5000);
            }
    
    
    
    
            var options = {
                    hostname: 'yourhostname.com',
                    port: 8081,
                    path: '/yourendpoint/path/XXXX',
                    method: 'GET',
                    headers: {
                      'token':'XXXXXXXX'
                    }
                  };
    
            var req = http.request(options, function(res) {
                console.log('STATUS: ' + res.statusCode);
                console.log('HEADERS: ' + JSON.stringify(res.headers));
                res.setEncoding('utf8');
                res.on('data', function (chunk) {
                  console.log('BODY: ' + chunk);
    gotResponse = true;
    myResponse=JSON.parse(chunk);
    
    /*
        TO DO 
        Add the script validations here…..
    
    */ 
    
    
                });
              });
    
    
            req.on('error', function(e) {
                console.log('problem with request: ' + e.message);
              });
    
    
    
            req.on('connect', function(res, socket, head) {
                console.log('got connected!');
            });
    
    
              req.end();
       waitForBackend();
        });
    
    0 讨论(0)
  • 2021-01-06 05:19

    An alternate way that doesn't depend on Angular is manually creating an XMLHttpRequest inside of browser.executeAsyncScript. This is especially helpful if you need to make a call as part of the test setup, prior to Angular loading or prior to navigating to a page at all.

    See this example in the Protractor docs:

    Example #3: Injecting a XMLHttpRequest and waiting for the result. In this example, the inject script is specified with a function literal. When using this format, the function is converted to a string for injection, so it should not reference any symbols not defined in the scope of the page under test.

    driver.executeAsyncScript(function() {
      var callback = arguments[arguments.length - 1];
      var xhr = new XMLHttpRequest();
      xhr.open("GET", "/resource/data.json", true);
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
          callback(xhr.responseText);
        }
      }
      xhr.send('');
    }).then(function(str) {
      console.log(JSON.parse(str)['food']);
    });
    
    0 讨论(0)
  • 2021-01-06 05:25

    If you have a service in your Angular app that you can call to create your testing objects there is a trick that I described here:

    Accessing Angular inside Protractor Test

    I gave a presentation about Protractor a few weeks ago. Here is an example of the technique:

    https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/member3-spec.js#L25 https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js

    You can also take a look at this post: http://eitanp461.blogspot.com/2014/01/advanced-protractor-features.html

    You can inject a module with protractor and then call it.

    0 讨论(0)
提交回复
热议问题