error handler is not triggered when the parentElementFinder fails to find an element
Hello,
I use element(locator).element(more_specific).getText() a lot to limit the context in which the more specific element is searched. And I wish to be able to recover if the parent element does not exits (anymore)
I expected errorfn to be called, an error is thrown in stead in:
element(nonmatchinglocator).element(locator).getText().then(null, errorfn);
I ran the following test in the protractor test suite (protractor version 1.1.1) The tests succeed as expected, except the last one.
describe('ElementFinder', function() {
var handlers;
beforeEach(function() {
// Clear everything between each test.
browser.driver.get('about:blank');
handlers = {
onError: function(error){
return {};
},
onSucces: function(text){
return {};
}
}
});
/* guarentee our existing element is indeed findable */
it('should find an element using a parent locator', function() {
browser.get('index.html#/form');
var root = element(by.css('ul.menu'));
var leaf = root.element(by.linkText("repeater"));
expect(leaf.getText()).toEqual("repeater");
});
it('should trigger onError on non matching locator', function() {
browser.get('index.html#/form');
var root = element(by.css('ul.nonexistent'));
spyOn(handlers, 'onError').andCallThrough();
root.getText().then(handlers.onSucces, handlers.onError)
.then(function(){ // wait for getText to resolve before testing if the handler has been called
expect(handlers.onError).toHaveBeenCalled();
})
});
it('should trigger onError in then on an element using a parent with non matching locator', function() {
browser.get('index.html#/form');
var root = element(by.css('ul.nonexistent'));
var leaf = root.element(by.linkText("repeater"));
spyOn(handlers, 'onError').andCallThrough();
leaf.getText().then(handlers.onSucces, handlers.onError)
.then(function(){ // wait for getText to resolve before testing if the handler has been called
expect(handlers.onError).toHaveBeenCalled();
}, function(){ throw new Error("Should not have gotten here")})
});
});
I think the problem is that you're using spyOn(handlers, 'onError').andCallThrough();, so it's still throwing an error and you get to the error. Try this instead:
it('should trigger onError in then on an element using a parent with non matching locator', function() {
browser.get('index.html#/form');
var root = element(by.css('ul.nonexistent'));
var leaf = root.element(by.linkText("repeater"));
leaf.getText().then(function() {
throw new Error('expected a failure, got a pass')
}, function() {
console.log('yay, caught an error');
});
});
Thanks for looking into it. I tried your example to make sure that I did not look over any thing. But your example gives the same error: node lib/cli.js spec/errorHandlerChainConf.js Using the selenium server at http://localhost:4444/wd/hub ...F
Failures:
- ElementFinder should trigger onError in then on an element using a parent with non matching locator
Message:
Error: No element found using locator: By.cssSelector("ul.nonexistent")
Stacktrace:
Error: No element found using locator: By.cssSelector("ul.nonexistent")
==== async task ====
Asynchronous test function: it()
Error
at null.
(/home/ferengee/src/protractor/spec/errorHandlerChain/elements_spec.js:50:10) Error at null. (/home/ferengee/src/protractor/spec/errorHandlerChain/elements_spec.js:46:3) at Object. (/home/ferengee/src/protractor/spec/errorHandlerChain/elements_spec.js:1:63)
Finished in 6.661 seconds 4 tests, 4 assertions, 1 failure Line 49 tm 54:
var leaf = root.element(by.linkText("repeater"));
leaf.getText().then(function() {
throw new Error('expected a failure, got a pass');
}, function() {
console.log('yay, caught an error');
});
I am trying to dig deeper into this in the protractor source code but I have not yet found were I can expect it to go wrong. Maybe you could give me a pointer to where the child element's promise is rejected if the lookup for the parent elements locator fails. Thanks again
Ah apologies, didn't read well enough. You are right - at the moment, there's no way to capture errors from a parent element. This should be addressed when we do some element refactoring - see #1066
This is now fixed with 466b3831569dc28c5fc2be31fbdf96574e57c3f0
Very nice!