protractor icon indicating copy to clipboard operation
protractor copied to clipboard

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

Open harin10 opened this issue 6 years ago • 30 comments

My code runs like this : ActivationData - array of test data I want to run the describe for each array element and then I wanna to clear the catch and cookies (restart ) then I need to run the same describe for second array element.

I Have used restartBrowserBetweenTests: true, and also I tried to restart in afterEach block. but I still see the below error for each array element that means if I have an array of size 3 then I see the 3 same errors.

If I remove the restartBrowserBetweenTests or afterEach/beforeEach block. there wont be any errors. and I have also made this config browser.ignoreSynchronization = true;

drive(activationData, function (inputActivationData) { describe('Drive Spec', function () {

        beforeEach(function () {
            console.log("restarting");
            browser.restart();
            console.log("complete");
        });
        
        it('Copying activation test data to session', function (done) {
            sessionData.netten.esnPartNumber = inputActivationData.PartNumber;
            sessionData.netten.simPartNumber = inputActivationData.SIM;
            sessionData.netten.zip = inputActivationData.ZipCode;
            sessionData.netten.pinPartNumber = inputActivationData.PIN;
            console.log(sessionData.netten.esnPartNumber);
            console.log(sessionData.netten.simPartNumber);
            console.log(sessionData.netten.zip);
            console.log(sessionData.netten.pinPartNumber);
            done();
        });
        FlowUtil.run('LANDINGPAGE_INTI_SCENARIO');
    
    }).result.data = inputActivationData;

});

error_code.txt

harin10 avatar May 16 '18 20:05 harin10

@harin10, I don't immediately see the same scenario in your sample as I have seen in our code when it failed for us with restarting browsers in between tests, but what I can say is that this same error for us was resolved by ensuring that PageObjects, etc. were properly re-initialized before each test. Our first test always passed, but all subsequent tests failed because we used previously-initialized objects with elements that were acquired for the first browser instance, which then got intentionally discarded after the first test completed. In other words, make sure you re-acquire each element you need for your test.

Hth,

Simon

simonua avatar May 17 '18 01:05 simonua

I am also having the same issue. My first test is running successfully, there after all other tests were failing with the same error. @simonua

vambcool avatar Jul 16 '18 20:07 vambcool

Any update on this issue?

Setting restartBrowserBetweenTests to true or false doesn't seem to be helping while running multiple test cases together. ONly first test case is running and later all other test cases are failing with error: " NoSuchSessionError: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used."

Protractor version: 5.3.2

sdjnaik avatar Aug 16 '18 09:08 sdjnaik

@simonua Hello! I have the same problem, but how can we make sure that we re-acquire each element?

kav3109 avatar Mar 13 '19 17:03 kav3109

Hi @kav3109,

This approach is what I ended up doing, which has worked very successfully and reliably:

beforeEach(() => {
    //Ensure that we initialize the page object before every test as we otherwise hold references to old browser instances, which fails when restartBrowserBetweenTests is true.
    loginPage = new PageObjects.Login();
});

simonua avatar Mar 13 '19 17:03 simonua

Thank you very much!!!

kav3109 avatar Mar 13 '19 18:03 kav3109

@kav3109, you're welcome. If you have more questions, I may be able to give you more code but I'd have to abstract it quite a bit from our (private) implementation.

simonua avatar Mar 13 '19 18:03 simonua

@simonua, if you give me more code I'll be grateful. I'll try to adapt my code. Thanks!

kav3109 avatar Mar 14 '19 10:03 kav3109

@kav3109, this is really it in a nutshell. Make sure you reinitialize all of your page objects for every test run with a beforeEach. This ensures that the ElementFinder properties are using the current browser session. Can you try a very simple example with one page object, one or two ElementFinder properties, restartBrowserBetweenTests set to true, and two independent tests, please?

simonua avatar Mar 14 '19 12:03 simonua

i don't know why, but it isn't reproduced when i try to write example. I need to investigate issue in my project. Thanks for help! :+1:

kav3109 avatar Mar 14 '19 20:03 kav3109

@kav3109, you're welcome. If you wrote an example based on what I suggested, I would not be surprised for it to work. Remember that you have to reinitialize all page objects as even just one ElementFinder that is tied to an old browser session ID can trip it. That took me a while to understand and identify in our solution.

simonua avatar Mar 15 '19 10:03 simonua

@simonua Hello! What about step definitions written with typescript and using browser methods? They should be reinitiated too? If yes - how can I do that?

EugeneSnihovsky avatar Mar 15 '19 19:03 EugeneSnihovsky

@EugeneSnihovsky, my apologies, but I am not familiar with cucumber.

simonua avatar Mar 18 '19 11:03 simonua

Hi I am facing same issue Failed: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used. i am trying to use restartBrowserBetweenTests: true(configuration file) and calling my login() function in beforeEach() but sill facing this issue . Please provide your suggestion

vijayjdk avatar Apr 01 '19 13:04 vijayjdk

@kav3109, this is really it in a nutshell. Make sure you reinitialize all of your page objects for every test run with a beforeEach. This ensures that the ElementFinder properties are using the current browser session. Can you try a very simple example with one page object, one or two ElementFinder properties, restartBrowserBetweenTests set to true, and two independent tests, please?

It actually looks like protractor bug, and probably can be fixed on ElementFinder level easily just with regular try/catch...

Xotabu4 avatar Jun 04 '19 14:06 Xotabu4

This issue is still reproducible. Node version: 12.10.0 Protractor version: 5.4.2 Webdriver version: 6.11.3

amercehic avatar Oct 10 '19 12:10 amercehic

I am facing this issue when I am using browser.restart() before a particular test. My scenario is I want to login as one user, do something, then do browser.restart() & login as another user. I can the login page getting opened after restart but it does not enter the user's credentials and everything fails Please help!

Areeba-Akhtar avatar Nov 28 '19 05:11 Areeba-Akhtar

@Areeba-Akhtar Besides the problem solutions above you could configure sharding in your browser capabilities:

capabilities: {
    'browserName': 'chrome',
    shardTestFiles: true,
    maxInstances: 1,
    ...
}

For each set of tests with a different user you would now have to create seperate files and set the login in a beforeAll which runs once for all tests.

pirminrehm avatar Nov 28 '19 06:11 pirminrehm

oneSpec.js file:

beforeAll(function () { browser.get("www.google.com"); });

twoSpec.js file:

beforeAll(function () { browser.get("www.yahoo.com"); });

oneSpec.js file is executed and getting below error when twoSpec.js file: start executing: please help

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

chandrasekhaer avatar Mar 02 '20 11:03 chandrasekhaer

Hi @kav3109,

This approach is what I ended up doing, which has worked very successfully and reliably:

beforeEach(() => {
    //Ensure that we initialize the page object before every test as we otherwise hold references to old browser instances, which fails when restartBrowserBetweenTests is true.
    loginPage = new PageObjects.Login();
});

Hi @simonua,

Can you please help in resolving this:

My Page Objects file looks like :

let loginPage = function(){

this.loginPageText = element(by.xpath("//h1[contains(text(),'Welcome to Crowe Connect')]")); this.termsLink = element(by.css('#abcs')); this.disclosureLink = element(by.css('#abcs')); this.croweLink = element(by.css('#abcs')); this.userName = element(by.css('#username')); this.passWord = element(by.css('#password')); this.loginButton = element(by.css('#login')); };

module.exports = new loginPage();

How can I re-initiate these objects in before each method to avoid - 'Failed: This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.'

Thanks!

mchopra1004 avatar Jul 30 '20 16:07 mchopra1004

Hi @mchopra1004, I suppose there are various ways to do this, but I would personally structure this a bit differently. Rather than exporting a new instance of the page object, I would contain this in a class, for example, to export (or export default ):

export class LoginPage {
   // Define elements here
   ...

   // Define functions here
   ...
}

In your spec file import that class (import { LoginPage } from <path>) and declare a variable (let loginPage: LoginPage) right below the import. Then you can set up the page object fresh for each test like so:

beforeEach(() => {
   loginPage = new LoginPage();
});

Again, several ways to do this, but does this make sense?

simonua avatar Jul 30 '20 17:07 simonua

I'm experiencing a similar issue

NoSuchSessionError: invalid session id

FrancescoBorzi avatar Jul 30 '20 21:07 FrancescoBorzi

Hi @mchopra1004, I suppose there are various ways to do this, but I would personally structure this a bit differently. Rather than exporting a new instance of the page object, I would contain this in a class, for example, to export (or export default ):

export class LoginPage {
   // Define elements here
   ...

   // Define functions here
   ...
}

In your spec file import that class (import { LoginPage } from <path>) and declare a variable (let loginPage: LoginPage) right below the import. Then you can set up the page object fresh for each test like so:

beforeEach(() => {
   loginPage = new LoginPage();
});

Again, several ways to do this, but does this make sense?

Thanks @simonua. The response you posted is in type script. I need that in plain javascript if you can help. I am pasting my spec file.

var loginpage = require('../pages/loginPage') var ReusableFunctions = require('../Utilities/ReusableFunctions')

describe('Test Login page functionality', function(){

beforeEach(function (){ ReusableFunctions.launchBrowser('https://abcd.com');

});

In before each how can I re-initiate page objects for loginPage and ReusableFunctions. Thanks!

mchopra1004 avatar Jul 31 '20 08:07 mchopra1004

Hi @mchopra1004,

This may get you closer then, but if that's not entirely right, see if you can get there from here, please. Also, if you can use post-ES5 syntax, consider changing to that.

var loginPage = require('../pages/loginPage')
var login;

describe('Test Login page functionality', function() {
   beforeEach(function() {
      login = new loginPage.default();
   };
});

simonua avatar Jul 31 '20 11:07 simonua

Hi @mchopra1004,

This may get you closer then, but if that's not entirely right, see if you can get there from here, please. Also, if you can use post-ES5 syntax, consider changing to that.

var loginPage = require('../pages/loginPage')
var login;

describe('Test Login page functionality', function() {
   beforeEach(function() {
      login = new loginPage.default();
   };
});

Thanks @simonua for the help, but now its giving 'loginpage.default is not a constructor' error

mchopra1004 avatar Jul 31 '20 11:07 mchopra1004

I'm sorry, I was thinking about my setup which exports a default class when I attempted to translate to what you have. It's early here. =P

I'm a bit rusty here, but can you just "new it up"? login = new loginPage();?

simonua avatar Jul 31 '20 11:07 simonua

I'm a bit rusty here, but can you just "new it up"? login = new loginPage();?

login = new loginpage(); gives "Failed: loginpage is not a constructor"

mchopra1004 avatar Jul 31 '20 11:07 mchopra1004

Hi @mchopra1004,

I'm not sure I'm much more help to you then, unfortunately. In my case, I just used a class (that's ES6+, not just TypeScript) and created a new instance of that class before every test run. I'm sorry, but this is where I need to bow out specifically to the way you are trying to do. Perhaps someone else can offer more advice. Good luck!

simonua avatar Jul 31 '20 11:07 simonua

Hello Sir @simonua , Im getting below error when i add before each in step.ts file error: TypeError: beforeEach is not a function at Object. (C:\work_Repo\myframework\javascript\steps\steps.js:47:1)

Code snippet import { Finder } from "../pages/launch"; let finder : Finder; beforeEach(function() { finder = new Finder(); });

BilalMohammad5 avatar Mar 19 '21 06:03 BilalMohammad5

Hi, I had same issue and tried several methods to solve it but unfortunately got same issue even to create new object under beforeEach. I solved this with changing afterEach to after and under after execute driver.quit() ... So my 2 tests will run and only after last test season will quit.

spyglass24 avatar Mar 11 '22 19:03 spyglass24