Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to, so to speak, restart or close browser after every protractor -spec test

I am implementing Protractor test for a web app. I have done some google searching but I have come up with zip, I want to every spec that I create to close the browser after it has ran all of the test in that specific spec file and then continue on to the next -spec file, etc. I've things such as using "beforeAll" and "afterAll" but Jasmine doesn't recognize these methods. A point in the right direction would be awesome!

describe('i will put something more meaningful here later :)', function () {

//not sure if this method actually exist in Jasmine
afterAll(function () {
   //restart browser or something of the nature
});

it('should do stuff', function () {

});

it('do stuff', function () {

});

});

browser should then close, and then open back up to run the next spec.

like image 833
Joseph Freeman Avatar asked Mar 11 '15 15:03

Joseph Freeman


People also ask

How do I restart my Protractor browser?

Restart the browser. This is done by closing this browser instance and creating a new one. A promise resolving to the new instance is returned, and if this function was called on the global browser instance then Protractor will automatically overwrite the global browser variable.

How do you close a tab on a Protractor?

window(handles[1]); browser. driver. close();


2 Answers

In protractor.conf.js:

capabilities:{
    'shardTestFiles': true,
    'maxInstances': 1
}

This will open and close the browser with each .spec file, but you may lose some of the reporting capabilities from standard plugins. If shardTestFiles is false, it will open the browser, run onPrepare, run all tests serially, then close the browser.

like image 123
martin770 Avatar answered Nov 15 '22 14:11

martin770


Speaking about restarting browser between tests, there is a relevant configuration option:

// If true, protractor will restart the browser between each test.
// CAUTION: This will cause your tests to slow down drastically.
restartBrowserBetweenTests: false,

Set it to true.

FYI, Here is the initial feature request:

  • Feature Request: to have an option to restart new browser session between each test case/scenario

beforeAll and afterAll are built into jasmine-2.x. To make them work, you need to set jasmine2 as a testing framework in the protractor config:

exports.config = {
    ...
    framework: 'jasmine2',
    ...
}

For jasmine-1.x, there is a third-party jasmine-beforeAll package that provides the same exact functionality.

like image 25
alecxe Avatar answered Nov 15 '22 12:11

alecxe