UNPKG

chai-webdriver

Version:

Build more expressive integration tests with some webdriver sugar for chai.js

186 lines (180 loc) 8.97 kB
// Generated by CoffeeScript 1.12.7 var chaiWebdriver, fs, seleniumWebdriver, sizzle, string; fs = require('fs'); string = require('string'); seleniumWebdriver = require('selenium-webdriver'); sizzle = require('webdriver-sizzle'); module.exports = chaiWebdriver = function(driver) { var $; $ = sizzle(driver, seleniumWebdriver); return function(chai, utils) { var assertElementExists; assertElementExists = function(selector, done) { return $.all(selector).then(function(els) { if (els.length === 0) { throw new Error("Could not find element with selector " + selector); } else { return done(); } }); }; chai.Assertion.addProperty('dom', function() { return utils.flag(this, 'dom', true); }); chai.Assertion.overwriteMethod('match', function(_super) { return function(matcher, done) { if (utils.flag(this, 'dom')) { return assertElementExists(this._obj, (function(_this) { return function() { return $(_this._obj).getText().then(function(text) { _this.assert(matcher.test(text), 'Expected element <#{this}> to match regular expression "#{exp}", but it contains "#{act}".', 'Expected element <#{this}> not to match regular expression "#{exp}"; it contains "#{act}".', matcher, text); return typeof done === "function" ? done() : void 0; }); }; })(this)); } else { return _super.call(this, matcher); } }; }); chai.Assertion.addMethod('visible', function(done) { var assert, assertDisplayed; if (!utils.flag(this, 'dom')) { throw new Error('Can only test visibility of dom elements'); } assert = (function(_this) { return function(condition) { _this.assert(condition, 'Expected #{this} to be visible but it is not', 'Expected #{this} to not be visible but it is'); return typeof done === "function" ? done() : void 0; }; })(this); assertDisplayed = (function(_this) { return function() { return $(_this._obj).isDisplayed().then(function(visible) { return assert(visible); }); }; })(this); if (utils.flag(this, 'negate')) { return $.all(this._obj).then(function(els) { if (els.length > 0) { return assertDisplayed(); } else { return assert(els.length > 0); } }); } else { return assertDisplayed(); } }); chai.Assertion.addMethod('count', function(length, done) { if (!utils.flag(this, 'dom')) { throw new Error('Can only test count of dom elements'); } return $.all(this._obj).then((function(_this) { return function(els) { _this.assert(els.length === length, 'Expected #{this} to appear in the DOM #{exp} times, but it shows up #{act} times instead.', 'Expected #{this} not to appear in the DOM #{exp} times, but it does.', length, els.length); return typeof done === "function" ? done() : void 0; }; })(this)); }); chai.Assertion.addMethod('text', function(matcher, done) { if (!utils.flag(this, 'dom')) { throw new Error('Can only test text of dom elements'); } return assertElementExists(this._obj, (function(_this) { return function() { return $(_this._obj).getText().then(function(text) { if (matcher instanceof RegExp) { _this.assert(matcher.test(text), 'Expected element <#{this}> to match regular expression "#{exp}", but it contains "#{act}".', 'Expected element <#{this}> not to match regular expression "#{exp}"; it contains "#{act}".', matcher, text); } else if (utils.flag(_this, 'contains')) { _this.assert(~text.indexOf(matcher), 'Expected element <#{this}> to contain text "#{exp}", but it contains "#{act}" instead.', 'Expected element <#{this}> not to contain text "#{exp}", but it contains "#{act}".', matcher, text); } else { _this.assert(text === matcher, 'Expected text of element <#{this}> to be "#{exp}", but it was "#{act}" instead.', 'Expected text of element <#{this}> not to be "#{exp}", but it was.', matcher, text); } return typeof done === "function" ? done() : void 0; }); }; })(this)); }); chai.Assertion.addMethod('style', function(property, value, done) { if (!utils.flag(this, 'dom')) { throw new Error('Can only test style of dom elements'); } return assertElementExists(this._obj, (function(_this) { return function() { return $(_this._obj).getCssValue(property).then(function(style) { _this.assert(style === value, "Expected " + property + " of element <" + _this._obj + "> to be '" + value + "', but it is '" + style + "'.", "Expected " + property + " of element <" + _this._obj + "> to not be '" + value + "', but it is."); return typeof done === "function" ? done() : void 0; }); }; })(this)); }); chai.Assertion.addMethod('value', function(value, done) { if (!utils.flag(this, 'dom')) { throw new Error('Can only test value of dom elements'); } return assertElementExists(this._obj, (function(_this) { return function() { return $(_this._obj).getAttribute('value').then(function(actualValue) { if (value instanceof RegExp) { _this.assert(value.test(actualValue), "Expected value of element <" + _this._obj + "> to be '" + value + "', but it is '" + actualValue + "'.", "Expected value of element <" + _this._obj + "> to not be '" + value + "', but it is."); } else { _this.assert(value === actualValue, "Expected value of element <" + _this._obj + "> to be '" + value + "', but it is '" + actualValue + "'.", "Expected value of element <" + _this._obj + "> to not be '" + value + "', but it is."); } return typeof done === "function" ? done() : void 0; }); }; })(this)); }); chai.Assertion.addMethod('disabled', function(done) { if (!utils.flag(this, 'dom')) { throw new Error('Can only test value of dom elements'); } return assertElementExists(this._obj, (function(_this) { return function() { return $(_this._obj).getAttribute('disabled').then(function(disabled) { _this.assert(disabled, 'Expected #{this} to be disabled but it is not', 'Expected #{this} to not be disabled but it is'); return typeof done === "function" ? done() : void 0; }); }; })(this)); }); chai.Assertion.addMethod('htmlClass', function(value, done) { if (!utils.flag(this, 'dom')) { throw new Error('Can only test value of dom elements'); } return assertElementExists(this._obj, (function(_this) { return function() { return $(_this._obj).getAttribute('class').then(function(classList) { _this.assert(~classList.indexOf(value), "Expected " + classList + " to contain " + value + ", but it does not."); return typeof done === "function" ? done() : void 0; }); }; })(this)); }); return chai.Assertion.addMethod('attribute', function(attribute, value, done) { if (!utils.flag(this, 'dom')) { throw new Error('Can only test style of dom elements'); } return assertElementExists(this._obj, (function(_this) { return function() { return $(_this._obj).getAttribute(attribute).then(function(actual) { if (typeof value === 'function' || ((value == null) && (done == null))) { done = value; _this.assert(typeof actual === 'string', "Expected attribute " + attribute + " of element <" + _this._obj + "> to exist", "Expected attribute " + attribute + " of element <" + _this._obj + "> to not exist"); return typeof done === "function" ? done() : void 0; } else { if (value instanceof RegExp) { _this.assert(value.test(actual), "Expected attribute " + attribute + " of element <" + _this._obj + "> to match '" + value + "', but it is '" + actual + "'.", "Expected attribute " + attribute + " of element <" + _this._obj + "> to not match '" + value + "', but it does."); } else { _this.assert(actual === value, "Expected attribute " + attribute + " of element <" + _this._obj + "> to be '" + value + "', but it is '" + actual + "'.", "Expected attribute " + attribute + " of element <" + _this._obj + "> to not be '" + value + "', but it is."); } return typeof done === "function" ? done() : void 0; } }); }; })(this)); }); }; };