@zohodesk/testinglibrary 0.1.8-exp-bdd-v3 → 0.1.8-exp-bdd

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/build/bdd-framework/cli/commands/env.js +42 -0
  2. package/build/bdd-framework/cli/commands/export.js +47 -0
  3. package/build/bdd-framework/cli/commands/test.js +60 -0
  4. package/build/bdd-framework/cli/index.js +11 -0
  5. package/build/bdd-framework/cli/options.js +19 -0
  6. package/build/bdd-framework/cli/worker.js +13 -0
  7. package/build/bdd-framework/config/dir.js +27 -0
  8. package/build/bdd-framework/config/env.js +49 -0
  9. package/build/bdd-framework/config/index.js +90 -0
  10. package/build/bdd-framework/cucumber/buildStepDefinition.js +43 -0
  11. package/build/bdd-framework/cucumber/gherkin.d.js +5 -0
  12. package/build/bdd-framework/cucumber/gherkin.d.ts +45 -0
  13. package/build/bdd-framework/cucumber/loadConfig.js +17 -0
  14. package/build/bdd-framework/cucumber/loadFeatures.js +39 -0
  15. package/build/bdd-framework/cucumber/loadSnippetBuilder.js +20 -0
  16. package/build/bdd-framework/cucumber/loadSources.js +57 -0
  17. package/build/bdd-framework/cucumber/loadSteps.js +35 -0
  18. package/build/bdd-framework/decorators.js +18 -0
  19. package/build/bdd-framework/gen/formatter.js +88 -0
  20. package/build/bdd-framework/gen/i18n.js +35 -0
  21. package/build/bdd-framework/gen/index.js +163 -0
  22. package/build/bdd-framework/gen/poms.js +46 -0
  23. package/build/bdd-framework/gen/testFile.js +356 -0
  24. package/build/bdd-framework/gen/testNode.js +48 -0
  25. package/build/bdd-framework/gen/testPoms.js +123 -0
  26. package/build/bdd-framework/index.js +45 -0
  27. package/build/bdd-framework/playwright/fixtureParameterNames.js +77 -0
  28. package/build/bdd-framework/playwright/getLocationInFile.js +46 -0
  29. package/build/bdd-framework/playwright/loadConfig.js +42 -0
  30. package/build/bdd-framework/playwright/testTypeImpl.js +57 -0
  31. package/build/bdd-framework/playwright/transform.js +80 -0
  32. package/build/bdd-framework/playwright/types.js +5 -0
  33. package/build/bdd-framework/playwright/utils.js +37 -0
  34. package/build/bdd-framework/run/bddFixtures.js +107 -0
  35. package/build/bdd-framework/run/bddWorld.js +88 -0
  36. package/build/bdd-framework/snippets/index.js +134 -0
  37. package/build/bdd-framework/snippets/snippetSyntax.js +41 -0
  38. package/build/bdd-framework/snippets/snippetSyntaxDecorators.js +26 -0
  39. package/build/bdd-framework/snippets/snippetSyntaxTs.js +18 -0
  40. package/build/bdd-framework/stepDefinitions/createBdd.js +49 -0
  41. package/build/bdd-framework/stepDefinitions/createDecorators.js +108 -0
  42. package/build/bdd-framework/stepDefinitions/decorators/poms.js +64 -0
  43. package/build/bdd-framework/stepDefinitions/decorators/steps.js +93 -0
  44. package/build/bdd-framework/stepDefinitions/defineStep.js +61 -0
  45. package/build/bdd-framework/stepDefinitions/stepConfig.js +24 -0
  46. package/build/bdd-framework/utils/exit.js +54 -0
  47. package/build/bdd-framework/utils/index.js +44 -0
  48. package/build/bdd-framework/utils/jsStringWrap.js +44 -0
  49. package/build/bdd-framework/utils/logger.js +28 -0
  50. package/build/bdd-poc/core-runner/exportMethods.js +6 -12
  51. package/build/bdd-poc/core-runner/stepDefinitions.js +3 -1
  52. package/build/bdd-poc/test/cucumber/featureFileParer.js +5 -4
  53. package/build/bdd-poc/test/stepGenerate/stepFileGenerate.js +9 -8
  54. package/build/bdd-poc/test/stepGenerate/stepsnippets.js +1 -2
  55. package/build/bdd-poc/test/testDataMap.js +2 -2
  56. package/build/bdd-poc/test/testStructure.js +4 -3
  57. package/build/core/jest/preprocessor/jsPreprocessor.js +2 -3
  58. package/build/core/playwright/custom-commands.js +1 -2
  59. package/build/core/playwright/index.js +18 -8
  60. package/build/core/playwright/readConfigFile.js +1 -2
  61. package/build/core/playwright/setup/config-creator.js +7 -10
  62. package/build/core/playwright/setup/config-utils.js +13 -14
  63. package/build/core/playwright/test-runner.js +28 -36
  64. package/build/decorators.d.ts +1 -1
  65. package/build/decorators.js +16 -2
  66. package/build/index.js +14 -10
  67. package/build/parser/parser.js +0 -1
  68. package/build/utils/logger.js +1 -2
  69. package/build/utils/stepDefinitionsFormatter.js +1 -2
  70. package/npm-shrinkwrap.json +570 -4
  71. package/package.json +3 -3
  72. package/build/bdd-poc/runner.js +0 -19
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Formatter = void 0;
7
+ var _jsStringWrap = require("../utils/jsStringWrap");
8
+ /**
9
+ * Helper to format Playwright test file.
10
+ */
11
+
12
+ const TAGS_FIXTURE_TEST_KEY_SEPARATOR = '|';
13
+ class Formatter {
14
+ config;
15
+ constructor(config) {
16
+ this.config = config;
17
+ }
18
+ fileHeader(uri, importTestFrom) {
19
+ const file = (importTestFrom === null || importTestFrom === void 0 ? void 0 : importTestFrom.file) || '@zohodesk/testinglibrary';
20
+ let varName = (importTestFrom === null || importTestFrom === void 0 ? void 0 : importTestFrom.varName) || 'test';
21
+ if (varName !== 'test') varName = `${varName} as test`;
22
+ return [`/** Generated from: ${uri} */`,
23
+ // this.quoted() is not possible for 'import from' as backticks not parsed
24
+ `import { ${varName} } from ${JSON.stringify(file)};`, ''];
25
+ }
26
+ suite(node, children) {
27
+ // prettier-ignore
28
+ return [`test.describe${this.getSubFn(node)}(${this.quoted(node.title)}, () => {`, '', ...children.map(indent), `});`, ''];
29
+ }
30
+ beforeEach(fixtures, children) {
31
+ const fixturesStr = [...fixtures].join(', ');
32
+ // prettier-ignore
33
+ return [`test.beforeEach(async ({ ${fixturesStr} }) => {`, ...children.map(indent), `});`, ''];
34
+ }
35
+ test(node, fixtures, children) {
36
+ const fixturesStr = [...fixtures].join(', ');
37
+ // prettier-ignore
38
+ return [`test${this.getSubFn(node)}(${this.quoted(node.title)}, async ({ ${fixturesStr} }) => {`, ...children.map(indent), `});`, ''];
39
+ }
40
+ // eslint-disable-next-line max-params
41
+ step(keyword, text, argument, fixtureNames = []) {
42
+ const fixtures = fixtureNames.length ? `{ ${fixtureNames.join(', ')} }` : '';
43
+ const argumentArg = argument ? JSON.stringify(argument) : fixtures ? 'null' : '';
44
+ const textArg = this.quoted(text);
45
+ const args = [textArg, argumentArg, fixtures].filter(arg => arg !== '').join(', ');
46
+ return `await ${keyword}(${args});`;
47
+ }
48
+ missingStep(keyword, text) {
49
+ return `// missing step: ${keyword}(${this.quoted(text)});`;
50
+ }
51
+ useFixtures(fixtures) {
52
+ return fixtures.length > 0 ? ['// == technical section ==', '', 'test.use({', ...fixtures.map(indent), '});'] : [];
53
+ }
54
+ testFixture() {
55
+ return ['$test: ({}, use) => use(test),'];
56
+ }
57
+ tagsFixture(testNodes) {
58
+ const lines = testNodes.filter(node => node.tags.length).map(node => {
59
+ // remove first parent as it is the same for all tests: root suite
60
+ const key = node.titlePath.slice(1).join(TAGS_FIXTURE_TEST_KEY_SEPARATOR);
61
+ return `${JSON.stringify(key)}: ${JSON.stringify(node.tags)},`;
62
+ });
63
+ return lines.length > 0 ? ['$tags: ({}, use, testInfo) => use({', ...lines.map(indent),
64
+ // .slice(2) -> b/c we remove filename and root suite title
65
+ `}[testInfo.titlePath.slice(2).join(${JSON.stringify(TAGS_FIXTURE_TEST_KEY_SEPARATOR)})] || []),`] : [];
66
+ }
67
+ getSubFn(node) {
68
+ if (node.flags.only) return '.only';
69
+ if (node.flags.skip) return '.skip';
70
+ if (node.flags.fixme) return '.fixme';
71
+ return '';
72
+ }
73
+ /**
74
+ * Apply this function only to string literals (mostly titles here).
75
+ * Objects and arrays are handled with JSON.strinigfy,
76
+ * b/c object keys can't be in backtiks.
77
+ * See: https://stackoverflow.com/questions/33194138/template-string-as-object-property-name
78
+ */
79
+ quoted(str) {
80
+ return (0, _jsStringWrap.jsStringWrap)(str, {
81
+ quotes: this.config.quotes
82
+ });
83
+ }
84
+ }
85
+ exports.Formatter = Formatter;
86
+ function indent(value) {
87
+ return value ? `${' '}${value}` : value;
88
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getKeywordsMap = getKeywordsMap;
7
+ var _gherkin = require("@cucumber/gherkin");
8
+ /**
9
+ * Get i18n keywords.
10
+ * See: https://github.com/cucumber/cucumber-js/blob/main/src/cli/i18n.ts
11
+ */
12
+
13
+ function getKeywordsMap(language) {
14
+ const origMap = _gherkin.dialects[language];
15
+ if (!origMap) {
16
+ throw new Error(`Language not found: ${language}`);
17
+ }
18
+ const targetMap = new Map();
19
+ const enKeywords = Object.keys(origMap);
20
+ enKeywords.forEach(enKeyword => handleKeyword(enKeyword, origMap, targetMap));
21
+ return targetMap;
22
+ }
23
+ function handleKeyword(enKeyword, origMap, targetMap) {
24
+ const nativeKeywords = origMap[enKeyword];
25
+ // Array.isArray converts to any[]
26
+ if (typeof nativeKeywords === 'string') return;
27
+ nativeKeywords.forEach(nativeKeyword => {
28
+ nativeKeyword = nativeKeyword.trim();
29
+ if (!nativeKeyword || nativeKeyword === '*') return;
30
+ targetMap.set(nativeKeyword, capitalizeFirstLetter(enKeyword));
31
+ });
32
+ }
33
+ function capitalizeFirstLetter(s) {
34
+ return s.charAt(0).toUpperCase() + s.slice(1);
35
+ }
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.TestFilesGenerator = void 0;
8
+ var _promises = _interopRequireDefault(require("fs/promises"));
9
+ var _path = _interopRequireDefault(require("path"));
10
+ var _fastGlob = _interopRequireDefault(require("fast-glob"));
11
+ var _testFile = require("./testFile");
12
+ var _loadConfig = require("../cucumber/loadConfig");
13
+ var _loadFeatures = require("../cucumber/loadFeatures");
14
+ var _loadSteps = require("../cucumber/loadSteps");
15
+ var _config = require("../config");
16
+ var _snippets = require("../snippets");
17
+ var _steps = require("../stepDefinitions/decorators/steps");
18
+ var _transform = require("../playwright/transform");
19
+ var _dir = require("../config/dir");
20
+ var _logger = require("../utils/logger");
21
+ var _tagExpressions = _interopRequireDefault(require("@cucumber/tag-expressions"));
22
+ var _exit = require("../utils/exit");
23
+ /**
24
+ * Generate playwright test files from Gherkin documents.
25
+ */
26
+
27
+ class TestFilesGenerator {
28
+ config;
29
+ // all these props are exist
30
+ runConfiguration;
31
+ features;
32
+ supportCodeLibrary;
33
+ files = [];
34
+ tagsExpression;
35
+ logger;
36
+ constructor(config) {
37
+ this.config = config;
38
+ this.logger = new _logger.Logger({
39
+ verbose: config.verbose
40
+ });
41
+ if (config.tags) this.tagsExpression = (0, _tagExpressions.default)(config.tags);
42
+ }
43
+ async generate() {
44
+ await (0, _exit.withExitHandler)(async () => {
45
+ await this.loadCucumberConfig();
46
+ await Promise.all([this.loadFeatures(), this.loadSteps()]);
47
+ this.buildFiles();
48
+ await this.checkUndefinedSteps();
49
+ this.checkImportCustomTest();
50
+ await this.clearOutputDir();
51
+ await this.saveFiles();
52
+ });
53
+ }
54
+ async extractSteps() {
55
+ await this.loadCucumberConfig();
56
+ await this.loadSteps();
57
+ return this.supportCodeLibrary.stepDefinitions;
58
+ }
59
+ async loadCucumberConfig() {
60
+ const environment = {
61
+ cwd: (0, _dir.getPlaywrightConfigDir)()
62
+ };
63
+ const {
64
+ runConfiguration
65
+ } = await (0, _loadConfig.loadConfig)({
66
+ provided: (0, _config.extractCucumberConfig)(this.config)
67
+ }, environment);
68
+ this.runConfiguration = runConfiguration;
69
+ this.warnForTsNodeRegister();
70
+ }
71
+ async loadFeatures() {
72
+ const environment = {
73
+ cwd: (0, _dir.getPlaywrightConfigDir)()
74
+ };
75
+ this.logger.log(`Loading features from: ${this.runConfiguration.sources.paths.join(', ')}`);
76
+ this.features = await (0, _loadFeatures.loadFeatures)(this.runConfiguration, environment);
77
+ this.logger.log(`Loaded features: ${this.features.size}`);
78
+ }
79
+ async loadSteps() {
80
+ const {
81
+ requirePaths,
82
+ importPaths
83
+ } = this.runConfiguration.support;
84
+ this.logger.log(`Loading steps from: ${requirePaths.concat(importPaths).join(', ')}`);
85
+ const environment = {
86
+ cwd: (0, _dir.getPlaywrightConfigDir)()
87
+ };
88
+ this.supportCodeLibrary = await (0, _loadSteps.loadSteps)(this.runConfiguration, environment);
89
+ await this.loadDecoratorSteps();
90
+ this.logger.log(`Loaded steps: ${this.supportCodeLibrary.stepDefinitions.length}`);
91
+ }
92
+ async loadDecoratorSteps() {
93
+ const {
94
+ importTestFrom
95
+ } = this.config;
96
+ if (importTestFrom) {
97
+ // require importTestFrom for case when it is not required by step definitions
98
+ // possible re-require but it's not a problem as it is cached by Node.js
99
+ await (0, _transform.requireTransform)().requireOrImport(importTestFrom.file);
100
+ (0, _steps.appendDecoratorSteps)(this.supportCodeLibrary);
101
+ }
102
+ }
103
+ buildFiles() {
104
+ this.files = [...this.features.entries()].map(([doc, pickles]) => {
105
+ return new _testFile.TestFile({
106
+ doc,
107
+ pickles,
108
+ supportCodeLibrary: this.supportCodeLibrary,
109
+ outputPath: this.getOutputPath(doc),
110
+ config: this.config,
111
+ tagsExpression: this.tagsExpression
112
+ }).build();
113
+ }).filter(file => file.testNodes.length > 0);
114
+ }
115
+ getOutputPath(doc) {
116
+ const configDir = (0, _dir.getPlaywrightConfigDir)();
117
+ // doc.uri is always relative to cwd (coming after cucumber handling)
118
+ // see: https://github.com/cucumber/cucumber-js/blob/main/src/api/gherkin.ts#L51
119
+ const relFeaturePath = doc.uri;
120
+ const absFeaturePath = _path.default.resolve(configDir, relFeaturePath);
121
+ const relOutputPath = _path.default.relative(this.config.featuresRoot, absFeaturePath);
122
+ if (relOutputPath.startsWith('..')) {
123
+ (0, _exit.exit)(`All feature files should be located underneath featuresRoot.`, `Please change featuresRoot or paths in configuration.\n`, `featureFile: ${absFeaturePath}\n`, `featuresRoot: ${this.config.featuresRoot}\n`);
124
+ }
125
+ const absOutputPath = _path.default.resolve(this.config.outputDir, relOutputPath);
126
+ return `${absOutputPath}.spec.js`;
127
+ }
128
+ async checkUndefinedSteps() {
129
+ const undefinedSteps = this.files.reduce((sum, file) => sum + file.undefinedSteps.length, 0);
130
+ if (undefinedSteps > 0) {
131
+ const snippets = new _snippets.Snippets(this.files, this.runConfiguration, this.supportCodeLibrary);
132
+ await snippets.print();
133
+ (0, _exit.exit)();
134
+ }
135
+ }
136
+ checkImportCustomTest() {
137
+ if (this.config.importTestFrom) return;
138
+ const hasCustomTest = this.files.some(file => file.hasCustomTest);
139
+ if (hasCustomTest) {
140
+ (0, _exit.exit)(`When using custom "test" function in createBdd() you should`, `set "importTestFrom" config option that points to file exporting custom test.`);
141
+ }
142
+ }
143
+ async saveFiles() {
144
+ this.files.forEach(file => {
145
+ file.save();
146
+ this.logger.log(`Generated: ${_path.default.relative(process.cwd(), file.outputPath)}`);
147
+ });
148
+ this.logger.log(`Generated files: ${this.files.length}`);
149
+ }
150
+ async clearOutputDir() {
151
+ const pattern = `${_fastGlob.default.convertPathToPattern(this.config.outputDir)}/**/*.spec.js`;
152
+ const testFiles = await (0, _fastGlob.default)(pattern);
153
+ this.logger.log(`Clearing output dir: ${testFiles.length} file(s)`);
154
+ const tasks = testFiles.map(testFile => _promises.default.rm(testFile));
155
+ await Promise.all(tasks);
156
+ }
157
+ warnForTsNodeRegister() {
158
+ if ((0, _loadSteps.hasTsNodeRegister)(this.runConfiguration)) {
159
+ this.logger.warn(`WARNING: usage of requireModule: ['ts-node/register'] is not recommended for playwright-bdd.`, `Remove this option from defineBddConfig() and`, `Playwright's built-in loader will be used to compile TypeScript step definitions.`);
160
+ }
161
+ }
162
+ }
163
+ exports.TestFilesGenerator = TestFilesGenerator;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.POMS = void 0;
7
+ exports.buildFixtureTag = buildFixtureTag;
8
+ var _createDecorators = require("../stepDefinitions/createDecorators");
9
+ /**
10
+ * Handle POMs graph for decorator steps.
11
+ */
12
+
13
+ const FIXTURE_TAG_PREFIX = '@fixture:';
14
+ class POMS {
15
+ usedPoms = new Map();
16
+ add(pomNode) {
17
+ if (pomNode) this.usedPoms.set(pomNode, null);
18
+ }
19
+ addByFixtureName(fixtureName) {
20
+ const pomNode = (0, _createDecorators.getPomNodeByFixtureName)(fixtureName);
21
+ if (pomNode) this.add(pomNode);
22
+ }
23
+ addByTag(tag) {
24
+ const fixtureName = extractFixtureName(tag);
25
+ if (fixtureName) this.addByFixtureName(fixtureName);
26
+ }
27
+ resolveFixtureNames(pomNode) {
28
+ const resolvedFixtureNames = this.usedPoms.get(pomNode);
29
+ if (resolvedFixtureNames) return resolvedFixtureNames;
30
+ const fixtureNames = [...pomNode.children].map(child => this.resolveFixtureNames(child)).flat();
31
+ if (this.usedPoms.has(pomNode)) {
32
+ // if nothing returned from children, use own fixtureName,
33
+ // otherwise use what returned from child
34
+ if (!fixtureNames.length) fixtureNames.push(pomNode.fixtureName);
35
+ this.usedPoms.set(pomNode, fixtureNames);
36
+ }
37
+ return fixtureNames;
38
+ }
39
+ }
40
+ exports.POMS = POMS;
41
+ function extractFixtureName(tag) {
42
+ return tag.startsWith(FIXTURE_TAG_PREFIX) ? tag.replace(FIXTURE_TAG_PREFIX, '') : '';
43
+ }
44
+ function buildFixtureTag(fixtureName) {
45
+ return `${FIXTURE_TAG_PREFIX}${fixtureName}`;
46
+ }
@@ -0,0 +1,356 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.TestFile = void 0;
8
+ var _fs = _interopRequireDefault(require("fs"));
9
+ var _path = _interopRequireDefault(require("path"));
10
+ var _formatter = require("./formatter");
11
+ var _i18n = require("./i18n");
12
+ var _loadSteps = require("../cucumber/loadSteps");
13
+ var _createBdd = require("../stepDefinitions/createBdd");
14
+ var _index = require("@cucumber/cucumber/lib/formatter/helpers/index");
15
+ var _utils = require("../utils");
16
+ var _testPoms = require("./testPoms");
17
+ var _testNode = require("./testNode");
18
+ var _stepConfig = require("../stepDefinitions/stepConfig");
19
+ var _exit = require("../utils/exit");
20
+ /**
21
+ * Generate test code.
22
+ */
23
+
24
+ class TestFile {
25
+ options;
26
+ lines = [];
27
+ i18nKeywordsMap;
28
+ formatter;
29
+ testNodes = [];
30
+ hasCustomTest = false;
31
+ undefinedSteps = [];
32
+ constructor(options) {
33
+ this.options = options;
34
+ this.formatter = new _formatter.Formatter(options.config);
35
+ }
36
+ get sourceFile() {
37
+ const {
38
+ uri
39
+ } = this.options.doc;
40
+ if (!uri) throw new Error(`Document without uri`);
41
+ return uri;
42
+ }
43
+ get content() {
44
+ return this.lines.join('\n');
45
+ }
46
+ get language() {
47
+ var _this$options$doc$fea;
48
+ return ((_this$options$doc$fea = this.options.doc.feature) === null || _this$options$doc$fea === void 0 ? void 0 : _this$options$doc$fea.language) || 'en';
49
+ }
50
+ get config() {
51
+ return this.options.config;
52
+ }
53
+ get outputPath() {
54
+ return this.options.outputPath;
55
+ }
56
+ build() {
57
+ this.loadI18nKeywords();
58
+ this.lines = [...this.getFileHeader(), ...this.getRootSuite(), ...this.getFileFixtures()];
59
+ return this;
60
+ }
61
+ save() {
62
+ const dir = _path.default.dirname(this.outputPath);
63
+ if (!_fs.default.existsSync(dir)) _fs.default.mkdirSync(dir, {
64
+ recursive: true
65
+ });
66
+ _fs.default.writeFileSync(this.outputPath, this.content);
67
+ }
68
+ getFileHeader() {
69
+ const importTestFrom = this.getRelativeImportTestFrom();
70
+ return this.formatter.fileHeader(this.sourceFile, importTestFrom);
71
+ }
72
+ loadI18nKeywords() {
73
+ if (this.language !== 'en') {
74
+ this.i18nKeywordsMap = (0, _i18n.getKeywordsMap)(this.language);
75
+ }
76
+ }
77
+ getRelativeImportTestFrom() {
78
+ const {
79
+ importTestFrom
80
+ } = this.config;
81
+ if (!importTestFrom) return;
82
+ const {
83
+ file,
84
+ varName
85
+ } = importTestFrom;
86
+ const dir = _path.default.dirname(this.outputPath);
87
+ return {
88
+ file: _path.default.relative(dir, file),
89
+ varName
90
+ };
91
+ }
92
+ getFileFixtures() {
93
+ return this.formatter.useFixtures([...this.formatter.testFixture(), ...this.formatter.tagsFixture(this.testNodes)]);
94
+ }
95
+ getRootSuite() {
96
+ const {
97
+ feature
98
+ } = this.options.doc;
99
+ if (!feature) throw new Error(`Document without feature.`);
100
+ return this.getSuite(feature);
101
+ }
102
+ /**
103
+ * Generate test.describe suite for root Feature or Rule
104
+ */
105
+ getSuite(feature, parent) {
106
+ const node = new _testNode.TestNode(feature, parent);
107
+ const lines = [];
108
+ // const { backgrounds, rules, scenarios } =
109
+ // bgFixtures, bgTags - used as fixture hints for decorator steps
110
+ feature.children.forEach(child => lines.push(...this.getSuiteChild(child, node)));
111
+ return this.formatter.suite(node, lines);
112
+ }
113
+ getSuiteChild(child, parent) {
114
+ if ('rule' in child && child.rule) return this.getSuite(child.rule, parent);
115
+ if (child.background) return this.getBeforeEach(child.background, parent);
116
+ if (child.scenario) return this.getScenarioLines(child.scenario, parent);
117
+ throw new Error(`Empty child: ${JSON.stringify(child)}`);
118
+ }
119
+ getScenarioLines(scenario, parent) {
120
+ return this.isOutline(scenario) ? this.getOutlineSuite(scenario, parent) : this.getTest(scenario, parent);
121
+ }
122
+ /**
123
+ * Generate test.beforeEach for Background
124
+ */
125
+ getBeforeEach(bg, parent) {
126
+ const node = new _testNode.TestNode({
127
+ name: 'background',
128
+ tags: []
129
+ }, parent);
130
+ const {
131
+ fixtures,
132
+ lines
133
+ } = this.getSteps(bg, node.tags);
134
+ return this.formatter.beforeEach(fixtures, lines);
135
+ }
136
+ /**
137
+ * Generate test.describe suite for Scenario Outline
138
+ */
139
+ getOutlineSuite(scenario, parent) {
140
+ const node = new _testNode.TestNode(scenario, parent);
141
+ const lines = [];
142
+ let exampleIndex = 0;
143
+ scenario.examples.forEach(examples => {
144
+ const titleFormat = this.getExamplesTitleFormat(examples);
145
+ examples.tableBody.forEach(exampleRow => {
146
+ const testTitle = this.getOutlineTestTitle(titleFormat, examples, exampleRow, ++exampleIndex);
147
+ const testLines = this.getOutlineTest(scenario, examples, exampleRow, testTitle, node);
148
+ lines.push(...testLines);
149
+ });
150
+ });
151
+ return this.formatter.suite(node, lines);
152
+ }
153
+ /**
154
+ * Generate test from Examples row of Scenario Outline
155
+ */
156
+ // eslint-disable-next-line max-params
157
+ getOutlineTest(scenario, examples, exampleRow, title, parent) {
158
+ const node = new _testNode.TestNode({
159
+ name: title,
160
+ tags: examples.tags
161
+ }, parent);
162
+ if (this.skipByTagsExpression(node)) return [];
163
+ this.testNodes.push(node);
164
+ const {
165
+ fixtures,
166
+ lines
167
+ } = this.getSteps(scenario, node.tags, exampleRow.id);
168
+ return this.formatter.test(node, fixtures, lines);
169
+ }
170
+ /**
171
+ * Generate test from Scenario
172
+ */
173
+ getTest(scenario, parent) {
174
+ const node = new _testNode.TestNode(scenario, parent);
175
+ if (this.skipByTagsExpression(node)) return [];
176
+ this.testNodes.push(node);
177
+ const {
178
+ fixtures,
179
+ lines
180
+ } = this.getSteps(scenario, node.tags);
181
+ return this.formatter.test(node, fixtures, lines);
182
+ }
183
+ /**
184
+ * Generate test steps
185
+ */
186
+ getSteps(scenario, tags, outlineExampleRowId) {
187
+ const testFixtureNames = new Set();
188
+ const testPoms = new _testPoms.TestPoms(scenario.name || 'Background');
189
+ const decoratorSteps = [];
190
+ let previousKeywordType = undefined;
191
+ const lines = scenario.steps.map((step, index) => {
192
+ const {
193
+ keyword,
194
+ keywordType,
195
+ fixtureNames: stepFixtureNames,
196
+ line,
197
+ pickleStep,
198
+ stepConfig
199
+ } = this.getStep(step, previousKeywordType, outlineExampleRowId);
200
+ previousKeywordType = keywordType;
201
+ testFixtureNames.add(keyword);
202
+ stepFixtureNames.forEach(fixtureName => testFixtureNames.add(fixtureName));
203
+ if ((0, _stepConfig.isDecorator)(stepConfig)) {
204
+ testPoms.addByStep(stepConfig.pomNode);
205
+ decoratorSteps.push({
206
+ index,
207
+ keyword,
208
+ pickleStep,
209
+ pomNode: stepConfig.pomNode
210
+ });
211
+ }
212
+ return line;
213
+ });
214
+ // decorator steps handled in second pass to guess fixtures
215
+ if (decoratorSteps.length) {
216
+ testFixtureNames.forEach(fixtureName => testPoms.addByFixtureName(fixtureName));
217
+ tags === null || tags === void 0 || tags.forEach(tag => testPoms.addByTag(tag));
218
+ testPoms.resolveFixtures();
219
+ decoratorSteps.forEach(step => {
220
+ const {
221
+ line,
222
+ fixtureName
223
+ } = this.getDecoratorStep(step, testPoms);
224
+ lines[step.index] = line;
225
+ testFixtureNames.add(fixtureName);
226
+ });
227
+ }
228
+ return {
229
+ fixtures: testFixtureNames,
230
+ lines
231
+ };
232
+ }
233
+ /**
234
+ * Generate step for Given, When, Then
235
+ */
236
+ // eslint-disable-next-line max-statements, complexity
237
+ getStep(step, previousKeywordType, outlineExampleRowId) {
238
+ const pickleStep = this.getPickleStep(step, outlineExampleRowId);
239
+ const stepDefinition = (0, _loadSteps.findStepDefinition)(this.options.supportCodeLibrary, pickleStep.text, this.sourceFile);
240
+ const keywordType = (0, _index.getStepKeywordType)({
241
+ keyword: step.keyword,
242
+ language: this.language,
243
+ previousKeywordType
244
+ });
245
+ let keyword = this.getStepKeyword(step);
246
+ if (!stepDefinition) {
247
+ this.undefinedSteps.push({
248
+ keywordType,
249
+ step,
250
+ pickleStep
251
+ });
252
+ return this.getMissingStep(keyword, keywordType, pickleStep);
253
+ }
254
+ // for cucumber-style stepConfig is undefined
255
+ const stepConfig = (0, _stepConfig.getStepConfig)(stepDefinition);
256
+ if (stepConfig !== null && stepConfig !== void 0 && stepConfig.hasCustomTest) this.hasCustomTest = true;
257
+ // for cucumber-style transform Given/When/Then -> Given_/When_/Then_
258
+ // to use own bddWorld (containing PW built-in fixtures)
259
+ if (!(0, _stepConfig.isPlaywrightStyle)(stepConfig)) keyword = `${keyword}_`;
260
+ // for decorator steps fixtureNames are defined later in second pass
261
+ const fixtureNames = (0, _stepConfig.isDecorator)(stepConfig) ? [] : (0, _createBdd.extractFixtureNames)(stepConfig === null || stepConfig === void 0 ? void 0 : stepConfig.fn);
262
+ const line = (0, _stepConfig.isDecorator)(stepConfig) ? '' : this.formatter.step(keyword, pickleStep.text, pickleStep.argument, fixtureNames);
263
+ return {
264
+ keyword,
265
+ keywordType,
266
+ fixtureNames,
267
+ line,
268
+ pickleStep,
269
+ stepConfig
270
+ };
271
+ }
272
+ getMissingStep(keyword, keywordType, pickleStep) {
273
+ return {
274
+ keyword,
275
+ keywordType,
276
+ fixtureNames: [],
277
+ line: this.formatter.missingStep(keyword, pickleStep.text),
278
+ pickleStep,
279
+ stepConfig: undefined
280
+ };
281
+ }
282
+ getPickleStep(step, outlineExampleRowId) {
283
+ for (const pickle of this.options.pickles) {
284
+ const pickleStep = pickle.steps.find(({
285
+ astNodeIds
286
+ }) => {
287
+ const hasStepId = astNodeIds.includes(step.id);
288
+ const hasRowId = !outlineExampleRowId || astNodeIds.includes(outlineExampleRowId);
289
+ return hasStepId && hasRowId;
290
+ });
291
+ if (pickleStep) return pickleStep;
292
+ }
293
+ throw new Error(`Pickle step not found for step: ${step.text}`);
294
+ }
295
+ getStepKeyword(step) {
296
+ const origKeyword = step.keyword.trim();
297
+ const enKeyword = origKeyword === '*' ? 'And' : this.getEnglishKeyword(origKeyword);
298
+ if (!enKeyword) throw new Error(`Keyword not found: ${origKeyword}`);
299
+ return enKeyword;
300
+ }
301
+ getDecoratorStep(step, testPoms) {
302
+ const {
303
+ keyword,
304
+ pickleStep,
305
+ pomNode
306
+ } = step;
307
+ const resolvedFixtures = testPoms.getResolvedFixtures(pomNode);
308
+ if (resolvedFixtures.length !== 1) {
309
+ const suggestedTags = resolvedFixtures.filter(f => !f.byTag).map(f => (0, _testPoms.buildFixtureTag)(f.name)).join(', ');
310
+ const suggestedTagsStr = suggestedTags.length ? ` or set one of the following tags: ${suggestedTags}` : '.';
311
+ (0, _exit.exit)(`Can't guess fixture for decorator step "${pickleStep.text}" in file: ${this.sourceFile}.`, `Please refactor your Page Object classes${suggestedTagsStr}`);
312
+ }
313
+ const fixtureName = resolvedFixtures[0].name;
314
+ return {
315
+ fixtureName,
316
+ line: this.formatter.step(keyword, pickleStep.text, pickleStep.argument, [fixtureName])
317
+ };
318
+ }
319
+ getOutlineTestTitle(titleFormat, examples, exampleRow, exampleIndex) {
320
+ const params = {
321
+ _index_: exampleIndex
322
+ };
323
+ exampleRow.cells.forEach((cell, index) => {
324
+ var _examples$tableHeader;
325
+ const colName = (_examples$tableHeader = examples.tableHeader) === null || _examples$tableHeader === void 0 || (_examples$tableHeader = _examples$tableHeader.cells[index]) === null || _examples$tableHeader === void 0 ? void 0 : _examples$tableHeader.value;
326
+ if (colName) params[colName] = cell.value;
327
+ });
328
+ return (0, _utils.template)(titleFormat, params);
329
+ }
330
+ getExamplesTitleFormat(examples) {
331
+ var _comment$text;
332
+ const {
333
+ line
334
+ } = examples.location;
335
+ const titleFormatCommentLine = line - 1;
336
+ const comment = this.options.doc.comments.find(c => {
337
+ return c.location.line === titleFormatCommentLine;
338
+ });
339
+ const commentText = comment === null || comment === void 0 || (_comment$text = comment.text) === null || _comment$text === void 0 ? void 0 : _comment$text.trim();
340
+ const prefix = '# title-format:';
341
+ return commentText !== null && commentText !== void 0 && commentText.startsWith(prefix) ? commentText.replace(prefix, '').trim() : this.config.examplesTitleFormat;
342
+ }
343
+ skipByTagsExpression(node) {
344
+ var _this$options$tagsExp;
345
+ // see: https://github.com/cucumber/tag-expressions/tree/main/javascript
346
+ return ((_this$options$tagsExp = this.options.tagsExpression) === null || _this$options$tagsExp === void 0 ? void 0 : _this$options$tagsExp.evaluate(node.tags)) === false;
347
+ }
348
+ isOutline(scenario) {
349
+ const keyword = this.getEnglishKeyword(scenario.keyword);
350
+ return keyword === 'ScenarioOutline' || keyword === 'Scenario Outline' || keyword === 'Scenario Template';
351
+ }
352
+ getEnglishKeyword(keyword) {
353
+ return this.i18nKeywordsMap ? this.i18nKeywordsMap.get(keyword) : keyword;
354
+ }
355
+ }
356
+ exports.TestFile = TestFile;