Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Protractor wait for an alert box?

I am writing E2E tests for a log in page. If the log in fails, an alert box pops up informing the user of the invalid user name or password. The log in itself is a call to a web service and the controller is handling the callback. When I use browser.switchTo().alert(); in Protractor, it happens before the callback finishes. Is there a way I can make Protractor wait for that alert box to pop up?

like image 790
Justin Avatar asked Jun 04 '14 12:06

Justin


People also ask

How do you wait for an alert in protractor?

switchTo(). alert(). then( function() { return true; }, function() { return false; } ); }); In general, this code constantly tries to switch to an alert until success (when the alert is opened at last).

How do I check if an alert is present in protractor?

getText() from Alerts in protractor : getText() method fetches the text present in the javascript alert; it returns the values as String promise. We have to resolve the promise to get the values out of it. getText() method is also applicable for all the javascript alerts like alert, confirmation box, prompt.


1 Answers

I solved similar task with the following statement in my Protractor test:

browser.wait(function() {
    return browser.switchTo().alert().then(
        function() { return true; }, 
        function() { return false; }
    );
});

In general, this code constantly tries to switch to an alert until success (when the alert is opened at last). Some more details:
"browser.wait" waits until called function returns true.
"browser.switchTo().alert()" tries to switch to an opened alert box and either has success, or fails.
Since "browser.switchTo().alert()" returns a promise, then the promise either resolved and the first function runs (returns true), or rejected and the second function runs (returns false).

like image 160
a-bobkov Avatar answered Sep 18 '22 15:09

a-bobkov