我是 Protractor 和 AngularJS 的新手。我在后端使用 Parse。尝试做一个非常简单的测试:
describe('Test', function () {
beforeEach(function () {
browser.get('index.html#/example')
});
it('should have a button', function () {
expect(element(by.css('#test321')).isElementPresent()).toBe(true); //fails
}); ...
测试失败。该元素在 template.html 中:
...
<body>
<button id="test321">stuff</button>
...
它是由angular-route加载的。该路由还从后端加载数据:
...
config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('/example', {
templateUrl: 'template.html',
controller: 'templateCtrl',
resolve: {
data: 'dataService' //Data is loaded from Parse. This line causes the problem
}...
问题是由上面的“数据:”行引起的。如果我去掉那条线,或者让它返回静态结果,它就可以正常工作。此外,如果我移动此元素 index.html,它也能正常工作。
这似乎是一个时间问题。然而,根据文档 Protractor (或特别是 isElementPresent)在定位元素之前等待所有分辨率。
我晕了。非常感谢您的帮助。
更新:根据this这已在 Protractor 1.2 中解决,但我使用的是 1.4。很奇怪。
最佳答案
由于这是一个时间问题,解决方法是等待元素出现,然后再断言它存在:
describe('Test', function () {
beforeEach(function () {
browser.get('index.html#/example')
});
it('should have a button', function () {
browser.wait(function() {
return $('#test321').isPresent(); // keeps waiting until this statement resolves to true
}, timeToWaitInMilliseconds, 'message to log to console if element is not present after that time');
expect($('#test321').isPresent()).toBe(true);
}); ...
关于angularjs - Protractor、AngularJS、Parse——Protractor 不等待 Angular 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27219335/