[英]Protractor times out waiting for sync with page when using $resource
I'm testing Protractor with a small AngularJS app. 我正在使用一个小型AngularJS应用程序测试Protractor。
This is the test: 这是测试:
describe('Testing Protractor', function() {
var draftList;
it('should count the number of drafts', function() {
browser.get('#/');
draftList = element.all(by.repeater('newsletter in drafts'));
expect(draftList.count()).toEqual(2);
});
});
Controller: 控制器:
angular.module('myApp.controllers', []).
controller('DraftsCtrl', ['$scope', 'Draft', function($scope, Draft) {
$scope.drafts = Draft.query();
}])
Draft service: 服务草案:
angular.module('myApp.services', ['ngResource']).
factory('Draft', ['$resource',
function($resource) {
return $resource('api/drafts/:id')
}])
Running this test using Protractor results in the following error: 使用Protractor运行此测试会导致以下错误:
Error: Timed out waiting for Protractor to synchronize with the page after 11 seconds
However, if in the controller I change this line: 但是,如果在控制器中我改变了这一行:
$scope.drafts = Draft.query();
to this: 对此:
$scope.drafts = [];
The test fails as expected, but more importantly: it does not time out. 测试按预期失败,但更重要的是:它没有超时。
With query() enabled, both when running the app manually in a browser and when looking at the browser window opened by Protractor, the data returned by the API is correctly displayed by a repeater. 启用query()后,无论是在浏览器中手动运行应用程序还是查看Protractor打开的浏览器窗口时,API返回的数据都由转发器正确显示。
Why is Protractor not able to synchronize with the page when the service is communicating with the API? 为什么当服务与API通信时,Protractor无法与页面同步?
AngularJS is v1.2.0-rc3. AngularJS是v1.2.0-rc3。 Protractor is v0.12.0.
量角器是v0.12.0。
This is a known issue , but there is a temporary workaround. 这是一个已知问题 ,但有一个临时解决方法。 Set
ptor.ignoreSynchronization = true
. 设置
ptor.ignoreSynchronization = true
。
For example: 例如:
describe('Testing Protractor', function() {
var draftList;
var ptor;
beforeEach(function() {
ptor = protractor.getInstance();
ptor.ignoreSynchronization = true;
});
it('should count the number of drafts', function() {
ptor.get('#/');
draftList = element.all(by.repeater('newsletter in drafts'));
expect(draftList.count()).toEqual(2);
});
});
browser.ignoreSynchronization = true;
worked out for me. 为我工作了。
I'm using Protractor 3.3.0 and to get this to work in my test I had to defer the ignore synchronisation until after I had done the setup. 我正在使用Protractor 3.3.0并且为了在我的测试中使用它,我不得不推迟忽略同步,直到我完成设置。
So in my beforeEach I call my action: 所以在我之前我每次都叫我的行动:
var searchBox = element(by.css('#inpt_search'));
searchBox.sendKeys('test');
I then have to wait for the mock backend to populate the view (I'm not happy about these sleep
calls so if anyone has a better way of doing this please comment, I can't get expectedConditions.presenceOf
to work as it's part of the same bug) using browser.sleep(500)
. 然后我必须等待模拟后端填充视图(我对这些
sleep
调用不满意所以如果有人有更好的方法这样做请评论,我不能得到expectedConditions.presenceOf
工作,因为它的一部分同样的错误)使用browser.sleep(500)
。 Then in the test I set browser.ignoreSynchronization = true
which unblocks whatever is blocked and sees the browser content. 然后在测试中我设置
browser.ignoreSynchronization = true
,取消阻止被阻止的内容并查看浏览器内容。
describe('standard search', function (){
beforeEach(function (){
openApp();
var searchBox = element(by.css('#inpt_search'));
searchBox.sendKeys('test');
browser.sleep(500);
});
it('should work or summat', function () {
browser.ignoreSynchronization = true;
var fileItems = element.all(by.repeater('item in list'));
expect(fileItems.count()).toEqual(50);
});
});
Instead of using browser.ignoreSynchronization
, use browser.waitForAngularEnabled(*boolean*)
. 而不是使用
browser.ignoreSynchronization
,请使用browser.waitForAngularEnabled(*boolean*)
。 browser.waitForAngularEnabled(false)
sets browser.ignoreSynchronization
to true
, browser.waitForAngularEnabled(true)
sets browser.ignoreSynchronization
to false
. browser.waitForAngularEnabled(false)
将browser.ignoreSynchronization
设置为true
, browser.waitForAngularEnabled(true)
将browser.ignoreSynchronization
设置为false
。
you can also include this as part of your test suites' config file: 您还可以将此作为测试套件配置文件的一部分包含在内:
onPrepare: function () {
'use strict';
browser.waitForAngularEnabled(false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.