javascript 使用 $resource 时,量角器超时等待与页面同步
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19741896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Protractor times out waiting for sync with page when using $resource
提问by Joseph S.
I'm testing Protractor with a small AngularJS app.
我正在使用一个小型 AngularJS 应用程序测试量角器。
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. Protractor is v0.12.0.
AngularJS 是 v1.2.0-rc3。量角器是 v0.12.0。
回答by Harri Siirak
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);
});
});
回答by Lokesh
browser.ignoreSynchronization = true;
worked out for me.
browser.ignoreSynchronization = true;
对我来说有效。
回答by Stevo
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:
所以在我的 beforeEach 中,我呼吁我的行动:
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)
.
Then in the test I set browser.ignoreSynchronization = true
which unblocks whatever is blocked and sees the browser content.
然后我必须等待模拟后端填充视图(我对这些sleep
调用不满意,所以如果有人有更好的方法请发表评论,我无法开始expectedConditions.presenceOf
工作,因为它是同一错误的一部分)使用browser.sleep(500)
. 然后在测试中我设置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);
});
});
回答by Peter Parker
Instead of using browser.ignoreSynchronization
, use browser.waitForAngularEnabled(*boolean*)
. browser.waitForAngularEnabled(false)
sets browser.ignoreSynchronization
to true
, browser.waitForAngularEnabled(true)
sets browser.ignoreSynchronization
to false
.
而不是使用browser.ignoreSynchronization
,使用browser.waitForAngularEnabled(*boolean*)
。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);
}