2017-07-19 167 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

相關問題