在运行量角器测试时进行API调用 [英] Making an API call while running protractor tests
问题描述
我已经使用angular2.0
和typescript
构建了一个Web应用程序.现在,我正在使用protractor
为我的站点编写E2E
.
I have built a web application using angular2.0
and typescript
. Now I am writing E2E
for my site using protractor
.
现在,在我的一项测试中,我需要进行API调用(HTTP GET请求),并将响应值用作我的测试用例中的输入.
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.
所以基本上我想知道如何在Protractor-Jasmine
中制作一个GET request
并使用结果/响应.
So basically I want to know how to make a GET request
in Protractor-Jasmine
and use the result/response.
推荐答案
量角器在nodejs之上运行,并且在后台调用Selenium API.您可以使用所有节点库,包括request
.
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');
并执行您的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
});
});
查看此链接:
- https://nodejs.org/api/http.html
- https://www.npmjs.com/package/request
这篇关于在运行量角器测试时进行API调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!