最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Bare HTTP calls from Protractor tests - Stack Overflow

programmeradmin6浏览0评论

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 execute "bare" HTTP calls from Protractor tests?

One way that I found is using Node Http module, but it's a bit unwieldy. I wonder how such problems are typically solved - does Protractor expose anything? Is using Http (and other Node modules when you need them) the way to go? Is there some other way?

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 execute "bare" HTTP calls from Protractor tests?

One way that I found is using Node Http module, but it's a bit unwieldy. I wonder how such problems are typically solved - does Protractor expose anything? Is using Http (and other Node modules when you need them) the way to go? Is there some other way?

Share Improve this question asked Jan 10, 2014 at 22:54 Konrad GarusKonrad Garus 54.1k44 gold badges162 silver badges233 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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./andresdominguez/protractor-meetup/blob/master/test/e2e/member3-spec.js#L25 https://github./andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js

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

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

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']);
});

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/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.',
                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)

  1. 暂无评论