0

I actually have issue with following code :

var promise = element(by.id("closeNotification")).isPresent(); // point A

console.log(promise);

promise.then((message) => {
    element(by.id("closeNotification")).click();
        browser.sleep(3000);
}, (errorMessage) => {                // Point B
    browser.refresh();
});

Here at point A, if element is present, program runs smoothly. But if element is not present, an error is thrown. At point B i have tried to handle the promise if it is rejected. Please help me with the condition if element is not present. Thanks in advance.

FCin
  • 3,804
  • 4
  • 20
  • 49
Omkar
  • 157
  • 1
  • 9
  • Possible duplicate of [Protractor waiting for element to be in DOM](https://stackoverflow.com/questions/30205235/protractor-waiting-for-element-to-be-in-dom) – Gunderson Mar 28 '18 at 13:15
  • And/or [Creating and resolving promises in protractor](https://stackoverflow.com/questions/24289431/creating-and-resolving-promises-in-protractor) – Gunderson Mar 28 '18 at 13:17
  • My issue is that element may or may not appear on every test run. Both suggested questions do not address this issue @Gunderson – Omkar Mar 28 '18 at 14:10

1 Answers1

0

move the click into if block, only when element is present, then execute click

var promise = element(by.id("closeNotification")).isPresent(); // point A

promise.then((present) => {
    if(present) { // only click when present == true
        element(by.id("closeNotification")).click();
    }
    browser.sleep(3000);
}, (errorMessage) => {                // Point B
    browser.refresh();
});
yong
  • 13,357
  • 1
  • 16
  • 27
  • It isn't working and reason is that variable 'promise' is not a boolean type but is a promise. Hence 'if' statement does not work. – Omkar Mar 29 '18 at 05:32