MCPcopy Index your code
hub / github.com/codeceptjs/CodeceptJS / session

Function session

lib/session.js:18–138  ·  view source on GitHub ↗

* @param {CodeceptJS.LocatorOrString} sessionName * @param {Function | Object } config * @param {Function} [fn] * @return {any}

(sessionName, config, fn)

Source from the content-addressed store, hash-verified

16 * @return {any}
17 */
18function session(sessionName, config, fn) {
19 if (typeof config === 'function') {
20 if (typeof fn === 'function') {
21 config = config()
22 } else {
23 // no config but with function
24 fn = config
25 config = {}
26 }
27 }
28 // session helpers won't use beforeSuite and afterSuite hooks...
29 // restart: false options are not allowed as well
30 // but those helpers should be already started so inside listener/helpers.js the `_init` method should already be called
31
32 const helpers = container.helpers()
33
34 if (!savedSessions[sessionName]) {
35 for (const helper in helpers) {
36 if (!helpers[helper]._session) continue
37 savedSessions[sessionName] = {
38 start: () => null,
39 stop: () => null,
40 loadVars: () => null,
41 restoreVars: () => null,
42 ...(store.dryRun ? {} : helpers[helper]._session()),
43 }
44 break
45 }
46
47 const closeBrowsers = () => {
48 const session = savedSessions[sessionName]
49 if (!session) return
50 session.stop(session.vars)
51 delete savedSessions[sessionName]
52 }
53
54 event.dispatcher.once(event.test.after, () => {
55 recorder.add('close session browsers', closeBrowsers)
56 })
57
58 if (!savedSessions[sessionName]) {
59 throw new Error('Configured helpers do not support starting sessions. Please use a helper with session support.')
60 }
61
62 recorder.add('save vars', async () => {
63 savedSessions[sessionName].vars = await savedSessions[sessionName].start(sessionName, config)
64 })
65 }
66
67 // pick random color
68 const color = sessionColors[Object.keys(savedSessions).indexOf(sessionName) % sessionColors.length]
69
70 const addContextToStep = step => {
71 step.actor = `${output.colors[color](sessionName)}: I`
72 }
73
74 if (!fn) return // no current session steps
75

Callers 4

session_test.jsFile · 0.85

Calls 6

isAsyncFunctionFunction · 0.90
helpersMethod · 0.80
finalizeFunction · 0.70
_sessionMethod · 0.45
addMethod · 0.45
startMethod · 0.45

Tested by

no test coverage detected