在一个单独的页面对象文件中(不在带有测试的实际文件中)我正在尝试执行以下操作:
this.item0 = element.all(by.repeater('menu items')).get(0);
这不起作用,因为代码在运行测试之前执行。除了在测试文件中调用get()(我不想这样做)之外,我还没有找到另一种方法。有没有办法在页面对象文件中执行此操作?
答案 0 :(得分:3)
此行为通常会使尝试编写页面对象的人感到困惑。
在您调用elementFinder或elementArrayFinder上的函数之前,您的定位器不会被执行(请参阅https://github.com/angular/protractor/blob/master/docs/api.md)。
我通常使用这种模式:
// Page object
MyView = function() {
// This will not find elements until you call count(), get(), first(), etc.
this.itemList = element.all(by.repeater('menu items'));
};
module.exports = new MyView();
// Test
// Require the page object at the top of the test file.
var myView = require('./my-view.js');
// Use the page object in the test.
it('should get first element', function() {
myView.itemList.get(0).then(function(webElement) {
})
});
答案 1 :(得分:1)
以下内容来自source code for ElementArrayFinder.get
/**
* Get an element within the ElementArrayFinder by index. The index starts at 0.
* This does not actually retrieve the underlying element.
*
ElementArrayFinder.prototype.get = function...
所以显然你甚至可以在加载元素之前从页面对象中调用它。
答案 2 :(得分:0)
是的,我不是唯一一个使用页面对象的人;) 我做了以下事情:
var MyPage = function () {
this.item0 = element.all(by.repeater('menu items')).get(0);
}
在我的测试中:
describe('MyPage:', function () {
var myPage = new MyPage();
// after this line navigate your browser to your page
// then you can call myPage.item0, myPage.whatEver
}