Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoSuchSessionError: Invalid Session id, when Trying to take screenshot ( cucumber with hook file) #506

Closed
mtahat opened this issue Mar 19, 2020 · 4 comments

Comments

@mtahat
Copy link

mtahat commented Mar 19, 2020

I am using Serenity-js with Cucumber, protractor & typescript. I added a hook.js to my framework to close and clean the locale storage session after each feature/scenario. Then I start getting this Invalid session id exception. I also have restartBrowserBetweenTests: true in my protractor.config file.

FYI: I used the template cucumber/serenity template that you have here to build my project. see dependencies below, I am also using Mac book, VS Code IDE.

How can I close the browser and clean the cookies after each cucumber feature/scenario without braking the screenshot? Can you recommend a "good hook file setting"?

Hook.ts

import { Log } from '@serenity-js/core';
import { After, Before } from 'cucumber';
import { browser } from 'protractor';

Before({ timeout: 2 * 5000 }, async () => {
    browser.driver.manage().window().maximize();
});

After({ timeout: 2 * 5000 }, async () => {
    browser.executeScript('window.sessionStorage.clear();').catch(() => { Log.the('sessionStorage Already Cleared!'); });
    browser.executeScript('window.localStorage.clear();').catch(() => { Log.the('Locale Storage Already Cleared!'); });
    await browser.close();
});
**Error: Some of the async operations have failed:
[test:execute]   [Photographer:TakePhotosOfInteractions] Taking screenshot of 'Interaction ABC'... - NoSuchSessionError: invalid session id**
[test:execute]       at Object.throwDecodedError (/node_modules/selenium-webdriver/lib/error.js:514:15)
[test:execute]       at parseHttpResponse (/node_modules/selenium-webdriver/lib/http.js:519:13)
[test:execute]       at /node_modules/selenium-webdriver/lib/http.js:441:30
[test:execute]       at runMicrotasks (<anonymous>)
[test:execute]       at processTicksAndRejections (internal/process/task_queues.js:93:5)
[test:execute]       at async Promise.all (index 0)

Dependencies

"dependencies": {
    "@types/cucumber": "^6.0.1",
    "@types/node": "^12.12.27",
    "axios": "^0.19.2",
    "chromedriver": "^79.0.3",
    "cucumber": "^6.0.5",
    "cucumber-html-reporter": "^5.1.0",
    "cucumber-junit": "^1.7.1",
    "dotenv": "^8.2.0",
    "is-ci": "^2.0.0",
    "moment": "^2.24.0",
    "npm-failsafe": "^0.4.1",
    "protractor": "^5.4.3",
    "rimraf": "^3.0.2",
    "ts-node": "^8.6.2",
    "tslint": "^5.20.1",
    "tslint-microsoft-contrib": "^6.2.0",
    "typescript": "^3.7.5"
  },
  "devDependencies": {
    "@serenity-js/assertions": "^2.1.5",
    "@serenity-js/console-reporter": "^2.1.5",
    "@serenity-js/core": "^2.1.5",
    "@serenity-js/cucumber": "^2.1.5",
    "@serenity-js/protractor": "^2.1.5",
    "@serenity-js/rest": "^2.1.5",
    "@serenity-js/serenity-bdd": "^2.1.5",
    "lodash": "^4.17.15"
  }
@jan-molak
Copy link
Member

Hey @mtahat!

It looks like the code samples mix synchronous and asynchronous calls, and screenplay and non-screnplay design.

What you'd want in your hook.ts is this:

import { actorCalled, actorInTheSpotlight, Log } from '@serenity-js/core';
import { ResizeBrowserWindow } from '@serenity-js/protractor';
import { After, Before } from 'cucumber';
import { browser } from 'protractor';

Before({ timeout: 2 * 5000 }, () => actorCalled('some actor name').attemptsTo(
    ResizeBrowserWindow.toMaximum(),
));

After({ timeout: 2 * 5000 }, () => actorInTheSpotlight().attemptsTo(
    ExecuteScript.sync(`window.sessionStorage.clear()`),
    ExecuteScript.sync(`window.localStorage.clear()`),
    Log.the('any message you want to log'),
));

Please note that calling browser.close() is not needed since Protractor manages the state of the browser for you.

And in the code sample below:

browser.executeScript('window.sessionStorage.clear();').catch(() => { Log.the('sessionStorage Already Cleared!'); });

Calling a clear() on an empty sessionStorage won't result in an Error, so the Log will never get triggered. Also calling Log.the('sessionStorage Already Cleared!') only instantiates the interaction fro the actor to perform, you still need to tell your actor to perform it by passing it to attemptsTo:

actorCalled('some name').attemptsTo(
    Log.the('message to be logged'),
)

Does this help?

Jan

@jan-molak jan-molak added this to Ideas in Serenity/JS Board via automation Mar 19, 2020
@jan-molak jan-molak moved this from Ideas to Awaiting feedback in Serenity/JS Board Mar 19, 2020
@mtahat
Copy link
Author

mtahat commented Mar 19, 2020

That's Make perfect sense! Thank you for your quick response. Do I need restratBrowserBetweenTest attribute in the Protractor.congif? it seems like having that attribute True causes issues with the screenshot!

@mtahat
Copy link
Author

mtahat commented Mar 19, 2020

The changes that @jan-molak suggested for the hook file works for me. now all tests passed no issues with screenshot. I am closing this this issue since it was related to my hook file not serenity js.
Thank Jan.!

@mtahat mtahat closed this as completed Mar 19, 2020
Serenity/JS Board automation moved this from Awaiting feedback to Done Mar 19, 2020
@jan-molak
Copy link
Member

You're most welcome, happy to have helped!


If Serenity/JS makes your life easier, make sure to buy us a coffee to keep us going :octocat:

jan-molak added a commit that referenced this issue Oct 24, 2020
Photographer will now ignore selenium-webdriver NoSuchSessionError, which occurs when the browser
window (i.e. a pop-up) gets closed during the test

Closes #680 and #506
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants