2017-07-19 62 views
1

我已经使用angular2.0typescript构建了一个Web应用程序。现在我正在使用protractor为我的网站编写E2E运行量角器测试时进行API调用

现在,在我的一个测试中,我需要进行API调用(HTTP GET请求),并将响应值用作测试用例的输入。

所以基本上我想知道如何在Protractor-Jasmine中制作GET request并使用结果/响应。

回答

4

量角器运行在nodejs之上,引擎盖下调用Selenium API。您可以使用所有节点库,包括request

进口之间的选择/要求:

import * as request from 'request'; 
var request = require('request'); 

,并执行GET要求:

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

看看这个链接:

+0

因此,我想这一点, '它( '应该返回200,并含有适当的身体',函数(){ 请求( 'http://www.google.com',函数(误差,响应body){ console.log('error:',error); //如果发生错误,打印错误 console.log('statusCode:',response && response.statusCode); //收到回复后打印回复状态码 console.log('body:',body); //打印Google主页的HTML。 expect(response.statusCode).toEqual(500); }); });' 但测试似乎通过没有错误 – Vinay

+0

请求异步工作。让我更新我的答案:)更新。在done()之前放置你的断言。现在它应该像你期望的那样工作 –

+0

正是我需要的。感谢您的快速回复 – Vinay

相关问题