From 9aae736bff17b6c5d3df53d03675a32e05d13ac5 Mon Sep 17 00:00:00 2001 From: Michael Bodnarchuk Date: Thu, 30 Jan 2025 03:12:59 +0200 Subject: [PATCH] injecting test & siute to container (#4785) * injecting test & siute to container * added container const * fix: export STANDARD_ACTING_HELPERS type --------- Co-authored-by: DavertMik Co-authored-by: kobenguyent --- lib/codecept.js | 4 +-- lib/command/check.js | 13 ++++++++-- lib/command/definitions.js | 2 +- lib/command/interactive.js | 2 +- lib/container.js | 7 ++++++ lib/helper/AI.js | 3 ++- lib/mocha/asyncWrapper.js | 4 ++- lib/mocha/inject.js | 5 ++++ lib/plugin/autoDelay.js | 4 +-- lib/plugin/pageInfo.js | 2 +- lib/plugin/screenshotOnFail.js | 2 +- lib/plugin/standardActingHelpers.js | 5 +++- lib/plugin/stepByStepReport.js | 2 +- .../store-test-and-suite/codecept.conf.js | 11 ++++++++ .../store-test-and-suite_test.js | 25 +++++++++++++++++++ test/runner/store-test-and-suite_test.js | 20 +++++++++++++++ 16 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 test/data/sandbox/configs/store-test-and-suite/codecept.conf.js create mode 100644 test/data/sandbox/configs/store-test-and-suite/store-test-and-suite_test.js create mode 100644 test/runner/store-test-and-suite_test.js diff --git a/lib/codecept.js b/lib/codecept.js index e18aba1f8..7953b20a0 100644 --- a/lib/codecept.js +++ b/lib/codecept.js @@ -202,12 +202,12 @@ class Codecept { } const done = () => { event.emit(event.all.result, container.result()) - event.emit(event.all.after) + event.emit(event.all.after, this) resolve() } try { - event.emit(event.all.before) + event.emit(event.all.before, this) mocha.run(() => done()) } catch (e) { output.error(e.stack) diff --git a/lib/command/check.js b/lib/command/check.js index 4c32ba359..6e3dc3e45 100644 --- a/lib/command/check.js +++ b/lib/command/check.js @@ -1,7 +1,6 @@ const { getConfig, getTestRoot } = require('./utils') const Codecept = require('../codecept') const output = require('../output') -const standardActingHelpers = require('../plugin/standardActingHelpers') const store = require('../store') const container = require('../container') const figures = require('figures') @@ -23,6 +22,7 @@ module.exports = async function (options) { container: false, pageObjects: false, plugins: false, + ai: true, // we don't need to check AI helpers: false, setup: false, tests: false, @@ -51,6 +51,8 @@ module.exports = async function (options) { checks.container = err } + const standardActingHelpers = container.STANDARD_ACTING_HELPERS + printCheck('container', checks['container']) if (codecept) { @@ -83,6 +85,13 @@ module.exports = async function (options) { } } + if (config?.ai?.request) { + checks.ai = true + printCheck('ai', checks['ai'], 'AI configuration is enabled, request function is set') + } else { + printCheck('ai', checks['ai'], 'AI is disabled') + } + printCheck('tests', checks['tests'], `Total: ${numTests} tests`) store.dryRun = true @@ -170,7 +179,7 @@ function printCheck(name, value, comment = '') { } if (value instanceof Error) { - comment = `${comment} ${chalk.red.italic(value.message)}`.trim() + comment = `${comment} ${chalk.red(value.message)}`.trim() } output.print(status, name.toUpperCase(), chalk.dim(comment)) diff --git a/lib/command/definitions.js b/lib/command/definitions.js index 580698ddc..e0e0efcd1 100644 --- a/lib/command/definitions.js +++ b/lib/command/definitions.js @@ -5,7 +5,7 @@ const { getConfig, getTestRoot } = require('./utils') const Codecept = require('../codecept') const container = require('../container') const output = require('../output') -const actingHelpers = [...require('../plugin/standardActingHelpers'), 'REST'] +const actingHelpers = [...container.STANDARD_ACTING_HELPERS, 'REST'] /** * Prepare data and generate content of definitions file diff --git a/lib/command/interactive.js b/lib/command/interactive.js index c9306ea12..44398798a 100644 --- a/lib/command/interactive.js +++ b/lib/command/interactive.js @@ -4,7 +4,7 @@ const Codecept = require('../codecept') const Container = require('../container') const event = require('../event') const output = require('../output') -const webHelpers = require('../plugin/standardActingHelpers') +const webHelpers = Container.STANDARD_ACTING_HELPERS module.exports = async function (path, options) { // Backward compatibility for --profile diff --git a/lib/container.js b/lib/container.js index 267ec5cff..66c7c1610 100644 --- a/lib/container.js +++ b/lib/container.js @@ -34,6 +34,13 @@ let container = { * Dependency Injection Container */ class Container { + /** + * Get the standard acting helpers of CodeceptJS Container + * + */ + static get STANDARD_ACTING_HELPERS() { + return ['Playwright', 'WebDriver', 'Puppeteer', 'Appium', 'TestCafe'] + } /** * Create container with all required helpers and support objects * diff --git a/lib/helper/AI.js b/lib/helper/AI.js index 4b2d75978..4627f5612 100644 --- a/lib/helper/AI.js +++ b/lib/helper/AI.js @@ -3,13 +3,14 @@ const ora = require('ora-classic') const fs = require('fs') const path = require('path') const ai = require('../ai') -const standardActingHelpers = require('../plugin/standardActingHelpers') const Container = require('../container') const { splitByChunks, minifyHtml } = require('../html') const { beautify } = require('../utils') const output = require('../output') const { registerVariable } = require('../pause') +const standardActingHelpers = Container.STANDARD_ACTING_HELPERS + const gtpRole = { user: 'user', } diff --git a/lib/mocha/asyncWrapper.js b/lib/mocha/asyncWrapper.js index 9be59ddb3..7c6e515bc 100644 --- a/lib/mocha/asyncWrapper.js +++ b/lib/mocha/asyncWrapper.js @@ -145,11 +145,13 @@ module.exports.injected = function (fn, suite, hookName) { const opts = suite.opts || {} const retries = opts[`retry${ucfirst(hookName)}`] || 0 + const currentTest = hookName === 'before' || hookName === 'after' ? suite?.ctx?.currentTest : null + promiseRetry( async (retry, number) => { try { recorder.startUnlessRunning() - await fn.call(this, getInjectedArguments(fn)) + await fn.call(this, { ...getInjectedArguments(fn), suite, test: currentTest }) await recorder.promise().catch(err => retry(err)) } catch (err) { retry(err) diff --git a/lib/mocha/inject.js b/lib/mocha/inject.js index 0d88b725f..ace92ca91 100644 --- a/lib/mocha/inject.js +++ b/lib/mocha/inject.js @@ -5,6 +5,7 @@ const getInjectedArguments = (fn, test) => { const testArgs = {} const params = parser.getParams(fn) || [] const objects = container.support() + for (const key of params) { testArgs[key] = {} if (test && test.inject && test.inject[key]) { @@ -18,6 +19,10 @@ const getInjectedArguments = (fn, test) => { testArgs[key] = container.support(key) } + if (test) { + testArgs.suite = test?.parent + testArgs.test = test + } return testArgs } diff --git a/lib/plugin/autoDelay.js b/lib/plugin/autoDelay.js index 80f17b28f..ccd4a7836 100644 --- a/lib/plugin/autoDelay.js +++ b/lib/plugin/autoDelay.js @@ -2,8 +2,8 @@ const Container = require('../container') const store = require('../store') const recorder = require('../recorder') const event = require('../event') -const log = require('../output').log -const supportedHelpers = require('./standardActingHelpers').slice() +const { log } = require('../output') +const standardActingHelpers = Container.STANDARD_ACTING_HELPERS const methodsToDelay = ['click', 'fillField', 'checkOption', 'pressKey', 'doubleClick', 'rightClick'] diff --git a/lib/plugin/pageInfo.js b/lib/plugin/pageInfo.js index 2a1dd3117..c254bb3f9 100644 --- a/lib/plugin/pageInfo.js +++ b/lib/plugin/pageInfo.js @@ -3,7 +3,7 @@ const fs = require('fs') const Container = require('../container') const recorder = require('../recorder') const event = require('../event') -const supportedHelpers = require('./standardActingHelpers') +const supportedHelpers = Container.STANDARD_ACTING_HELPERS const { scanForErrorMessages } = require('../html') const { output } = require('..') const { humanizeString, ucfirst } = require('../utils') diff --git a/lib/plugin/screenshotOnFail.js b/lib/plugin/screenshotOnFail.js index 059a55159..1e4317317 100644 --- a/lib/plugin/screenshotOnFail.js +++ b/lib/plugin/screenshotOnFail.js @@ -15,7 +15,7 @@ const defaultConfig = { fullPageScreenshots: false, } -const supportedHelpers = require('./standardActingHelpers') +const supportedHelpers = Container.STANDARD_ACTING_HELPERS /** * Creates screenshot on failure. Screenshot is saved into `output` directory. diff --git a/lib/plugin/standardActingHelpers.js b/lib/plugin/standardActingHelpers.js index 536ac6a68..ab3ea64d6 100644 --- a/lib/plugin/standardActingHelpers.js +++ b/lib/plugin/standardActingHelpers.js @@ -1,3 +1,6 @@ -const standardActingHelpers = ['Playwright', 'WebDriver', 'Puppeteer', 'Appium', 'TestCafe'] +const Container = require('../container') +// due to using this in internal tooling we won't post deprecation warning +// but please switch to Container.STANDARD_ACTING_HELPERS +const standardActingHelpers = Container.STANDARD_ACTING_HELPERS module.exports = standardActingHelpers diff --git a/lib/plugin/stepByStepReport.js b/lib/plugin/stepByStepReport.js index 52dc64902..d2aae48aa 100644 --- a/lib/plugin/stepByStepReport.js +++ b/lib/plugin/stepByStepReport.js @@ -12,7 +12,7 @@ const event = require('../event') const output = require('../output') const { template, deleteDir } = require('../utils') -const supportedHelpers = require('./standardActingHelpers') +const supportedHelpers = Container.STANDARD_ACTING_HELPERS const defaultConfig = { deleteSuccessful: true, diff --git a/test/data/sandbox/configs/store-test-and-suite/codecept.conf.js b/test/data/sandbox/configs/store-test-and-suite/codecept.conf.js new file mode 100644 index 000000000..f9aa56dc7 --- /dev/null +++ b/test/data/sandbox/configs/store-test-and-suite/codecept.conf.js @@ -0,0 +1,11 @@ +exports.config = { + tests: './*_test.js', + output: './output', + helpers: { + FileSystem: {}, + }, + include: {}, + bootstrap: false, + mocha: {}, + name: 'store-test-and-suite tests', +} diff --git a/test/data/sandbox/configs/store-test-and-suite/store-test-and-suite_test.js b/test/data/sandbox/configs/store-test-and-suite/store-test-and-suite_test.js new file mode 100644 index 000000000..fc635c87e --- /dev/null +++ b/test/data/sandbox/configs/store-test-and-suite/store-test-and-suite_test.js @@ -0,0 +1,25 @@ +const assert = require('assert') + +Feature('store-test-and-suite suite') + +BeforeSuite(({ suite, test }) => { + assert.strictEqual(suite.title, 'store-test-and-suite suite') + assert(!test) +}) + +Before(({ test }) => { + assert.strictEqual(test.title, 'test store-test-and-suite test') + test.artifacts.screenshot = 'screenshot' +}) + +Scenario('test store-test-and-suite test', ({ test }) => { + assert.strictEqual(test.title, 'test store-test-and-suite test') + assert(test.artifacts) + assert(test.meta) + assert.strictEqual(test.artifacts.screenshot, 'screenshot') + test.meta.browser = 'chrome' +}) + +After(({ test }) => { + assert.strictEqual(test.meta.browser, 'chrome') +}) diff --git a/test/runner/store-test-and-suite_test.js b/test/runner/store-test-and-suite_test.js new file mode 100644 index 000000000..7a8bbc72e --- /dev/null +++ b/test/runner/store-test-and-suite_test.js @@ -0,0 +1,20 @@ +const { expect } = require('expect') +const exec = require('child_process').exec +const { codecept_dir, codecept_run } = require('./consts') +const debug = require('debug')('codeceptjs:tests') + +const config_run_config = (config, grep, verbose = false) => `${codecept_run} ${verbose ? '--verbose' : ''} --config ${codecept_dir}/configs/store-test-and-suite/${config} ${grep ? `--grep "${grep}"` : ''}` + +describe('CodeceptJS store-test-and-suite', function () { + this.timeout(10000) + + it('should run store-test-and-suite test', done => { + exec(config_run_config('codecept.conf.js'), (err, stdout) => { + debug(stdout) + expect(stdout).toContain('test store-test-and-suite test') + expect(stdout).toContain('OK') + expect(err).toBeFalsy() + done() + }) + }) +})