nhrcpt
nhrcpt

Reputation: 872

Protractor : How to detect if an unexpected alert is open

In one of our tests, sometimes we get the browser alert message when the user is trying to log out.

enter image description here

I do not want the tests failed due to these alerts. We are getting the below error when there is an alert:

An error was thrown in an afterAll
AfterAll Failed: unexpected alert open: {Alert text : You have unsaved changes! If you leave, your changes will be lost.}

I have tried to resolve it by 1. added unexpectedAlertBehaviour: 'accept' to the conf.js file, which failed, and 2, modifying the code using an if/else block as follows:

exports.logOutfromESY =function(){

    var G = GV;
    var EC = protractor.ExpectedConditions;
    expect(G.User_Menu_Dropdown.isPresent()).toBeTruthy();
    G.User_Menu_Dropdown.click();
    browser.wait(EC.presenceOf(G.logOut_Button), 2000, 'The Logout Buttons taking too long to appear in the DOM');
    G.logOut_Button.click();
    browser.sleep(500);
    if(alert.isPresent()){
        browser.switchTo().alert().then(function (alert) {
            alert.accept();
    });
    }
    browser.wait(EC.presenceOf(G.Email_Input_box), 3000, 'The Login Page redirection taking long time');
    browser.sleep(500);

};

It did not work either. Note that the "alert" variable was my last ditch effort to make protractor to identify the alert.

Is there any way to achieve this?

Upvotes: 0

Views: 857

Answers (1)

Kacper
Kacper

Reputation: 1199

Seems that this solution may be helpful: https://stackoverflow.com/a/29873887/6331748

EDIT: You can also consider to use --disable-notifications flag.

You need to add it in your config file like that:

export const config = {
    capabilities: {
        chromeOptions: {
            args: ['--disable-notifications']
        }
    }
}

Upvotes: 1

Related Questions