protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Protractor is throwing an error when executing more than two tests with restartBrowserBetweenTests set to true

Open mm7163 opened this issue 11 years ago • 31 comments

I am using Protractor 2.0.0 and Chrome 41.0.2272.101.

config.js:

exports.config = {
    directConnect: true,
    specs: ['testCases/**.js'],
    allScriptsTimeout: 10000,
    restartBrowserBetweenTests: true,
    capabilities: {'browserName': 'chrome'},
    baseUrl: 'http://angularjs.org',
    jasmineNodeOpts: {showColors: true,isVerbose: true,includeStackTrace: true},onPrepare: function() {require('jasmine-reporters');jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter('./xmloutput', true, true));
    }
};

pageObjects/generalObjects.js:

var m2testWebGeneralObjects = function() {
    this.text1 = by.xpath('/html/body/div[1]/div[2]/div/div[2]/p');
};
module.exports = new m2testWebGeneralObjects();

testCases/test1.js (same as test2.js and test3.js):

var m2generalObjects = require('../pageObjects/generalObjects.js');

describe('Test Error', function() {
    var ptor;
    browser.driver.manage().window().maximize();

    beforeEach(function () {
        ptor = browser;
    });

    it('Should display correct content', function() {
        ptor.manage().deleteAllCookies();
        ptor.get(ptor.baseUrl);
        expect(element(m2generalObjects.text1).getText()).toContain('address');
    });
});

When I run protractor config.js, after the test1.js is executed I get following error message:

c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:392
      throw new Error('This driver instance does not have a valid session ID ' +

            ^
Error: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.
    at checkHasNotQuit (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:392:13)
    at flow.execute.then.value (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:365:5)
    at [object Object].promise.ControlFlow.runInFrame_ (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1877:20)
    at [object Object].promise.ControlFlow.runEventLoop_ (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1755:8)
    at [object Object].<anonymous> (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:2056:12)
    at [object Object].goog.async.run.processWorkQueue [as _onTimeout] (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:125:21)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)From: Task: WebDriver.manage().window().maximize()
    at [object Object].webdriver.WebDriver.schedule (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:360:15)
    at [object Object].webdriver.WebDriver.Window.maximize (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:1474:23)
    at [object Object].<anonymous> (d:\protractor\protractorIssue\testCases\test3.js:6:38)
    at [object Object].jasmine.Env.describe_ (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:913:21)
    at [object Object].jasmine.Env.describe (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:898:15)
    at describe (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:658:27)
    at Object.<anonymous> (d:\protractor\protractorIssue\testCases\test3.js:4:1)

    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
From: Task: WebDriver.manage().timeouts().setScriptTimeout(10000)
    at [object Object].webdriver.WebDriver.schedule (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:360:15)
    at [object Object].webdriver.WebDriver.Timeouts.setScriptTimeout (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:1367:23)
    at [object Object].Runner.createBrowser (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\lib\runner.js:181:30)
    at c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\lib\runner.js:257:21
    at _fulfilled (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\q\q.js:797:54)
    at self.promiseDispatch.done (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\q\q.js:826:30)
    at Promise.promise.promiseDispatch (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\q\q.js:759:13)
    at c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\q\q.js:573:44
    at flush (c:\Users\pcuser\AppData\Roaming\npm\node_modules\protractor\node_modules\q\q.js:108:17)
    at process._tickCallback (node.js:419:13)
[launcher] Process exited with error code 1

For only two files, everything works fine. For 3 files or more the error is thrown.

mm7163 avatar Apr 02 '15 15:04 mm7163

I cannot reproduce this. Testing with 3 files. Can you reproduce reliably? Maybe this is a windows bug, has anyone else seen it?

juliemr avatar Apr 02 '15 18:04 juliemr

I can reproduce with two different test sets (first one is the one described above), on two different environments (although, both windows 7 sp1 x64). I uploaded my test set here https://github.com/mm7163/protractorRBBTIssue Please let me know if you need any additional details.

mm7163 avatar Apr 03 '15 07:04 mm7163

+1 using Jasmine 2.2, Selenium 2.45.0, and Protractor 1.8.0

cjroth avatar Apr 03 '15 19:04 cjroth

Hmm Maybe a windows issue?

juliemr avatar Apr 03 '15 19:04 juliemr

I reproed this on MacOS Yosemite in chromedriver_2.15, selenium 2.45, protractor 2.1.0. To avoid this error, you must remove all JS code from the describe block that is not part of a function declaration, and do not use any variables in your it() tests that you declared inside the "beforeEach" block.

I posted a refactored example at this fork https://github.com/tmulry/protractorRBBTIssue

FWIW, this error happens when I run tests locally or via SauceLabs.

If you run protractor config.js from that repo, they will all pass. Here is a sample refactor of test1.js (same goes for 2 and 3):

var m2generalObjects = require('../pageObjects/generalObjects.js');

describe('Test Error', function() {

    beforeEach(function () {
      browser.driver.manage().window().maximize();
    });

    it('Should display correct content', function() {
        var ptor;
        ptor = browser;
        ptor.manage().deleteAllCookies();
        ptor.get(ptor.baseUrl);
        expect(element(m2generalObjects.text1).getText()).toContain('address');
    });
});

tmulry avatar Jun 05 '15 17:06 tmulry

I had this issue too using cucumber-js. I found that I needed to make sure that will every step function I included a callback and ended the step by calling the callback.

this.Then(/^I should see something$/, function(){ ... }

becomes

this.Then(/^I should see something$/, function(done){ ...; done(); }

chris31389 avatar Jul 06 '15 08:07 chris31389

I have also reproduced this error on ubuntu

sjelin avatar Jul 07 '15 21:07 sjelin

I am facing this issue with OS x Yosemite too. After running the first test, a fresh browser session is not getting started. Tried the solution given by @tmulry but no result.

DevTestOpsCoach avatar Jul 29 '15 19:07 DevTestOpsCoach

The solution given by @tmulry worked for me. Thanks!

mm7163 avatar Jul 30 '15 09:07 mm7163

Is there any update on this issue? Occurring for me as well

rfodge avatar Sep 27 '15 01:09 rfodge

I've had it happen to me while using Protractor 3.0.0 + CucumberJS 0.9.2 + Selenium Standalone 2.48.0 on Fedora 20. Would be nice to have an update on this...

calendir avatar Jan 19 '16 08:01 calendir

I was also struggling with this for ages - @tmulry your workaround got things going for me! Thanks.

s-kavanagh avatar Feb 05 '16 11:02 s-kavanagh

Any update on this? I can't get it working.

ghost avatar Mar 10 '16 19:03 ghost

Same issue here. I am using page-objects and i was trying to adopt @tmulry `s solution, but it still fails locally and on browserstack:

var helper = require('./helpers/webdriver_helper.js');
var myMainPage= require('./pageobjects/my_page.js');

var myTestFlow = function () {
myMainPage
    .navigate()
    .search(prodInfo.isbn);
}
describe('my site', function () {

  it('should go through flow in ".col-xs-$ - Extra Small" size', function (done) {
    helper.setDriverSize('S');
    myTestFlow ();
    done();
  });

  it('should go through flow in ".col-sm-$ - Small Devices" size', function (done) {
    helper.setDriverSize('M');
    myTestFlow ();
    done();
  });

  afterEach(function (done) {
    browser.driver.manage()
      .deleteAllCookies()
      .then(done);
  });
});

Am I missing something?

Feels like it fails to assign new value for browser.driver, so it should be easy to fix, right?

pavlomrv avatar Apr 04 '16 11:04 pavlomrv

Same problem here.

Selenium v2.52.0, with Core v2.52.0. Built from revision 4c2593c Driver info: driver.version: RemoteWebDriver OS: Windows 10 10.0 amd64 Java: Oracle Corporation 25.91-b14 chromedriver_2.21 and chromedriver_2.15.322448 seem to act the same way CEF 3.2454.1344.g2782fb8 Chromium 45.0.2454.101 WebKit 537.36 JavaScript 4.5.103.35

leegee avatar May 11 '16 10:05 leegee

I had the same error when using element.all in a page object file. The error was thrown when instantiating the page object once and using the tabs promise in more than one test. Fixed the issue by instantiating the tabs promise in the before each instead of in the page object file.

somePage.pageObjects.js

var OtherObject = require(global.Root + '/Components/otherObject.js');

var pageObjects = function(){ this.get = function(){ browser.get(browser.baseUrl + "/en-CA/testPageURL"); }

//Line causing the issue
this.OtherObject = new OtherObject();

//Line fixing the issue
this.OtherObject = OtherObject;

}

module.exports = new pageObjects();


otherObject.js

var OtherObject = function(){ this.tabs = element(by.css(".mediazoneTabs")).all(by.repeater("tab in $context.tabs")); }

module.exports = OtherObject;


somePage.spec.js

var pageObjects = require('../somePage.pageObjects.js');

//Line causing the issue var OtherObject = pageObjects.OtherObject;

//Fix part 1: declare variable before var OtherObject;

describe('When app is loaded', function () {

beforeEach(function () {        
    //Fix part 2: instantiate object in beforeEach
    OtherObject = new pageObjects.OtherObject();

    //Get the page
    pageObjects.get();
});

it('should contains 3 tabs', function () {
    expect(OtherObject.tabs.count()).toBe(3);
});

it('Should have photo tab selected', function(){
    expect(OtherObject.tabs.first().getAttribute("class")).toContain("selected");
});

});


MathieuMathurin avatar May 16 '16 20:05 MathieuMathurin

Same here if i want to restart the browser, only for a suit

beforeAll(function () {
     browser.restart();
     browser.controlFlow().execute(...); //works fine;
})

it('', function () { 
      browser.controlFlow().execute(...); //throws error;
})

holdfenytolvaj avatar Jun 04 '16 02:06 holdfenytolvaj

I am experiencing the same here.

  • Node Version: 4.3.2
  • Protractor Version: 3.2.2
  • Browser(s): Chrome
  • Operating System and Version openSUSE 13.2 (Harlequin) (x86_64) 64-bit

pedroscaff avatar Jun 22 '16 13:06 pedroscaff

Sorry for the delays here. On the radar!

juliemr avatar Jul 14 '16 17:07 juliemr

Same issue for me, on Ubuntu

/path/to/app/node_modules/selenium-webdriver/error.js:27
    super(opt_error);
    ^
UnsupportedOperationError: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.
    at WebDriverError (/path/to/app/node_modules/selenium-webdriver/error.js:27:5)
    at UnsupportedOperationError (/path/to/app/node_modules/selenium-webdriver/error.js:436:5)
    at checkHasNotQuit (/path/to/app/node_modules/selenium-webdriver/lib/webdriver.js:378:15)
    at WebDriver.schedule (/path/to/app/node_modules/selenium-webdriver/lib/webdriver.js:323:5)
    at WebDriver.findElements (/path/to/app/node_modules/selenium-webdriver/lib/webdriver.js:897:22)
    at /path/to/app/node_modules/protractor/built/element.js:141:44
    at Promise.invokeCallback_ (/path/to/app/node_modules/selenium-webdriver/lib/promise.js:1329:14)
    at TaskQueue.execute_ (/path/to/app/node_modules/selenium-webdriver/lib/promise.js:2790:14)
    at TaskQueue.executeNext_ (/path/to/app/node_modules/selenium-webdriver/lib/promise.js:2773:21)
    at asyncRun (/path/to/app/node_modules/selenium-webdriver/lib/promise.js:2652:27)
    at /path/to/app/node_modules/selenium-webdriver/lib/promise.js:639:7
14:02 $ node --version
v6.3.0

14:06 $ npm --version
3.10.5

14:06 $ node_modules/.bin/protractor --version
Version 3.3.0

14:06 $ uname -a
Linux ubuntu02 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

dogmatic69 avatar Jul 19 '16 13:07 dogmatic69