|
1 | 1 | const fs = require('fs') |
2 | 2 | const path = require('path') |
3 | 3 | const { fileExists } = require('../utils') |
4 | | -const FuncStep = require('../step/func') |
5 | | -const Section = require('../step/section') |
6 | | -const recordStep = require('../step/record') |
7 | 4 | const container = require('../container') |
8 | 5 | const store = require('../store') |
9 | | -const event = require('../event') |
10 | 6 | const recorder = require('../recorder') |
11 | 7 | const { debug } = require('../output') |
12 | 8 | const isAsyncFunction = require('../utils').isAsyncFunction |
@@ -280,42 +276,8 @@ module.exports = function (config) { |
280 | 276 | } |
281 | 277 |
|
282 | 278 | const loginFunction = async name => { |
283 | | - const I = container.support('I') |
284 | | - const test = store.currentTest |
285 | | - |
286 | | - // we are in BeforeSuite hook |
287 | | - if (!test) { |
288 | | - enableAuthBeforeEachTest(name) |
289 | | - return |
290 | | - } |
291 | | - |
292 | | - if (config.saveToFile && !store[`${name}_session`]) { |
293 | | - // loading from file |
294 | | - for (const name in config.users) { |
295 | | - const fileName = path.join(global.output_dir, `${name}_session.json`) |
296 | | - if (!fileExists(fileName)) continue |
297 | | - const data = fs.readFileSync(fileName).toString() |
298 | | - try { |
299 | | - store[`${name}_session`] = JSON.parse(data) |
300 | | - } catch (err) { |
301 | | - throw new Error(`Could not load session from ${fileName}\n${err}`) |
302 | | - } |
303 | | - debug(`Loaded user session for ${name}`) |
304 | | - } |
305 | | - } |
306 | | - |
307 | | - if (isPlaywrightSession() && test?.opts?.cookies) { |
308 | | - if (test.opts.user == name) { |
309 | | - debug(`Cookies already loaded for ${name}`) |
310 | | - // alreadyLoggedIn(name); |
311 | | - return |
312 | | - } else { |
313 | | - debug(`Cookies already loaded for ${test.opts.user}, but not for ${name}`) |
314 | | - await I.deleteCookie() |
315 | | - } |
316 | | - } |
317 | | - |
318 | 279 | const userSession = config.users[name] |
| 280 | + const I = container.support('I') |
319 | 281 | const cookies = store[`${name}_session`] |
320 | 282 | const shouldAwait = isAsyncFunction(userSession.login) || isAsyncFunction(userSession.restore) || isAsyncFunction(userSession.check) |
321 | 283 |
|
@@ -370,62 +332,8 @@ module.exports = function (config) { |
370 | 332 | return recorder.promise() |
371 | 333 | } |
372 | 334 |
|
373 | | - function enableAuthBeforeEachTest(name) { |
374 | | - const suite = store.currentSuite |
375 | | - if (!suite) return |
376 | | - |
377 | | - debug(`enabling auth as ${name} for each test of suite ${suite.title}`) |
378 | | - |
379 | | - // we are setting test opts so they can be picked up by Playwright if it starts browser for this test |
380 | | - suite.eachTest(test => { |
381 | | - // preload from store |
382 | | - if (store[`${name}_session`]) { |
383 | | - test.opts.cookies = store[`${name}_session`] |
384 | | - test.opts.user = name |
385 | | - return |
386 | | - } |
387 | | - |
388 | | - if (!config.saveToFile) return |
389 | | - const cookieFile = path.join(global.output_dir, `${name}_session.json`) |
390 | | - |
391 | | - if (!fileExists(cookieFile)) { |
392 | | - return |
393 | | - } |
394 | | - |
395 | | - const context = fs.readFileSync(cookieFile).toString() |
396 | | - test.opts.cookies = JSON.parse(context) |
397 | | - test.opts.user = name |
398 | | - }) |
399 | | - |
400 | | - function runLoginFunctionForTest(test) { |
401 | | - if (!suite.tests.includes(test)) return |
402 | | - // let's call this function to ensure that authorization happened |
403 | | - // if no cookies, it will login and save them |
404 | | - loginFunction(name) |
405 | | - } |
406 | | - |
407 | | - // we are in BeforeSuite hook |
408 | | - event.dispatcher.on(event.test.started, runLoginFunctionForTest) |
409 | | - event.dispatcher.on(event.suite.after, () => { |
410 | | - event.dispatcher.off(event.test.started, runLoginFunctionForTest) |
411 | | - }) |
412 | | - } |
413 | | - |
414 | 335 | // adding this to DI container |
415 | 336 | const support = {} |
416 | 337 | support[config.inject] = loginFunction |
417 | 338 | container.append({ support }) |
418 | | - |
419 | | - return loginFunction |
420 | | -} |
421 | | - |
422 | | -function isPlaywrightSession() { |
423 | | - return !!container.helpers('Playwright') |
424 | | -} |
425 | | - |
426 | | -function alreadyLoggedIn(name) { |
427 | | - const step = new FuncStep('am logged in as') |
428 | | - step.actor = 'I' |
429 | | - step.setCallable(() => {}) |
430 | | - return recordStep(step, [name]) |
431 | 339 | } |
0 commit comments