browser-monkey
Advanced tools
Comparing version 1.11.1 to 1.12.0
48
index.js
@@ -206,2 +206,26 @@ var retry = require('trytryagain'); | ||
Selector.prototype.containing = function () { | ||
var finder = elementFinder.apply(null, arguments); | ||
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; | ||
} | ||
}, | ||
toString: function() { | ||
return 'not containing: ' + finder.toString(); | ||
} | ||
}); | ||
}; | ||
Selector.prototype.printFinders = function (finders) { | ||
@@ -318,4 +342,26 @@ return finders.map(function (f) { return f.toString(); }).join(' / '); | ||
Selector.prototype.filter = function (filter, message) { | ||
return this.addFinder({ | ||
find: function (elements) { | ||
var filteredElements = elements.toArray().filter(filter); | ||
if (filteredElements && filteredElements.length > 0) { | ||
return $(filteredElements); | ||
} | ||
}, | ||
toString: function () { | ||
return message || '[filter]'; | ||
} | ||
}); | ||
}; | ||
Selector.prototype.enabled = function () { | ||
return this.filter(function (element) { | ||
return !((element.tagName == 'BUTTON' || element.tagName == 'INPUT') && element.disabled == true); | ||
}); | ||
}; | ||
Selector.prototype.click = function(options) { | ||
return this.element(options).then(function(element) { | ||
return this.enabled().element(options).then(function(element) { | ||
debug('click', element); | ||
@@ -322,0 +368,0 @@ return sendclick(element); |
{ | ||
"name": "browser-monkey", | ||
"version": "1.11.1", | ||
"version": "1.12.0", | ||
"description": "reliable dom testing", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -161,2 +161,10 @@ # browser monkey | ||
## filter | ||
```js | ||
var scope = scope.filter(filter); | ||
``` | ||
* `filter(element)` a function that takes a DOM element, and returns either truthy or falsey. If truthy, then the element will be considered as part of the scope, if falsey then it won't. | ||
## component | ||
@@ -163,0 +171,0 @@ |
@@ -32,2 +32,26 @@ var browser = require('..'); | ||
it('should eventually find an element using a filter', function () { | ||
var promise = browser.find('.element').filter(function (element) { | ||
return element.classList.contains('correct'); | ||
}, 'has class "correct"').element(); | ||
$('<div class="element"></div>').appendTo(div); | ||
eventuallyInsertHtml('<div class="element correct"></div>'); | ||
return promise.then(function (element) { | ||
expect(element.className).to.equal('element correct'); | ||
}); | ||
}); | ||
it('filter fails with the right message', function () { | ||
var promise = browser.find('.element').filter(function (element) { | ||
return element.classList.contains('correct'); | ||
}, 'has class "correct"').element(); | ||
$('<div class="element"></div>').appendTo(div); | ||
eventuallyInsertHtml('<div class="element"></div>'); | ||
return expect(promise).to.be.rejectedWith('has class "correct"'); | ||
}); | ||
it('should eventually find an element in an iframe', function(){ | ||
@@ -82,15 +106,57 @@ var iframe = document.createElement('iframe'); | ||
it('should eventually click an element', function () { | ||
var promise = browser.find('.element').click(); | ||
var clicked = false; | ||
describe('clicking', function () { | ||
it('should eventually click an element', function () { | ||
var promise = browser.find('.element').click(); | ||
var clicked = false; | ||
eventuallyInsertHtml( | ||
$('<div class="element"></div>').click(function () { | ||
clicked = true; | ||
}) | ||
); | ||
eventuallyInsertHtml( | ||
$('<div class="element"></div>').click(function () { | ||
clicked = true; | ||
}) | ||
); | ||
return promise.then(function () { | ||
expect(clicked).to.equal(true); | ||
return promise.then(function () { | ||
expect(clicked).to.equal(true); | ||
}); | ||
}); | ||
it('waits until checkbox is enabled before clicking', function () { | ||
var promise = browser.find('input[type=checkbox]').click(); | ||
var clicked; | ||
var buttonState = 'disabled'; | ||
var button = $('<input type=checkbox disabled></input>').appendTo(div); | ||
button[0].addEventListener('click', function () { | ||
clicked = buttonState; | ||
}); | ||
setTimeout(function () { | ||
button.prop('disabled', false); | ||
buttonState = 'enabled' | ||
}, 100); | ||
return promise.then(function () { | ||
expect(clicked).to.equal('enabled'); | ||
}); | ||
}); | ||
it('waits until button is enabled before clicking', function () { | ||
var promise = browser.find('button', {text: 'a button'}).click(); | ||
var clicked; | ||
var buttonState = 'disabled'; | ||
var button = $('<button disabled>a button</button>').appendTo(div); | ||
button[0].addEventListener('click', function () { | ||
clicked = buttonState; | ||
}); | ||
setTimeout(function () { | ||
button.prop('disabled', false); | ||
buttonState = 'enabled' | ||
}, 100); | ||
return promise.then(function () { | ||
expect(clicked).to.equal('enabled'); | ||
}); | ||
}); | ||
}); | ||
@@ -97,0 +163,0 @@ |
42114
825
336