Protractor: browser.restart() opens browser one more time then number of test

Created on 20 Jun 2016  路  7Comments  路  Source: angular/protractor

I have tested by using protractor 3.2 and jasmine 2 with chrome browser having conf file

Conf.js
exports.config = {
seleniumServerJar:'../node_modules/protractor/selenium/selenium-server-standalone-2.53.0.jar',
chromeDriver: '../node_modules/protractor/selenium/chromedriver.exe',
allScriptsTimeout: 150000,
capabilities: {
browserName: 'chrome',
browser_version: '50.0',
},
restartBrowserBetweenTests: true,
framework: 'jasmine2',
specs:['../Specs/Spec.js'],
onPrepare: function() {

    },

jasmineNodeOpts: {
defaultTimeoutInterval: 300000,
onComplete: null,
showColors: true,
includeStackTrace: true,
}
}

when i try to run with spec having 2 it test case then restart browser open browser 3 times instead of 2 times.

Spec.js
describe('Testing', function() {
beforeEach(function() {
browser.get('http://juliemr.github.io/protractor-demo/');
});
fit('should have a title', function() {
expect(browser.getTitle()).toEqual('Super Calculator');
});

  fit('should add one and two', function() {
        element(by.model('first')).sendKeys(1);
        element(by.model('second')).sendKeys(2);
        element(by.id('gobutton')).click();
        expect(element(by.binding('latest')).getText()).
            toEqual('5'); // This is wrong!
      });

});

Most helpful comment

@cnishina , i am getting below error with changing the 'restartBrowserBetweenTests: true' with browser = browser.forkNewDriverInstance(); inside before each method
image

All 7 comments

@Saurabh06 Let me just modify it so that people could understand the issue easily :
following is the config file:

exports.config = {
    directConnect: 'true'
    , allScriptsTimeout: 15000
    , capabilities: {
        browserName: 'chrome'

    }
    , restartBrowserBetweenTests: true
    , framework: 'jasmine2'
    , specs: ['./spec.js']
    , onPrepare: function () {
        browser.ignoreSynchronization = true;
        browser.manage().window().maximize();
    }
    , jasmineNodeOpts: {
        defaultTimeoutInterval: 30000
        , onComplete: null
        , showColors: true
        , includeStackTrace: true
    }
}

and the test spec is as follows :

describe('testing', function () {
    beforeEach(function () {
        browser.get('http://juliemr.github.io/protractor-demo/');
    })

    afterEach(function () {
        browser.close();
    })
    fit('shoould have a title', function () {
        expect(browser.getTitle()).toEqual('Super Calculator');
    });

    fit('shoould add one and two ', function () {
        element(by.model('first')).sendKeys(1);
        element(by.model('second')).sendKeys(2);
        element(by.id('gobutton')).click();

        var el = element(by.binding('latest'));
        expect(el.getText()).toEqual('3');
    });
});

The issue is when the "restartBrowserBetweenTests: true" and the spec.js has 1 describe and 2 it blocks ideally 2 times the browser should be restarted but in this case 3 times the browser is restarted!
I gave it a try and I able to reproduce this issue.
@cnishina @NickTomlin Any thoughts on this? What am I & @Saurabh06 missing here?

So when setting restartBrowserBetweenTests to true, it is calling restart at the end of the test, afterEach, and afterAll. In this case, it should restart the browser after calling afterEach. So if you have a single test, it will restart the regardless if there is an additional test after it.

describe('testing', function () {
  beforeEach(function () {
    console.log('beforeEach: browser.get');
    browser.get('http://juliemr.github.io/protractor-demo/');
  });
  afterEach(function() {
    console.log('afterEach: close browser');
    browser.close();
  });
  it('should have a title', function () {
    console.log('it: should have a title');
    expect(browser.getTitle()).toEqual('Super Calculator');
  });
});

editing runner.js at line 215 (to see when restart occurs):

        browser_.restart = function () {
          console.log('restarting!');
            // Note: because tests are not paused at this point, any async
            // calls here are not guaranteed to complete before the tests resume.
            _this.driverprovider_.quitDriver(browser_.driver);
            // Copy mock modules, but do not navigate to previous URL.
            browser_ = browser_.forkNewDriverInstance(false, true);
            _this.setupGlobals_(browser_);
        };

taking a look at the console output we can see where restart is actually happening:

Started
beforeEach: browser.get
it: should have a title
afterEach: close browser
restarting!

Since the browser is restarting, the afterEach is not needed since it already closes and opens the browser window.

If you are concerned about having this extra browser window restarting, consider the following instead. The following test does not use the restartBrowserBetweenTests in the config and will create a new driver instance from the original. This will launch a new browser window and should have as many browsers opened as you have tests.

describe('testing', function () {
  var firstTest = true;
  beforeEach(function () {
    console.log('beforeEach: browser.get');
    if (firstTest) {
      firstTest = !firstTest;
    } else {
      browser = browser.forkNewDriverInstance();
    }
    browser.get('http://juliemr.github.io/protractor-demo/');
  });
  afterEach(function() {
    console.log('afterEach: close browser');
    browser.close();
  });
  it('should have a title', function () {
    console.log('it: should have a title');
    expect(browser.getTitle()).toEqual('Super Calculator');
  });
  it('shoould add one and two ', function () {
    element(by.model('first')).sendKeys(1);
    element(by.model('second')).sendKeys(2);
    element(by.id('gobutton')).click();

    var el = element(by.binding('latest'));
    expect(el.getText()).toEqual('3');
  });
});

I believe this is as designed and will close this issue.

@cnishina , i am getting below error with changing the 'restartBrowserBetweenTests: true' with browser = browser.forkNewDriverInstance(); inside before each method
image

browser = browser.forkNewDriverInstance(); i
An error was thrown in an afterAll
AfterAll WebDriverError: no such session
(Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.12.4 x86_64)

any solution ?

any solution for AfterAll WebDriverError: no such session
(Driver info: chromedriver=2.31.488774 (7e15618d1bf16df8bf0ecf2914ed1964a387ba0b),platform=Mac OS X 10.12.4 x86_64)

@poojagadekar89 : Instead of browser.forNewDriverInstance(), use browser.restart and remove browser.close from afterEach block.

Hi @cnishina , It would be very grateful if you help me in this
Query is related to Protractor

//==================================================================================================================
// Every test case structure

describe("Login Suite", () => {
beforeEach(async function () {

//browser instance initialization & application navigation is happening here

})

it('Test case name', async function () {
    try {

    // Include test steps       

    } catch (e) {
        browser.logger.info("Test SWM-2019065 failed with exception: " + e)
        expect(true).toBe(false, e)
    }
})

afterEach(async function () {
    //await browser.restart();
or
//await browser.close();

})

})

//===================================================================================================================

//Programming language used is javascript (promises flow disabled) : using async await to disable the asynchronous nature of javascript

My every test case in test suite has strcuture like above. Now, How can i want every test case belonging to test suite to be run in a sequence following the trend like

  1. Opening of browser instance for a first test case & running the whole test case which is happening
  2. then closing the above instance of first test case irrespective of the failure/success run.
  3. Now opening the new browser instance for 2nd test case-->>running whole test case--->>closing the browser instance of this particular test case--->>
  4. Now opening the new browser instance for 3rd test case-->>running whole test case--->>closing the browser instance of this particular test case--->>

Is this advisable to run the test suite in this way ? If yes, then how should i manage with the commands like browser.close() or whatever your approach is. I have tried this but getting such errors like "AfterAll Failed: invalid session id" . Kindly provide your inputs.

Was this page helpful?
0 / 5 - 0 ratings