我使用了答案
https://stackoverflow.com/a/25149395/3330910中的代码.
原文链接:https://www.f2er.com/angularjs/240580.html我做下一个:
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 });
我按顺序得到错误:
如何强制量角器等待步骤#1然后执行步骤#2.
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); });
更新
因此,它最终采用下一种方法:
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的帮助.