question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I.saveScreenshot is confused by multiple sessions

See original GitHub issue

What are you trying to achieve?

I am writing tests that involve multiple sessions using the session() method made available by CodeceptJS. As I write these tests I often take screenshots to ensure the tests are performing as I expect.

What do you get instead?

It seems that after running steps in a different session than the main session subsequent calls to I.saveScreenshot show the last session used, not the main session as I would expect.

$ npx codeceptjs run --steps --verbose
CodeceptJS v2.5.0
Using test root "/Users/[REDACTED]/Projects/session-test"
Helpers: Puppeteer
Plugins: screenshotOnFail, retryFailedStep

session --
    [1] Starting recording promises
    Emitted | suite.before ([object Object])
  test something
    Emitted | test.before ([object Object])
    Emitted | test.start ([object Object])
    Emitted | step.before (I am on page "/")
    Emitted | step.after (I am on page "/")
    Emitted | step.before (I fill field "input[name="q"]", "This is User 1")
    Emitted | step.after (I fill field "input[name="q"]", "This is User 1")
    Emitted | step.before (I save screenshot "user1.png")
    Emitted | step.after (I save screenshot "user1.png")
    Emitted | step.before (I save screenshot "user1_2.png")
    Emitted | step.after (I save screenshot "user1_2.png")
    Emitted | step.start (I am on page "/")
    I am on page "/"
    › [Url] https://www.google.com/?gws_rd=ssl
    Emitted | step.passed (I am on page "/")
    Emitted | step.finish (I am on page "/")
    Emitted | step.start (I fill field "input[name="q"]", "This is User 1")
    I fill field "input[name="q"]", "This is User 1"
    Emitted | step.passed (I fill field "input[name="q"]", "This is User 1")
    Emitted | step.finish (I fill field "input[name="q"]", "This is User 1")
    Emitted | step.start (I save screenshot "user1.png")
    I save screenshot "user1.png"
    › Screenshot is saving to /Users/[REDACTED]/Projects/session-test/output/user1.png
    Emitted | step.passed (I save screenshot "user1.png")
    Emitted | step.finish (I save screenshot "user1.png")
 › [Incognito Tab] opened
    [1] Starting <session:user2> session
    Emitted | step.before (I am on page "/")
    Emitted | step.after (I am on page "/")
    Emitted | step.before (I fill field "input[name="q"]", "This is User 2")
    Emitted | step.after (I fill field "input[name="q"]", "This is User 2")
    Emitted | step.before (I save screenshot "user2.png")
    Emitted | step.after (I save screenshot "user2.png")
    Emitted | step.start (user2: I am on page "/")
    user2: I am on page "/"
    › [Url] https://www.google.com/?gws_rd=ssl
    Emitted | step.passed (user2: I am on page "/")
    Emitted | step.finish (user2: I am on page "/")
    Emitted | step.start (user2: I fill field "input[name="q"]", "This is User 2")
    user2: I fill field "input[name="q"]", "This is User 2"
    Emitted | step.passed (user2: I fill field "input[name="q"]", "This is User 2")
    Emitted | step.finish (user2: I fill field "input[name="q"]", "This is User 2")
    Emitted | step.start (user2: I save screenshot "user2.png")
    user2: I save screenshot "user2.png"
    › Screenshot is saving to /Users/[REDACTED]/Projects/session-test/output/user2.png
    Emitted | step.passed (user2: I save screenshot "user2.png")
    Emitted | step.finish (user2: I save screenshot "user2.png")
    [1] <session:user2> Finalize <session:user2> session
    Emitted | step.start (I save screenshot "user1_2.png")
    I save screenshot "user1_2.png"
    › Screenshot is saving to /Users/[REDACTED]/Projects/session-test/output/user1_2.png
    Emitted | step.passed (I save screenshot "user1_2.png")
    Emitted | step.finish (I save screenshot "user1_2.png")
    Emitted | test.passed ([object Object])
    Emitted | test.finish ([object Object])
  ✔ OK in 2915ms

    Emitted | test.after ([object Object])
    Emitted | suite.after ([object Object])

  OK  | 1 passed   // 3s
    Emitted | global.result ([object Object])
    Emitted | global.after ([object Object])

Provide test source code if related

Feature('session');

Scenario('test something', (I) => {
  const searchSel = 'input[name="q"]';

  I.amOnPage('/');
  I.fillField(searchSel, 'This is User 1');
  I.saveScreenshot('user1.png');

  session('user2', () => {
    I.amOnPage('/');
    I.fillField(searchSel, 'This is User 2');
    I.saveScreenshot('user2.png');
  });

  I.saveScreenshot('user1_2.png');
});

Details

  • CodeceptJS version: 2.5.0
  • NodeJS Version: 12.5.0
  • Operating System: macOS Catalina (10.15.3)
  • Helper: puppeteer
  • Configuration file:
const { setHeadlessWhen } = require('@codeceptjs/configure');

// turn on headless mode when running with HEADLESS=true environment variable
// HEADLESS=true npx codecept run
setHeadlessWhen(process.env.HEADLESS);

exports.config = {
  tests: './*_test.js',
  output: './output',
  helpers: {
    Puppeteer: {
      url: 'http://www.google.com',
      show: false
    }
  },
  include: {
    I: './steps_file.js'
  },
  bootstrap: null,
  mocha: {},
  name: 'session-test',
  plugins: {
    retryFailedStep: {
      enabled: true
    },
    screenshotOnFail: {
      enabled: true
    }
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

0reactions
ChexWarriorcommented, Mar 5, 2020

So in order for this to work (in Puppeteer) we need to tie a single session to a single page object. To do that I’m thinking we create a new object within the Puppeteer helper that will be used to track each session to the page generated for that session. Something like this.sessionPages = {};.

Within session.js we can start passing the sessionName into the start() method, making the parameter optional (it doesn’t seem to break any tests). The Puppeteer _session.start method will assign the newly created page to this name like so this.sessionPages[sessionName] = await bc.newPage();.

I’m currently trying to figure out how to pass the sessionName to the actual session steps (where I.saveScreenshot()) would occur so we can know to use a page contained within this.sessionPages(). I see in session.js where the function is actually applied but I’m still trying to think of way to actually pass in the sessionName so its usuable by I.saveScreenshot().

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple usage of I.saveScreenshot() pauses the codeceptjs ...
Expected Result: Trying to take multiple screenshots of the testcase runs, and smooth running of the testcases. What happens: Codeceptjs hangs ...
Read more >
saveScreenshot - WebdriverIO
Save a screenshot of the current browsing context to a PNG file on your OS. Be aware that some browser drivers take screenshots...
Read more >
How to save screen shot to relative path when running on VM
The robot must have been getting confused when saving the image since there were 2 users logged in with the same credentials (the...
Read more >
My PrntScreen key no longer works with Greenshot.... why?
For this reasons, it registers the PrntScreen key and Greenshot is no longer notified when you hit the key. Many people are confused...
Read more >
How to create a screenshot and automatically save it as a file ...
Though this method works, it was inconvenient, required an extra program, and was confusing for less experienced computer users.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found