Search code examples
javascriptseleniumtestingprotractorend-to-end

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?


Solution

  • 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: