How to wait for an HTTP request from Protractor

I used the code from the answer https://stackoverflow.com>

Further:

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

And I get errors in order:

  • Error # 2
  • Error # 1

How can I make the protractor wait for Step # 1 and then do step # 2.

So far, only I can do it, these are entire 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); }); 

Update

So this ends with the following 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'); }); }); }); }); 

Thanks Leo Galucci for the help.

+5
source share
1 answer

Step number 2 is immediately allowed, because there is nothing to wait, there is no web disk promises, you just compare the absolute numbers with expect(1).toBe(2);

You can stick to the then() chain, just like you, or, as it seems to me, this is a separate it() block:

 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 my other answer helpful!

+6
source

Source: https://habr.com/ru/post/1206255/


All Articles