Last active
October 16, 2017 15:12
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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