javascripttypescriptprotractore2e-testingangularjs-e2e

Check if 1 out of 2 elements isDisplayed in Protractor


In my test in Protractor I am expecting to see element1 Or element2 at the end, I found a "solution" which works only if element1 is displayed and element2 is not on the page

expect(element1.isDisplayed() || element2.isDisplayed()).toBeTruthy();

In case if element1 is not on the page but element2 is there - i am getting an error:

 Failed: No element found using locator: ...(element1 locator)

I cannot write test where only one expectation is possible, this is test with Data and in my case 2 results are possible. How can i validate if one of two elements isDisplayed on the page?


Solution

  • I don't think Jasmine understand the expression element1.isDisplayed() || element2.isDisplayed() , so ever the two elements are not display, the assertion still pass.

    The ExpectedConditions API supply and/or operation.

    var EC = protractor.ExpectedConditions;
    var condition = EC.or(EC.visibilityOf(element1), EC.visibilityOf(element2));
    
    expect(condition()).toBeTruthy();