andy mccullough
andy mccullough

Reputation: 9591

Webdriver.io pageObject pattern - define element selector by passing argument

I am following the example, in order to define elements within pageObjects, using the ID selector...

var Page = require('./page')

var MyPage= Object.create(Page, {
    /**
     * define elements
     */
    firstName: { get: function () { return browser.element('#firstName-0'); } },
    lastName: { get: function () { return browser.element('#lastName-0'); } },

...

I am looking a way to pass an argument that would allow me to dynamically define the selector. e.g. '#firstName-0' I would like to have '#firstName-' + i so I could collect multiple first names.

I have tried

firstName: { get: function (i) { return browser.element('#firstName-' + i);}}

then within the test..

MyPage.firstName.get(0).setValue('foo');

but it complains that get() is not a function.

Doe anyone have any ideas?

Upvotes: 2

Views: 1759

Answers (1)

Florent B.
Florent B.

Reputation: 42528

Here is a way to define a function on a property with a property descriptor:

var Page = require('./page')

var MyPage= Object.create(Page, {
    /**
     * define elements
     */
    firstName: { value: { get: function (i) { return browser.element('#firstName-' + i); } } },
...

Usage:

MyPage.firstName.get(0).setValue('foo');

Upvotes: 3

Related Questions