-->

Making an API call while running protractor tests

2020-07-18 20:49发布

问题:

I have built a web application using angular2.0 and typescript. Now I am writing E2E for my site using protractor.

Now, in one of my tests I need to make an API call(HTTP GET request) and use the response value as an input in my test case.

So basically I want to know how to make a GET request in Protractor-Jasmine and use the result/response.

回答1:

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

And perform your GET request:

it('Should reach google.com', done => {
    request('http://www.google.com', function (error, response, body) {
        console.log('error:', error); // Print the error if one occurred
        console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
        console.log('body:', body); // Print the HTML for the Google homepage.
        done(); //informs runner that the asynchronous code has finished
    });
});

Check out this links:

  • https://nodejs.org/api/http.html
  • https://www.npmjs.com/package/request