From 0c8d20fda4551c432ff0ab91ac1a0aef4dc9d6e8 Mon Sep 17 00:00:00 2001 From: sebv Date: Sat, 9 Nov 2013 13:11:44 +0800 Subject: [PATCH] added prebuilt asserters --- examples/promise/wait-for.js | 14 ++++++++++ lib/asserters.js | 42 +++++++++++++++++++++++++++++ lib/main.js | 1 + test/midway/wait-for-specs.js | 51 ++++++++++++++++++++++++++++++++++- 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 lib/asserters.js diff --git a/examples/promise/wait-for.js b/examples/promise/wait-for.js index aed6509d..50be8426 100644 --- a/examples/promise/wait-for.js +++ b/examples/promise/wait-for.js @@ -11,6 +11,8 @@ try { wd = require('../../lib/main'); } +var asserters = wd.asserters; // commonly used asserters + // enables chai assertion chaining chaiAsPromised.transferPromiseness = wd.transferPromiseness; @@ -81,6 +83,11 @@ browser .execute( appendChild, [500] ) .waitFor(textInclude('a waitFor child') , 2000) .should.eventually.include('a waitFor child') + // using prebuilt asserter + .execute(removeChildren) + .execute( appendChild, [500] ) + .waitFor(asserters.textInclude('a waitFor child') , 2000) + .should.eventually.include('a waitFor child') // waitForElement without asserter .execute(removeChildren) @@ -92,11 +99,18 @@ browser .execute(removeChildren) .execute( appendChild, [500] ) .waitForElementByCss("#i_am_an_id .child", textNonEmpty , 2000) + // using prebuilt asserter + .execute(removeChildren) + .execute( appendChild, [500] ) + .waitForElementByCss("#i_am_an_id .child", asserters.textNonEmpty , 2000) .text().should.become('a waitFor child') // trying isVisible asserter .waitForElementByCss("#i_am_an_id .child", isVisible , 2000) .text().should.become('a waitFor child') + // using prebuilt asserter + .waitForElementByCss("#i_am_an_id .child", asserters.isVisible , 2000) + .text().should.become('a waitFor child') // monkey patched method .execute(removeChildren) diff --git a/lib/asserters.js b/lib/asserters.js new file mode 100644 index 00000000..93015f6c --- /dev/null +++ b/lib/asserters.js @@ -0,0 +1,42 @@ +var S = require('string'); + +// todo: update doc tool to autodocument those + +function nonEmptyText(target, cb) { + target.text(function(err, text) { + if(err) { return cb(err); } + var satisfied = text && S(text).trim().length >0; + cb(null, satisfied, satisfied? text : undefined); + }); +} + +function textInclude(content) { + return function(target, cb) { + target.text(function(err, text) { + if(err) { return cb(err); } + var satisfied = text && S(text).contains(content); + cb(null, satisfied, satisfied? text : undefined); + }); + }; +} + +function isVisible(el,cb) { + el.isVisible(function(err, isVisible) { + if(err) { return cb(err); } + cb(null, isVisible); + }); +} + +function isHidden(el,cb) { + el.isVisible(function(err, isVisible) { + if(err) { return cb(err); } + cb(null, !isVisible); + }); +} + +module.exports = { + nonEmptyText: nonEmptyText, + isVisible: isVisible, + isHidden: isHidden, + textInclude: textInclude +}; diff --git a/lib/main.js b/lib/main.js index 710cc7c0..3a521b04 100644 --- a/lib/main.js +++ b/lib/main.js @@ -151,6 +151,7 @@ module.exports = { rewrap: wrap, // Useful stuff + asserters: require('./asserters'), SPECIAL_KEYS: SPECIAL_KEYS, Q: Q, findCallback: utils.findCallback, diff --git a/test/midway/wait-for-specs.js b/test/midway/wait-for-specs.js index 5b4f1808..afef1ec8 100644 --- a/test/midway/wait-for-specs.js +++ b/test/midway/wait-for-specs.js @@ -1,14 +1,29 @@ require('../helpers/setup'); describe('wait-for ' + env.ENV_DESC, function() { + var asserters = wd.asserters; var page = '
'; + var appendChild = 'setTimeout(function() {\n' + ' $("#theDiv").append("
a waitFor child
");\n' + '}, arguments[0]);\n'; var removeChildren = - ' $("#theDiv").empty();\n'; + '$("#theDiv").empty();\n'; + + var appendChildAndHide = + '$("#theDiv").append("
a waitFor child
");\n' + + 'setTimeout(function() {\n' + + ' $("#theDiv .child").hide();\n' + + '}, arguments[0]);\n'; + + var appendChildHideAndShow = + '$("#theDiv").append("
a waitFor child
");\n' + + '$("#theDiv .child").hide();\n' + + 'setTimeout(function() {\n' + + ' $("#theDiv .child").show();\n' + + '}, arguments[0]);\n'; // util function used tag chai assertion errors var tagChaiAssertionError = function(err) { @@ -169,4 +184,38 @@ describe('wait-for ' + env.ENV_DESC, function() { timeout: 2 * env.BASE_TIME_UNIT, pollFreq: 100 }); }); + express.partials['asserters.nonEmptyText'] = page; + it('asserters.nonEmptyText', function() { + return browser + .execute( appendChild, [env.BASE_TIME_UNIT] ) + .elementByCss("#theDiv .child").should.be.rejectedWith(/status: 7/) + .waitForElementByCss("#theDiv .child", asserters.nonEmptyText ,2 * env.BASE_TIME_UNIT) + .text().should.become('a waitFor child'); + }); + + express.partials['asserters.textInclude'] = page; + it('asserters.textInclude', function() { + return browser + .execute( appendChild, [env.BASE_TIME_UNIT] ) + .elementByCss("#theDiv .child").should.be.rejectedWith(/status: 7/) + .waitForElementByCss("#theDiv .child", asserters.textInclude('a waitFor child') ,2 * env.BASE_TIME_UNIT) + .text().should.become('a waitFor child'); + }); + + express.partials['asserters.isVisible'] = page; + it('asserters.isVisible', function() { + return browser + .execute( appendChildHideAndShow, [env.BASE_TIME_UNIT] ) + .waitForElementByCss("#theDiv .child", asserters.isVisible ,2 * env.BASE_TIME_UNIT) + .text().should.become('a waitFor child'); + }); + + express.partials['asserters.isHidden'] = page; + it('asserters.isHidden', function() { + return browser + .execute( appendChildAndHide, [env.BASE_TIME_UNIT] ) + .waitForElementByCss("#theDiv .child", asserters.isHidden ,2 * env.BASE_TIME_UNIT) + .text().should.become(''); + }); + });