Skip to content

Commit

Permalink
added prebuilt asserters
Browse files Browse the repository at this point in the history
  • Loading branch information
sebv committed Nov 9, 2013
1 parent 1fcff69 commit 0c8d20f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
14 changes: 14 additions & 0 deletions examples/promise/wait-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ try {
wd = require('../../lib/main');
}

var asserters = wd.asserters; // commonly used asserters

// enables chai assertion chaining
chaiAsPromised.transferPromiseness = wd.transferPromiseness;

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions lib/asserters.js
Original file line number Diff line number Diff line change
@@ -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
};
1 change: 1 addition & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ module.exports = {
rewrap: wrap,

// Useful stuff
asserters: require('./asserters'),
SPECIAL_KEYS: SPECIAL_KEYS,
Q: Q,
findCallback: utils.findCallback,
Expand Down
51 changes: 50 additions & 1 deletion test/midway/wait-for-specs.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
require('../helpers/setup');

describe('wait-for ' + env.ENV_DESC, function() {
var asserters = wd.asserters;
var page = '<div id="theDiv"></div>';

var appendChild =
'setTimeout(function() {\n' +
' $("#theDiv").append("<div class=\\"child\\">a waitFor child</div>");\n' +
'}, arguments[0]);\n';

var removeChildren =
' $("#theDiv").empty();\n';
'$("#theDiv").empty();\n';

var appendChildAndHide =
'$("#theDiv").append("<div class=\\"child\\">a waitFor child</div>");\n' +
'setTimeout(function() {\n' +
' $("#theDiv .child").hide();\n' +
'}, arguments[0]);\n';

var appendChildHideAndShow =
'$("#theDiv").append("<div class=\\"child\\">a waitFor child</div>");\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) {
Expand Down Expand Up @@ -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('');
});

});

0 comments on commit 0c8d20f

Please sign in to comment.