How to restart browser in protractor-cucumber framework or protractor-jasmine



  • 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,
    };
    


  • 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()         
       });
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2