Aman Gupta
Aman Gupta

Reputation: 3797

Protractor: cleanup after test suite is finished

In my "conf.js" test suites are arranged as follows(using saucelab's webdriver):

suites: {
    abc: './abc/spec.js',
    xyz: './xyz/spec.js',
    pqr: './pqr/spec.js'
},

The problem with above arrangement is if one of the alert box/window unexpectedly appears in one of the test suite,test suites after that particular suite suffer and start failing.

Is there an in-built way in protractor to close all windows/alert box etc. when a test suite is finished or it can only be handled manually?

Upvotes: 1

Views: 1410

Answers (2)

Aman Gupta
Aman Gupta

Reputation: 3797

Here is the function I have written which handles multiple windows and unexpected alerts.It does not closes the main window as it is required in the next test suite.

this.cleanUp = function(){
        browser.driver.getAllWindowHandles().then(function(handles){
            for(var i=handles.length-1;i>-1;i--){
                browser.switchTo().window(handles[i]);
                browser.switchTo().alert().then(function(alert){
                    alert.dismiss();
                },function(err){});
                if(i)
                    browser.close();
            }
        },function(err){});
    }; 

Upvotes: 0

alecxe
alecxe

Reputation: 473903

From what I understand, there is no place in protractor to provide "before test suite" or "after test suite" logic (correct me if I'm wrong about it).

The idea is to use afterEach(), try switching to the alert, dismiss() it if exists (or accept() depending on what you need), do nothing if does not exist:

describe("My test", function () {
    afterEach(function () {
        browser.switchTo().alert().then(
            function (alert) { 
                alert.dismiss(); 
            },
            function (err) {}
        );
    });

    it("Test smth", function () {
        // ...
    });
});

See also:

Upvotes: 1

Related Questions