joe hardy
joe hardy

Reputation: 21

Getting individual ElementFinders from an ElementArrayFinder in a Page Object

In a separate Page Object file (not in the actual file with tests) I'm trying to do something like:

this.item0 = element.all(by.repeater('menu items')).get(0);

This won't work because the code is executed before the tests are run. I haven't found another way of doing this except to call get() in the test file (which I don't want to do). Is there a way to do this in the Page Object file?

Upvotes: 2

Views: 2298

Answers (3)

Joel Kornbluh
Joel Kornbluh

Reputation: 1437

The following is from 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...

So Apparently you should be able to call it from within the page object even before the elements are loaded.

Upvotes: 1

Andres D
Andres D

Reputation: 8900

This behavior often confuses people who are trying to write page objects.

Your locator will not be executed until you call a function on the elementFinder or the elementArrayFinder (see https://github.com/angular/protractor/blob/master/docs/api.md).

I usually use this pattern:

// 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) {
  })
});

Upvotes: 3

nilsK
nilsK

Reputation: 4351

yay, i'm not the only one using page objects ;) i did the following:

var MyPage = function () {
  this.item0 = element.all(by.repeater('menu items')).get(0);
}

in my tests:

describe('MyPage:', function () {
    var myPage = new MyPage();
     // after this line navigate your browser to your page
     // then you can call myPage.item0, myPage.whatEver
}

Upvotes: 0

Related Questions