Permalink
Browse files

Refactor assertations, introduce click(..).

  • Loading branch information...
1 parent 45815f3 commit 1699e2cdfb967970bb8fb569ebbffea76f8dcdba @StanAngeloff committed Oct 29, 2011
Showing with 65 additions and 16 deletions.
  1. +7 −7 lib/inject/assert.js
  2. +57 −9 lib/inject/task_query.js
  3. +1 −0 lib/inject/test_page.js
View
@@ -82,25 +82,25 @@ var fail = exports.fail = function fail(actual, expected, message, operator) {
var ok = exports.ok = function ok(value, message) {
if ( !!! value) {
- fail(value, true, message, '==', exports.ok);
+ fail(value, true, message, '==');
}
};
var equal = exports.equal = function equal(actual, expected, message) {
if (actual != expected) {
- fail(actual, expected, message, '==', exports.equal);
+ fail(actual, expected, message, '==');
}
};
var notEqual = exports.notEqual = function notEqual(actual, expected, message) {
if (actual == expected) {
- fail(actual, expected, message, '!=', exports.notEqual);
+ fail(actual, expected, message, '!=');
}
};
var deepEqual = exports.deepEqual = function deepEqual(actual, expected, message) {
if ( ! _deepEqual(actual, expected)) {
- fail(actual, expected, message, 'deepEqual', exports.deepEqual);
+ fail(actual, expected, message, 'deepEqual');
}
};
@@ -167,19 +167,19 @@ var objectEqual = function objectEqual(left, right) {
exports.notDeepEqual = function notDeepEqual(actual, expected, message) {
if (_deepEqual(actual, expected)) {
- fail(actual, expected, message, 'notDeepEqual', exports.notDeepEqual);
+ fail(actual, expected, message, 'notDeepEqual');
}
};
exports.strictEqual = function strictEqual(actual, expected, message) {
if (actual !== expected) {
- fail(actual, expected, message, '===', exports.strictEqual);
+ fail(actual, expected, message, '===');
}
};
exports.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (actual === expected) {
- fail(actual, expected, message, '!==', exports.notStrictEqual);
+ fail(actual, expected, message, '!==');
}
};
View
@@ -6,18 +6,66 @@ function TaskQuery(task, query, message) {
return this;
};
-TaskQuery.prototype._elements = function TaskQuery_elements(variable) {
- return (variable || '_elements') + ' = document.querySelectorAll(' + JSON.stringify(this._query) + ')';
+TaskQuery.prototype._evaluate = function TaskQuery_evaluate(name, code) {
+ var result = this._page.evaluate(new Function(code));
+ if (result !== true) {
+ this._task.assert.fail(result, true, this._message, name + '(..)');
+ }
+ return this;
};
TaskQuery.prototype.first = function TaskQuery_first(block) {
- var hasElements = this._page.evaluate(new Function(' \
- var ' + this._elements() + '; \
- return _elements.length; \
- '));
- if ( ! hasElements) {
- return this._task.assert.fail(hasElements, true, this._message || ('expected at least one element: ' + this._query), this.first);
- }
+ return this._evaluate('first', '\n\
+ var _query = ' + JSON.stringify(this._query) + '\n\
+ _elements = document.querySelectorAll(_query);\n\
+ if ( ! _elements.length) {\n\
+ return "expected at least one element: " + _query;\n\
+ }\n\
+ if ((' + block.toString() + ')(_elements[0])) {\n\
+ return true;\n\
+ }\n\
+ return "expression failed on first element: " + _query;\n\
+ ');
+};
+
+TaskQuery.prototype.every = function TaskQuery_first(block) {
+ return this._evaluate('every', '\n\
+ var _query = ' + JSON.stringify(this._query) + '\n\
+ _elements = document.querySelectorAll(_query);\n\
+ if ( ! _elements.length) {\n\
+ return "expected at least one element: " + _query;\n\
+ }\n\
+ var _result = true,\n\
+ _index = 0;\n\
+ Array.prototype.slice.call(_elements).forEach(function(_element) {\n\
+ _index = _index + 1;\n\
+ if (_result === true) {\n\
+ if ((' + block.toString() + ')(_element)) {\n\
+ return true;\n\
+ }\n\
+ _result = "expression failed on element " + _index + ": " + _query;\n\
+ }\n\
+ });\n\
+ return _result;\n\
+ ');
+};
+
+TaskQuery.prototype.click = function TaskQuery_click(index) {
+ index || (index = 0);
+ return this._evaluate('click', '\n\
+ var _query = ' + JSON.stringify(this._query) + '\n\
+ _elements = document.querySelectorAll(_query);\n\
+ if ( ! _elements.length) {\n\
+ return "expected at least one element: " + _query;\n\
+ }\n\
+ if ( ! _elements[' + index + ']) {\n\
+ return "no element at index ' + index + ': " + _query;\n\
+ }\n\
+ var _event = document.createEvent("MouseEvents");\n\
+ _event.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n\
+ _elements[' + index + '].dispatchEvent(_event);\n\
+ return true;\n\
+ ');
};
exports = TaskQuery;
View
@@ -66,6 +66,7 @@ TestPage.prototype._complete = function TestPage_complete(child, status, message
if (child._status === STATUS_FAIL) {
this._skip = true;
_failed = true;
+ // TODO: throw exception to stop further processing of group
}
var remaining = 0;
this.top._descend(function() {

0 comments on commit 1699e2c

Please sign in to comment.