2
votes

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 first browser.wait will finish with a succeeded promise, so its .then should go into the first function, and print Found element 1.
  • If element1 is, however, not present, then the first browser.wait will finish with a failed promise, so the .then should go into the second function, which looks for element2. Then, if element2 is present, the test should continue, print Found element 2, and then pass. If element2 is not present, however, it should fail at this second browser.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?

1

1 Answers

3
votes

I would just use the Expected Conditions, there is a built-in or operator:

var EC = protractor.ExpectedConditions;
var element1 = element(by.id('test-elem1'));
var element2 = element(by.id('test-elem2'));

browser.wait(EC.or(EC.presenceOf(element1), EC.presenceOf(element2)), 5000); 

This would fail if both elements are not present and pass otherwise.


Now, let's try to understand what is happening with your initial code.

The first problem is that the first argument to browser.wait() should be a function. Also, you should put the second console.log() call into the then() callback of the second browser.wait() call:

var element1 = element(by.id('test-elem1'));
var element2 = element(by.id('test-elem2'));

browser.wait(function() {
    return element1.isPresent();
}, 5000).then(function() {
    console.log('Found element 1');
}, function() {
    // Element 1 not present --> look for element 2
    browser.wait(function() {
        return element2.isPresent();
    }, 5000).then(function () {
        print('Found element 2');
    }
});