docs/sessions.md
CodeceptJS can run several browser sessions in a single test. This is useful for testing real-time features like chat, notifications, or multi-user workflows.
Scenario('two users chat', ({ I }) => {
I.amOnPage('/chat')
I.fillField('name', 'davert')
I.click('Sign In')
I.see('Hello, davert')
session('john', () => {
I.amOnPage('/chat')
I.fillField('name', 'john')
I.click('Sign In')
I.see('Hello, john')
})
// back in default session (davert)
I.fillField('message', 'Hi, john')
I.see('me: Hi, john', '.messages')
session('john', () => {
I.see('davert: Hi, john', '.messages')
})
})
Switch back to a session at any point by using the same name.
Override the browser config for a specific session:
session('mobile', { browser: 'webkit', viewport: { width: 375, height: 812 } }, () => {
I.amOnPage('/')
I.see('Mobile View')
})
Open sessions in the background without switching to them immediately:
Scenario('multi-user test', ({ I }) => {
session('john')
session('mary')
session('jane')
I.amOnPage('/')
session('mary', () => {
I.amOnPage('/login')
})
})
session can return a value for use in the main scenario:
const profileName = await session('john', () => {
I.amOnPage('/profile')
return I.grabTextFrom({ css: 'h1' })
})
I.fillField('Recipient', profileName)
I object, page objects, and any other objects declared for the scenariowithin can be used inside a session, but session cannot be called from inside within