I'm trying to write a Protractor E2E test that checks if a certain element is present through browser.wait(...)
with element1.isPresent()
, and if it is, then good, but otherwise I want to check that another element is present via element2.isPresent()
.
Exactly one of element1
and element2
should be present, and if neither are, then I want the test to fail.
My code looks like this:
var element1 = element(by.id('test-elem1'));
var element2 = element(by.id('test-elem2'));
browser.wait(function() {
return element1.isPresent();
}, 5000, "Waiting for element 1").then(function() {
console.log('Found element 1');
// Do something with element 1
}, function() {
// Element 1 not present --> look for element 2
browser.wait(function() {
return element2.isPresent();
}, 5000, "Waiting for element 2");
print('Found element 2');
});
What I would expect is:
- if
element1
is present, then the firstbrowser.wait
will finish with a succeeded promise, so its.then
should go into the first function, and printFound element 1
. - If
element1
is, however, not present, then the firstbrowser.wait
will finish with a failed promise, so the.then
should go into the second function, which looks forelement2
. Then, ifelement2
is present, the test should continue, printFound element 2
, and then pass. Ifelement2
is not present, however, it should fail at this secondbrowser.wait
and thus the test should fail.
What actually happens is that if element1
is not present, the first browser.wait
just fails and makes the test fail. If element1
is present, then the rest of the test continues as expected.
I was under the impression that if the second parameter to .then()
is defined, then on fail, that second function should be called, rather than failing completely.
I'm pretty sure that I had code that was almost the same working elsewhere (I since deleted it, so I can't reference it anymore), so I'm not sure why this part is not working.
Why is this happening?