Skip to content

Instantly share code, notes, and snippets.

@th3hunt
Last active October 16, 2017 15:12
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save th3hunt/2d3ea458d29da181a212 to your computer and use it in GitHub Desktop.
Save th3hunt/2d3ea458d29da181a212 to your computer and use it in GitHub Desktop.
Chain PageObject methods with Intern.js, by having each PageObject instance using a new/enriched remote Command
define(function (require) {
var Page = require('./base/page')
, _ = require('vendor/lodash/lodash.underscore');
function DashboardPage() {
Page.apply(this, arguments);
}
DashboardPage.prototype = Object.create(Page.prototype);
DashboardPage.prototype.constructor = DashboardPage;
_.extend(DashboardPage.prototype, {
remoteDelegates: ['openMenu'],
remoteMethods: {
findMenu: function () {
return this.findByCssSelector('.menu');
},
openMenu: function () {
return this
.findMenu()
.click()
.end();
},
findMenuItem: function (index) {
index = index || 1;
return this.findByCssSelector('.menu > li:nth-child(' + index + ')');
}
},
otherMethodThatDoesNotNeedChaining: function () {
}
});
return DashboardPage;
});
define(function (require) {
var Command = require('intern/dojo/node!leadfoot/Command')
, _ = require('vendor/lodash/lodash.underscore');
function AbstractPage(remote) {
this.remote = this._buildRemote(remote);
this._setRemoteDelegates();
}
_.extend(AbstractPage.prototype, {
// a list of remote method names, that should be made available on the page object instance
remoteDelegates: [],
// extend the remote command of the page with these methods
remoteMethods: {},
_buildRemote: function (parent) {
function PageCommand() {
Command.apply(this, arguments);
}
PageCommand.prototype = Object.create(Command.prototype);
PageCommand.prototype.constructor = PageCommand;
_.extend(PageCommand.prototype, this.remoteMethods);
return new PageCommand(parent);
},
_setRemoteDelegates: function () {
this.remoteDelegates
.forEach(function (name) {
this[name] = function delegate() {
return this.remote[name].apply(this.remote, arguments);
};
}, this);
}
});
return AbstractPage;
});
var po;
bdd.beforeEach(function () {
po = new DashboardPage(this.remote);
return this.remote.get('/dashboard');
});
bdd.it('contains the proper menu entries', function () {
return po
.openMenu()
.findMenuItem(1)
.getVisibleText()
.then(function (text) {
assert.equal(text, 'Foo');
})
.end()
.findMenuItem(2)
.getVisibleText()
.then(function (text) {
assert.equal(text, 'Bar');
})
.end()
.findMenuItem(3)
.getVisibleText()
.then(function (text) {
assert.equal(text, 'Qux');
})
.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment