简体   繁体   中英

Making an API call while running protractor tests

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM