browser-monkey
Advanced tools
Comparing version 1.14.0 to 1.15.0
135
index.js
@@ -14,29 +14,8 @@ var retry = require('trytryagain'); | ||
function cssWithText(css, options) { | ||
var text = options && options.hasOwnProperty('text')? options.text: undefined; | ||
if (text) { | ||
return css + ":contains(" + JSON.stringify(text) + ")"; | ||
} else { | ||
return css; | ||
} | ||
} | ||
function elementFinder(css, options) { | ||
var text = options && options.hasOwnProperty('text')? options.text: undefined; | ||
var ensure = options && options.hasOwnProperty('ensure')? options.ensure: undefined; | ||
var message = options && options.hasOwnProperty('message')? options.message: undefined; | ||
var cssContains = cssWithText(css, { | ||
text: text | ||
}); | ||
function elementFinder(css) { | ||
return { | ||
find: function(element) { | ||
var els = $(element).find(cssContains); | ||
var els = $(element).find(css); | ||
if (els.length > 0) { | ||
if (ensure) { | ||
ensure(els); | ||
} | ||
return els; | ||
return els; | ||
} | ||
@@ -46,3 +25,3 @@ }, | ||
toString: function() { | ||
return message || cssContains; | ||
return css; | ||
} | ||
@@ -52,5 +31,9 @@ }; | ||
function assertElementProperties(elements, expected, getProperty) { | ||
function assertElementProperties(elements, expected, getProperty, exact) { | ||
function assertion(actual, expected) { | ||
expect(actual, 'expected element to have ' + JSON.stringify(expected) + ' but contained ' + JSON.stringify(actual)).to.contain(expected); | ||
if (exact) { | ||
expect(actual, 'expected element to have exact text ' + JSON.stringify(expected) + ' but contained ' + JSON.stringify(actual)).to.equal(expected); | ||
} else { | ||
expect(actual, 'expected element to contain ' + JSON.stringify(expected) + ' but contained ' + JSON.stringify(actual)).to.contain(expected); | ||
} | ||
} | ||
@@ -77,11 +60,29 @@ | ||
var optionsObject = typeof options === 'object'; | ||
var validOptions = []; | ||
var css = optionsObject && options.hasOwnProperty('css')? options.css: undefined; | ||
var text = optionsObject && options.hasOwnProperty('text')? options.text: undefined; | ||
var message = optionsObject && options.hasOwnProperty('message')? options.message: undefined; | ||
var predicate = optionsObject && options.hasOwnProperty('elements')? options.elements: undefined; | ||
var length = optionsObject && options.hasOwnProperty('length')? options.length: undefined; | ||
var value = optionsObject && options.hasOwnProperty('value')? options.value: undefined; | ||
var html = optionsObject && options.hasOwnProperty('html')? options.html: undefined; | ||
function option(name) { | ||
validOptions.push(name); | ||
var value = optionsObject && options.hasOwnProperty(name)? options[name]: undefined; | ||
delete options[name]; | ||
return value; | ||
} | ||
var css = option('css'); | ||
var text = option('text'); | ||
var exactText = option('exactText'); | ||
var message = option('message'); | ||
var predicate = option('elements'); | ||
var length = option('length'); | ||
var value = option('value'); | ||
var html = option('html'); | ||
var checked = option('checked'); | ||
if (optionsObject) { | ||
var keys = Object.keys(options); | ||
if (keys.length > 0) { | ||
throw new Error('properties ' + keys.join(', ') + ' not recognised, try ' + validOptions.join(', ')); | ||
} | ||
} | ||
if (typeof options === 'string') { | ||
@@ -115,2 +116,6 @@ css = options; | ||
if (exactText) { | ||
assertElementProperties(els, exactText, function (e) { return e.text(); }, true); | ||
} | ||
if (value) { | ||
@@ -120,2 +125,16 @@ assertElementProperties(els, value, function (e) { return e.val(); }); | ||
if (checked) { | ||
var elements = els.toArray(); | ||
if (checked instanceof Array) { | ||
var elementsChecked = elements.map(function (element) { | ||
return !!element.checked; | ||
}); | ||
expect(elementsChecked, 'expected ' + elementsToString(els) + ' to have checked states ' + JSON.stringify(checked)).to.eql(checked); | ||
} else { | ||
var elementsNotMatching = elements.filter(function (element) { return element.checked != checked; }); | ||
expect(elementsNotMatching.length, 'expected ' + elementsToString(els) + ' to be ' + (checked? 'checked': 'unchecked')).to.equal(0); | ||
} | ||
} | ||
if (html) { | ||
@@ -157,4 +176,10 @@ assertElementProperties(els, html, function (e) { return e.html(); }); | ||
Selector.prototype.find = function () { | ||
return this.addFinder(elementFinder.apply(null, arguments)); | ||
Selector.prototype.find = function (selector, options) { | ||
var scope = this.addFinder(elementFinder(selector)); | ||
if (options) { | ||
return scope.addFinder(elementTester(options)); | ||
} else { | ||
return scope; | ||
} | ||
}; | ||
@@ -192,29 +217,19 @@ | ||
Selector.prototype.containing = function () { | ||
var finder = elementFinder.apply(null, arguments); | ||
Selector.prototype.containing = function (selector, options) { | ||
var findElements = elementFinder(selector); | ||
var finder; | ||
return this.addFinder({ | ||
find: function(elements) { | ||
var els = elements.filter(function() { | ||
try { | ||
return finder.find(this); | ||
} catch (e) { | ||
return false; | ||
} | ||
}); | ||
if (els.length > 0) { | ||
return els; | ||
if (options) { | ||
var testElements = elementTester(options); | ||
finder = { | ||
find: function (elements) { | ||
var found = findElements.find(elements); | ||
testElements.find(found); | ||
return found; | ||
} | ||
}, | ||
toString: function() { | ||
return 'containing: ' + finder.toString(); | ||
} | ||
}); | ||
}; | ||
} else { | ||
finder = findElements; | ||
} | ||
Selector.prototype.containing = function () { | ||
var finder = elementFinder.apply(null, arguments); | ||
return this.addFinder({ | ||
@@ -236,3 +251,3 @@ find: function(elements) { | ||
toString: function() { | ||
return 'not containing: ' + finder.toString(); | ||
return 'containing: ' + finder.toString(); | ||
} | ||
@@ -239,0 +254,0 @@ }); |
{ | ||
"name": "browser-monkey", | ||
"version": "1.14.0", | ||
"version": "1.15.0", | ||
"description": "reliable dom testing", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -262,3 +262,4 @@ # browser monkey | ||
* `options.text` - a string, expects the resolved scope to have the text. If an array of strings, expects the elements to have the same number of elements as there are strings in the array, and expects each string to be found in each respective element's text. | ||
* `options.text` - a string, expects the resolved scope to contain the text. If an array of strings, expects the elements to have the same number of elements as there are strings in the array, and expects each string to be found in each respective element's text. | ||
* `options.exactText` - a string, expects the resolved scope to have the exact text. If an array of strings, expects the elements to have the same number of elements as there are strings in the array, and expects each string to equal each respective element's text. | ||
* `options.css` - a CSS string. Expects the resolved element to be matched by the CSS selector. Note that it won't match if the element contains other elements that match the CSS selector. So if we have `{css: '.class'}` then we expect the resolved element to have a class `class`. | ||
@@ -265,0 +266,0 @@ * `options.value` - a string, expects the resolved element to be an input and have the value. An array expects the same number of inputs, each with the respective value. |
@@ -211,2 +211,14 @@ var browser = require('..'); | ||
it('eventually finds an element containing exactText', function () { | ||
var good = browser.find('.a', {exactText: '8'}).shouldExist(); | ||
var bad = browser.find('.b', {exactText: '8'}).shouldExist(); | ||
eventuallyInsertHtml('<div><div class="a">8</div><div class="b">28</div></div>'); | ||
return Promise.all([ | ||
good, | ||
expect(bad).to.be.rejectedWith('28') | ||
]); | ||
}); | ||
describe('shouldHave', function () { | ||
@@ -237,2 +249,57 @@ it('eventually finds an element and asserts that it has text', function () { | ||
describe('checkboxes', function () { | ||
it('eventually finds a checked checkbox', function () { | ||
var good = browser.find('.checkbox').shouldHave({checked: true}); | ||
var checkbox = $('<input class="checkbox" type=checkbox />').appendTo(div); | ||
setTimeout(function () { | ||
checkbox.prop('checked', true); | ||
}, 20); | ||
return Promise.all([ | ||
good | ||
]); | ||
}); | ||
it('fails if only one of many checkboxes is checked', function () { | ||
var good = browser.find('.checkbox').shouldHave({checked: true}); | ||
var checkbox = $('<input class="checkbox" type=checkbox /><input class="checkbox" type=checkbox />').appendTo(div); | ||
setTimeout(function () { | ||
checkbox[0].checked = true; | ||
}, 20); | ||
return Promise.all([ | ||
expect(good).to.be.rejected | ||
]); | ||
}); | ||
it('ensures that each checkbox in the scope is either checked or unchecked', function () { | ||
var good = browser.find('.checkbox').shouldHave({checked: [true, false]}); | ||
var bad = browser.find('.checkbox').shouldHave({checked: [false, true]}); | ||
var checkbox = $('<input class="checkbox" type=checkbox /><input class="checkbox" type=checkbox />').appendTo(div); | ||
setTimeout(function () { | ||
checkbox[0].checked = true; | ||
}, 20); | ||
return Promise.all([ | ||
good, | ||
expect(bad).to.be.rejected | ||
]); | ||
}); | ||
it('fails to find a checked checkbox', function () { | ||
var good = browser.find('.checkbox').shouldHave({checked: false}); | ||
var bad = browser.find('.checkbox').shouldHave({checked: true}); | ||
var checkbox = $('<input class="checkbox" type=checkbox />').appendTo(div); | ||
return Promise.all([ | ||
good, | ||
expect(bad).to.be.rejected | ||
]); | ||
}); | ||
}); | ||
it('eventually finds elements and asserts that they each have text', function () { | ||
@@ -239,0 +306,0 @@ var good = browser.find('.element div').shouldHave({text: ['one', 2]}); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
46332
928
338
0