4

I am using protractor-cucumber-framework, but when I try to restart I get

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

I tried both in cucumber and default protractor jasmine framework:

I am just calling

await browser.restart()

From beforeEach or before hook

My Spec:

'use strict';

let stage1 = require('../pageobjects/stage1.js');



describe('Validate stage 1 and 2 behaviour', function () {

    beforeEach(async function () {
        await browser.restart();
        await browser.waitForAngularEnabled(false);
        stage1 = new Object(require('../pageobjects/stage1.js'));
    });


    it('Validate that error message in all fields given uploaded {Regression} {Smoke} {Sanity}', async function () {
        await stage1.goto()
        await stage1.sendValue('hi')
    });


    it('Validate that s message in all fields given uploaded {Regression} {Smoke} {Sanity}', async function () {
        await stage1.goto()
        await stage1.sendValue('hi')
    });

});

My page Object:

'use strict';

let fileListActions = require('../commons/fileListActions');

module.exports = {


  elements: {

    textField: element(by.css('[name="q"]')),
  },

goto: async function() {
    let elements= this.elements;
    await browser.get('https://www.google.com/');
},


sendValue: async function(item,value) {
    let elements= this.elements;
    await elements['textField'].sendKeys('Hi')
},


};

My config:

'use strict';

exports.config = {

  onPrepare: function () {


    browser.waitForAngularEnabled(false);
  },
  restartBrowserBetweenTests: true,
  multiCapabilities: [{
    browserName: 'chrome',
  },

  ],

  specs: ['testsuites/1.js'],


  jasmineNodeOpts: {
    showColors: true,
    realtimeFailure: true
  },

  SELENIUM_PROMISE_MANAGER: false,
};
  • Do you use browser.close or browser.quit somewhere in your afterhooks? – Niels van Reijmersdal Jan 20 at 18:03
  • No, I use browser.restart() – Learner Jan 20 at 18:04
  • How are you accessing the elements ? are you using page object model? – PDHide Jan 20 at 18:19
  • yes, see the updated question – Learner Jan 20 at 18:21
3

You are trying to access element instance that was created using the previous browser instance. The workflow is as follow,

  1. You imported the page object instance at the start of the spec using require
  2. A browser instant is created in the onPrepare
  3. Using that instance your page object model gets the element object
  4. But on next 'It' the browser restarts but the page object instance remains the same
  5. So when you try to interact with the element, you are getting session ID of non-existing browser.

Solution:

As given in your code, you have your page object as a javascript property, than a function

So what you could do is, reinitiate the page object whenever you restart the browser.

This could be done using npm decahe module: https://www.npmjs.com/package/decache

So whenever, you restart the browser reinitiate the module using below command:

//import in top of the spec
var decache = require('decache');

//reinitiate where browser is restarted
decache('../pageobjects/stage1.js');
stage1 = require('../pageobjects/stage1.js');

So your final code:

'use strict';
var decache = require('decache');
let stage1 = require('../pageobjects/stage1.js');


describe('Validate stage 1 and 2 behaviour', function () {

    beforeEach(async function () {        
        await browser.waitForAngularEnabled(false);
        decache('../pageobjects/stage1.js');
         stage1 = require('../pageobjects/stage1.js');
    });


    it('Validate that error message in all fields given uploaded {Regression} {Smoke} {Sanity}', async function () {
        await stage1.goto()
        await stage1.sendValue('hi')
        await browser.sleep(5000)        
    });

Note:

This works even if the page object is defined as function

For protractor-cucumber add the same in before hook :

you should add the hook in the step definition file itself and not in separate hook.js:

 "use strict";

let {Given,Before} = require('cucumber');
let decache = require('decache');
let stage1 = require('../pageobjects/stage1.js');

Before(async function (scenario) {
  decache('../pageobjects/stage1.js');
  stage1 = require('../pageobjects/stage1.js');  
  await browser.sleep(4000)

});

 Given('I navigates to google', async() => {
     await stage1.goto()         
   });
| improve this answer | |
  • Thank you so much it worked !!!! – Learner Jan 21 at 7:40
0

To resolved this issue ensuring that all PageObjects, were properly re-initialized before each test.

It ensures that the ElementFinder properties are using the current browser session.

Below is code snippet to initialise a page object before each test.

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.
    stageObj = stage1();
});

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.

| improve this answer | |
  • Hi could you edit and explain it according to the code In the post. There is no login page mentioned in the code , Try to explain your answer according to the context – PDHide Jan 20 at 20:39
  • @PDHide it just sample code snippet. I could not find PAge object name in question so I use this as sample Page object name. – Muzzamil Jan 20 at 20:40
  • It is simple. For new session we need initialise new Page object for new/next test case. If it is not clear probably I will try to add more detail – Muzzamil Jan 20 at 20:42
  • in the spec , the stage1.js is being accessed so it would be the page object. – PDHide Jan 20 at 20:50
  • @PDHide Thanks for review. I have updated the code snippet. – Muzzamil Jan 20 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.