Bare HTTP calls from Protractor tests

后端 未结 3 1868
萌比男神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();
        });
    

提交回复
热议问题