I've been juggling around taking the screenshot of the correct session in CodeceptJs but it always take the screenshot of the default session. I need to post the screenshot to the allure report. It can either be screenshots of all the sessions upon a failure or the screenshot of an active session (where the execution fails) instead of always getting the screenshot of default session.
Following is the test scenario and the config.js code I'm using to test the screenshots where I need to get the screenshot of the session user1 but I always get the screenshot of the default session
config.js file
plugins: {
allure: {
enabled: false,
require: '@codeceptjs/allure-legacy'
},
screenshotOnFail: {
enabled: true,
fullPageScreenshots: true,
uniqueScreenshotNames: true,
onFail: (test, context) => {
const { I, sessions } = context
for (const sessionName in sessions) {
I.switchTo(sessionName)
I.saveScreenshot(`e2e/output/${test.title}_${sessionName}_.failed.png`)
}
}
}
}
test file
const { I } = inject()
Before(async () => {
// some code to login to an web application
session('user1', async () => {
// some code to login to an web application
})
})
Scenario('Take Screenshot of the user1 session. @screenshots ', async () => {
await I.visit('https://page1.com')
session('user1', async () => {
I.say('======================= Session user1 ======================= ', 'blue')
await I.visit('https://page2.com'), I.wait(5)
assert(4 === 5, 'Failing the test in "user1" session.')
})
I.say('======================= Session default ======================= ', 'blue')
await I.visit('https://page2.com')
session('user1', async () => {
I.say('======================= Session user1 ======================= ', 'blue')
await I.visit('https://page1.com')
})
})