Skip to content

Commit

Permalink
Added preliminary support for page object model methogology
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Jul 29, 2014
1 parent 0c2b09a commit 8d805d2
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 4 deletions.
1 change: 1 addition & 0 deletions bin/_clirunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ CliRunner.prototype = {
this.test_settings = this.settings.test_settings[env];
this.test_settings.custom_commands_path = this.settings.custom_commands_path || '';
this.test_settings.custom_assertions_path = this.settings.custom_assertions_path || '';
this.test_settings.page_objects_path = this.settings.page_objects_path || '';

this.inheritFromDefaultEnv();

Expand Down
60 changes: 58 additions & 2 deletions lib/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = new (function() {
var client;
var custom_commands_path;
var custom_assertions_path;
var page_objects_path;

/////////////////////////////////////////////////////////////////////
// Assertions
Expand Down Expand Up @@ -274,16 +275,68 @@ module.exports = new (function() {

}

/**
* Loads page object files
*/
function loadPageObjects() {
if (!page_objects_path) {
return;
}

client.api.page = {};

var absPath = path.join(process.cwd(), page_objects_path);
var pageFiles = fs.readdirSync(absPath);

for (var i = 0, len = pageFiles.length; i < len; i++) {
if (path.extname(pageFiles[i]) === '.js') {
var pageName = path.basename(pageFiles[i], '.js');
var pageFnOrObject = require(path.join(absPath, pageFiles[i]));
addPageObject(pageName, pageFnOrObject, client.api, client.api.page);
}
}
}

/**
* Instantiates the page object class
* @param {String} name
* @param {Object} pageFnOrObject
* @param {Object} context
* @param {Object} parent
*/
function addPageObject(name, pageFnOrObject, context, parent) {
parent[name] = function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(context);

return new (function PageObject() {
if (typeof pageFnOrObject == 'function') {
function PageObject() {
return pageFnOrObject.apply(this, args);
}
PageObject.prototype = pageFnOrObject.prototype;

if (!pageFnOrObject.prototype._instance) {
pageFnOrObject.prototype._instance = new PageObject();
}

return pageFnOrObject.prototype._instance;
}

return pageFnOrObject;
})();
};
}

/**
* Adds a command/assertion to the queue.
*
* @param {String} name
* @param {Object} command
* @param {Object} context
* @param {Object} [parent]
* @param {Boolean} [resetQueue]
*/
function addCommand(name, command, context, parent, resetQueue) {
function addCommand(name, command, context, parent) {
parent = parent || client.api;
if (parent[name]) {
client.results.errors++;
Expand All @@ -308,6 +361,7 @@ module.exports = new (function() {
client = c;
custom_commands_path = c.options.custom_commands_path;
custom_assertions_path = c.options.custom_assertions_path;
page_objects_path = c.options.page_objects_path;
return this;
};

Expand All @@ -320,11 +374,13 @@ module.exports = new (function() {
loadAssertions();
loadCustomCommands();
loadCustomAssertions();
loadPageObjects();
return this;
};

this.addCommand = addCommand;
this.loadCustomCommands = loadCustomCommands;
this.loadCustomAssertions = loadCustomAssertions;
this.loadPageObjects = loadPageObjects;
this.createAssertion = createAssertion;
})();
1 change: 0 additions & 1 deletion lib/runner/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ module.exports = new (function() {
this.run = function runner(test_source, opts, additional_opts, finishCallback) {
opts.parallelMode = process.env.__NIGHTWATCH_PARALLEL_MODE == '1';
opts.live_output = additional_opts.live_output;

opts.report_prefix = '';

globalStartTime = new Date().getTime();
Expand Down
25 changes: 25 additions & 0 deletions tests/extra/assertions/customAssertion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
exports.assertion = function(test, testVal) {
this.expected = true;
this.message = '';

this.pass = function(value) {
test.equals(testVal, value, 'Value passed to the `pass` method is incorrect.');
return value === this.expected;
};

this.value = function(result) {
test.deepEqual(result, {
value : testVal
}, 'Value passed to the `value` method is incorrect.');
return result.value;
};

this.command = function(callback) {
callback({
value : testVal
});

return this;
};

};
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions tests/extra/pageobjects/SimplePage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function(client, test) {
test.ok(typeof client == 'object');

this.testPageAction = function() {
return this;
};
};
1 change: 1 addition & 0 deletions tests/src/runner/testCliRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ module.exports = {
silent: true,
custom_commands_path: '',
custom_assertions_path: '',
page_objects_path: '',
output: true
}});
test.equals(runner.output_folder, 'output');
Expand Down
36 changes: 35 additions & 1 deletion tests/src/testNightwatchApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = {
test.done();
});

client.options.custom_commands_path = './extra';
client.options.custom_commands_path = './extra/commands';
Api.init(client);
Api.loadCustomCommands();

Expand All @@ -57,6 +57,40 @@ module.exports = {
test.equal(command.context, client.api, 'Command should contain a reference to main client instance.');
},

testAddPageObject : function(test) {
var client = this.client;
client.on('selenium:session_create', function(sessionId) {
test.done();
});

client.options.page_objects_path = './extra/pageobjects';
Api.init(client);
Api.loadPageObjects();

test.ok(typeof client.api.page == 'object');
test.ok('SimplePage' in client.api.page);

client.api.page.SimplePage(test);
},

testAddCustomAssertion : function(test) {
var client = this.client;
client.on('selenium:session_create', function(sessionId) {
test.done();
});

client.options.custom_assertions_path = './extra/assertions';
Api.init(client);
Api.loadCustomAssertions();

test.expect(4);
test.ok('customAssertion' in client.api.assert);
test.ok('customAssertion' in client.api.verify);

client.api.assert.customAssertion(test, true);
client.queue.run();
},

tearDown : function(callback) {

this.client = null;
Expand Down

0 comments on commit 8d805d2

Please sign in to comment.