Search code examples
angularjsseleniumwindowprotractorprompt

Protractor Accept a Prompt


My AngularJS application uses Keycloak for auth. and it opens a window prompt to ask the user to enter a domain name.

angular.element(document).ready(function () {
var domain;
if (Modernizr.localstorage) {
    domain = window.localStorage['com.example.domain'];
}
if (!domain) {
    domain = window.prompt("Enter your customer domain:\n\nNote: This has to be entered only once.", "example.com");
    if (domain && Modernizr.localstorage) {
        window.localStorage['com.example.domain'] = domain;
    }
}

If I run my test with safari, I can close this prompt once, login using Keycloak and every other test runs succesfully. But in chrome this prompt opens everytime running the test.

How can I get Chrome to close this prompt and continue to the login page?

I tried

 browser.wait(EC.alertIsPresent()).then(function () {
        browser.driver.switchTo().alert().then(function (alert) {
            alert.accept();
        });
    });

or

 browser.wait(EC.alertIsPresent()).then(function () {
        browser.driver.switchTo().alert().then(function (alert) {
            alert.accept();
        });
    });

But I still get the unexpected alert error


Solution

  • I'm not sure what version of protractor you are running but ever since 2.0 the time on browser.waits have changed

    "Due to changes in WebDriverJS, wait without a timeout will now default to waiting for 0 ms instead of waiting indefinitely."

    https://github.com/angular/protractor/blob/master/CHANGELOG.md#breaking-changes

    you might be able to try it without a .then() as below

    browser.wait(EC.alertIsPresent(), 5000);
    browser.switchTo().alert().accept();
    

    also I noticed you mention trying two different things but I believe you made a typo because they are both the same.

    I tried

     browser.wait(EC.alertIsPresent()).then(function () {
            browser.driver.switchTo().alert().then(function (alert) {
                alert.accept();
            });
        }); 
    

    or

     browser.wait(EC.alertIsPresent()).then(function () {
            browser.driver.switchTo().alert().then(function (alert) {
                alert.accept();
            });
        }); 
    

    But I still get the unexpected alert error

    And you mention closing the prompt once in Safari, Is this manually or via the test? If it's via the test, what code are you using to do that