protractor icon indicating copy to clipboard operation
protractor copied to clipboard

When using ExpectedConditions after browser.restart(), getting "Driver does not have valid session ID" error

Open heathkit opened this issue 9 years ago • 11 comments

From azachar (copied from #3677)

Hi There, I am getting a very similar issue still on the version 4.0.13, it happens sometimes...

the test opens the page and waits to have a certain part in the url using the function bellow, the tests are using browser.restart() in afterEach. Since I am using a random order of running, it happens only sometimes.

  this.waitForUrl = function(url, timeout) {
    timeout = timeout || DEFAULT_WAIT_FOR_TIMEOUT;
    return this.browser.wait(EC.urlContains(url), timeout, 'waiting url to contains ' + url);
  };

I am getting this error:


Failed: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.
NoSuchSessionError: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.
    at WebDriverError (/home/ubuntu/xxx/node_modules/selenium-webdriver/lib/error.js:27:5)
    at NoSuchSessionError (/home/ubuntu/xxx/node_modules/selenium-webdriver/lib/error.js:170:5)
    at checkHasNotQuit (/home/ubuntu/xxx/node_modules/selenium-webdriver/lib/webdriver.js:395:15)
    at Driver.schedule (/home/ubuntu/xxx/node_modules/selenium-webdriver/lib/webdriver.js:356:5)x
    at Driver.getCurrentUrl (/home/ubuntu/xxx/node_modules/selenium-webdriver/lib/webdriver.js:804:17)
    at /home/ubuntu/xxx/node_modules/protractor/built/expectedConditions.js:306:41

The screenshot from this failure clearly shows that the page is fully loaded and has its content so I guess this might be a bug. I guess that is related to this issue too.

Anyway, thank you for your help.

Best regards, Andrej

Here is the failing test case:

describe('This driver instance does not have a valid session ID', function() {
  var EC = protractor.ExpectedConditions;

  function waitForUrl(url) {
    browser.wait(EC.urlContains(url), 5000).then(function() {
      console.log(url + ' found');
    })
  }

  it('Browser wait for url fails', function() {
    browser.get("https://angularjs.org/");
    waitForUrl('angular');

    browser.restart();

    browser.get("https://angularjs.org/");
    //fails here // This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.
    waitForUrl('angular');
  });

  it('Browser ignoreSynchronization fails', function() {
    browser.get("https://angularjs.org/");
    browser.restart();
    browser.get("https://angularjs.org/");

    browser.ignoreSynchronization = true;
    browser.get("https://github.com/");
    waitForUrl('github'); //fails here This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.

    browser.ignoreSynchronization = false;

    browser.restart();

    browser.get("https://angularjs.org/");
    waitForUrl('angular');
  });

});

here is protractor.conf

{ 
  rootElement: 'html', //it fails also with [ng-app]
  allScriptsTimeout: 110000,
  baseUrl: 'http://localhost:9000',
  chromeOnly: true,
  directConnect: true,
  exclude: [],
  capabilities: { browserName: 'chrome' },
  framework: 'jasmine',
  jasmineNodeOpts: { defaultTimeoutInterval: 15000, random: false },
  specs: [ 'e2e/**/*.spec.*.js', 'e2e/**/*.spec.js' ] 
}

heathkit avatar Dec 27 '16 18:12 heathkit

The issue is that you shouldn't be saving a reference to ExpectedConditions as in

  var EC = protractor.ExpectedConditions;

If you set EC again after each restart call, that will fix your problem. But to be sure this is confusing. I made https://github.com/angular/protractor/issues/3884 to document a possible solution

sjelin avatar Dec 27 '16 22:12 sjelin

Agree. EC has a reference back to the browser object. If the browser object goes away, the EC is no longer valid after the session quits.

A work around, for now, would be to get the ExpectedConditions from the browser object:

  function waitForUrl(url) {
   var EC = browser.ExpectedConditions;
    browser.wait(EC.urlContains(url), 5000).then(function() {
      console.log(url + ' found');
    })
  }

cnishina avatar Dec 27 '16 22:12 cnishina

Does it make sense to have protractor.ExpectedConditions if people can run into this problem? Should we just make browser.ExpectedConditions the only official way to create ExpectedConditions?

heathkit avatar Dec 27 '16 23:12 heathkit

^^^ Maybe we could make this as a breaking change in beta since ExpectedConditions is no longer static. @juliemr thoughts?

cnishina avatar Dec 28 '16 00:12 cnishina

Thanks a lot for your investigation, perhaps this will resolve some issues I had to comment out. I am using chat a like app to test, so I do have many browser instances... I will come back to you if this helped.

azachar avatar Dec 30 '16 03:12 azachar

Is this Fixed?

mustafa-turab-ali avatar Jul 31 '17 13:07 mustafa-turab-ali

Problem still occurs. I need to restart browser between 2 specific test cases and I'm not able to due to this issue. After restart page loads but error appears: -

Failed: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.

schwarcu avatar Sep 04 '17 12:09 schwarcu

Hi, i have same problem with 'restartBrowserBetweenTests' parameter and 'browser.restart()'. I check selenium logs and i have test cases + 1 selenium session. And of course "This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used."

wieta avatar Sep 13 '17 09:09 wieta

@wieta after some study I came to the source of issue which I think is common for most of users. If you are using page object pattern and you initialize and define objects in the "desc" segment, or even in bigger scope these objects will always refer to the first browser instance.

To solve the issue, you must decache them (https://www.npmjs.com/package/decache) and then use "require" again and also agian initialize and define variables.

For my project I created a common utility function which always returns new Page Object. After I restart the browser I use my function to re-load all Page Objects and now it works perfect.

objects

So your code logic should go like this:

  1. Import Page objects module, execute some test cases
  2. Restart browser.
  3. Decache all Page object modules
  4. Import Page object modules again so all references to the browser are pointing to the new instance

My common utility function does steps 3 and 4.

Bare in mind to split data from PO files. If you want to pass data from one spec to another, create a module which does only that task, So you don't have to decache it too.

schwarcu avatar Sep 28 '17 13:09 schwarcu

Hi there!

I am facing a similar issue, when using browser.restart. In my case, a subsequent browser.wait call waits almost indefinitely, and times out after an obscene amount of time, although the page is loaded and visible the whole time.

I perform the restart operation in the Before hook of Cucumber:

Before(function(scenario) { let currFeatureName=scenario.sourceLocation.uri; console.log('feature : ',currFeatureName); if(featureName===null) featureName=currFeatureName; else if(currFeatureName!==featureName) return browser.restart(); });

Followed by a Given, where I perform a browser.get, followed by a browser.wait, before interacting with my element:

Given(/^user is logged in SNAPP$/,function(){ console.log('started : '); browser.ignoreSynchronization = true; browser.driver.get(browser.params.logon.url); browser.wait(until.visibilityOf(element(by.id('logonDialogLbl'))),1000,'waiting to load url'); logonpage.logon(); browser.sleep(2000);

It is seemingly getting stuck in the browser.wait call, and times out, after waiting for an enormous amount of time.

ash-lionell avatar Jan 25 '18 20:01 ash-lionell

same issue as mentioned above.

I am facing a similar issue, when using browser.restart. In my case, a subsequent browser.wait call waits almost indefinitely, and times out after an obscene amount of time, although the page is loaded and visible the whole time.

arjunUnniumbath avatar Feb 08 '18 07:02 arjunUnniumbath