Andriy Simonov
Andriy Simonov

Reputation: 1288

Protractor does not close browser when a server is unavailable

I do e2e testing with protractor. When my application is running on a server the tests pass fine and a browser is closed afterward, however when the server is down the browser remains (Firefox, Chrome). It is OK on my local machine but I am experimenting with running tests on remote CI machine and afraid that the tests can spawn multiple browser windows if the server fails to run. How to make sure protractor closes the browser window in such situation?

Here is my configuration:

exports.config = {
    allScriptsTimeout: 11000,
    specs: [
            '../test/e2e/*.js'
    ],
    capabilities: {
        'browserName': 'firefox'
    },
    baseUrl: 'http://localhost:8000/',
    framework: 'jasmine',
    jasmineNodeOpts: {
        defaultTimeoutInterval: 30000
    }
};

and the test is:

'use strict';

describe('typicaClient', function() {
    browser.get('index.html');

    it('test', function() {
        expect('str').toMatch('str');
    });
});

Upvotes: 1

Views: 726

Answers (1)

AdityaReddy
AdityaReddy

Reputation: 3645

Protractor leaves browser open on those errors which it doesn't recognize and cannot handle.

Check out the example below. This error thrown by spawn() process is unhandled by Protractor and it exits with an error code - 199

describe('sample test', function(){
    it('Sample Check', function(){
        browser.get("http://www.protractortest.org/#/");
        browser.sleep(5000);
        var terminal = require('child_process').spawn('34e3545')
    });
});

Output:

[18:26:46] I/local - Starting selenium standalone server...
[18:26:46] I/launcher - Running 1 instances of WebDriver
[18:26:46] I/local - Selenium standalone server started at http://192.168.1.5:61146/wd/hub
Started
[18:26:49] E/launcher - spawn 34e3545 ENOENT
[18:26:49] E/launcher - Error: spawn 34e3545 ENOENT
    at exports._errnoException (util.js:873:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32)
    at onErrorNT (internal/child_process.js:344:16)
    at nextTickCallbackWith2Args (node.js:442:9)
    at process._tickCallback (node.js:356:17)
[18:26:49] E/launcher - Process exited with error code 199

To avoid these , handle gracefully the error at non-protractor commands

describe('sample test', function(){
    it('Sample Check', function(){
        browser.get("http://www.protractortest.org/#/");
        browser.sleep(5000);
        var terminal = require('child_process').spawn('34e3545').on('error', function(err) {
            fail('Test is failing because we provided an invalid process')
        });
    });
});

Output in this case:

[18:57:22] I/local - Starting selenium standalone server...
[18:57:22] I/launcher - Running 1 instances of WebDriver
[18:57:22] I/local - Selenium standalone server started at http://192.168.1.5:49867/wd/hub
Started
F

Failures:
1) sample test Sample Check
  Message:
    Failed: Test is failing because we provided an invalid process
  Stack:
    Error: Failed: Test is failing because we provided an invalid process
        at ChildProcess.<anonymous> (C:\Users\ayannam\WebstormProjects\demo\errorHandle.js:7:13)
        at emitOne (events.js:77:13)
        at ChildProcess.emit (events.js:169:7)
        at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)

1 spec, 1 failure
Finished in 6.582 seconds

See it this case Un-handled exception is handled and Protractor throws an error code - 1 which is gracefully handled

Upvotes: 2

Related Questions