如何等待量角器端的http请求响应 [英] How to wait for the response of http request from Protractor side
问题描述
我使用了答案 https://stackoverflow.com/a/25149395/3330910 中的代码.
I used code from the answer https://stackoverflow.com/a/25149395/3330910.
接下来我做:
it('HTTP request', function () {
var BackRequest = require('../helper/backRequest');
var request = new BackRequest();
page.visitPage();
request.setBaseUrl('http://localhost:8081');
// Step #1
request.get('/api/v1/one')
.then(function(result){
expect(result.statusCode).toBe(100); // An error #1
expect(result.bodyString).toContain('Some text');
});
// Step #2
expect(1).toBe(2); // an error #2
});
我按顺序得到错误:
- 错误 #2
- 错误 #1
如何强制量角器等待第 1 步,然后执行第 2 步.
How can I force the protractor to wait for Step #1 and then do the step #2.
目前我唯一能做的就是链接 then() 函数:
For now only I can do is to chain then() functions:
request.get('/api/v1/one')
.then(function(result){
expect(result.statusCode).toBe(100); // An error #1
expect(result.bodyString).toContain('Some text')
.then(function(result){
expect(1).toBe(2);
});
更新
所以,它以下一个方法结束:
So, it ends up with the next approach:
describe('Scenarios', function () {
beforeEach(function () {
page.visitPage();
});
var chain = function () {
var defer = protractor.promise.defer();
defer.fulfill(true);
return defer.promise;
};
it('HTTP request', function () {
var BackRequest = require('../helper/backRequest');
var request = new BackRequest();
request.setBaseUrl('http://localhost:8081');
chain()
.then(function () {
// Save data
})
.then(function () {
request.get('/api/v1/one')
.then(function (result) {
expect(result.statusCode).toBe(200);
expect(result.bodyString).toContain('text');
});
})
.then(function () {
// Change and Save again
})
.then(function () {
request.get('/api/v1/one')
.then(function (result) {
expect(result.statusCode).toBe(200);
expect(result.bodyString).toContain('new text');
expect(result.bodyString).not.toContain('text');
});
});
});
});
感谢 Leo Gallucci 的帮助.
推荐答案
第 2 步立即得到解决,因为没有什么可等待的,没有 webdriver 承诺,您只是将绝对数字与 expect(1).toBe(2);
Step #2 gets resolved immediately because there is nothing to wait for, there are no webdriver promises there you're simply comparing absolute numbers with expect(1).toBe(2);
您可以像以前那样坚持使用链接 then()
,或者我更喜欢单独的 it()
块:
You can stick with chaining then()
as you did or the way I prefer is separate it()
blocks:
it('HTTP request', function () {
// Step #1 code ...
});
it('keeps testing other things in this step #2', function () {
expect(1).toBe(2);
});
顺便说一句,我很高兴你发现我的其他 回答!
BTW I'm glad you found useful my other answer!
这篇关于如何等待量角器端的http请求响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!