Making an API call while running protractor tests

穿精又带淫゛_ 提交于 2019-12-06 11:30:50

问题


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


来源:https://stackoverflow.com/questions/45182309/making-an-api-call-while-running-protractor-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!