Skip to content

Commit 6d10aa5

Browse files
Copilotkobenguyent
andcommitted
Changes before error encountered
Co-authored-by: kobenguyent <[email protected]>
1 parent 79b3c98 commit 6d10aa5

30 files changed

+64
-68
lines changed

lib/helper/Mochawesome.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ class Mochawesome extends Helper {
1515
disableScreenshots: false,
1616
}
1717

18-
this._addContext = (await import('mochawesome/addContext')).default
18+
this._addContext = null
1919

2020
this._createConfig(config)
2121
}
2222

23+
async _ensureAddContext() {
24+
if (!this._addContext) {
25+
this._addContext = (await import('mochawesome/addContext')).default
26+
}
27+
return this._addContext
28+
}
29+
2330
_createConfig(config) {
2431
// override defaults with config
2532
Object.assign(this.options, config)
@@ -40,7 +47,7 @@ class Mochawesome extends Helper {
4047
currentTest = { test }
4148
}
4249

43-
_failed(test) {
50+
async _failed(test) {
4451
if (this.options.disableScreenshots) return
4552
let fileName
4653
// Get proper name if we are fail on hook
@@ -58,13 +65,15 @@ class Mochawesome extends Helper {
5865
}
5966
if (test._retries < 1 || test._retries === test.retryNum) {
6067
fileName = `${fileName}.failed.png`
61-
return this._addContext(currentTest, fileName)
68+
const addContext = await this._ensureAddContext()
69+
return addContext(currentTest, fileName)
6270
}
6371
}
6472

65-
addMochawesomeContext(context) {
73+
async addMochawesomeContext(context) {
6674
if (currentTest === '') currentTest = { test: currentSuite.ctx.test }
67-
return this._addContext(currentTest, context)
75+
const addContext = await this._ensureAddContext()
76+
return addContext(currentTest, context)
6877
}
6978
}
7079

lib/locator.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
let cssToXPath;
2+
let csstoxpath;
23
import { sprintf } from 'sprintf-js'
34

45
import { xpathLocator } from './utils.js'
56

7+
// Load modules at startup using top-level await
8+
try {
9+
cssToXPath = (await import('css-to-xpath')).default;
10+
} catch (e) {
11+
console.warn('css-to-xpath module not available:', e.message);
12+
}
13+
14+
try {
15+
csstoxpath = (await import('csstoxpath')).default;
16+
} catch (e) {
17+
console.warn('csstoxpath module not available:', e.message);
18+
}
19+
620
const locatorTypes = ['css', 'by', 'xpath', 'id', 'name', 'fuzzy', 'frame', 'shadow', 'pw'];
721
/** @class */
822
class Locator {
@@ -177,14 +191,21 @@ class Locator {
177191
const locator = `${this.value}${pseudoSelector}`;
178192
const limitation = [':nth-of-type', ':first-of-type', ':last-of-type', ':nth-last-child', ':nth-last-of-type', ':checked', ':disabled', ':enabled', ':required', ':lang', ':nth-child', ':has'];
179193

194+
let converter;
180195
if (limitation.some(item => locator.includes(item))) {
181-
cssToXPath = (await import('css-to-xpath')).default;
196+
if (!cssToXPath) {
197+
throw new Error('css-to-xpath module not available. Install with: npm install css-to-xpath');
198+
}
199+
converter = cssToXPath;
182200
} else {
183-
cssToXPath = (await import('csstoxpath')).default;
201+
if (!csstoxpath) {
202+
throw new Error('csstoxpath module not available. Install with: npm install csstoxpath');
203+
}
204+
converter = csstoxpath;
184205
}
185206

186207
if (this.isXPath()) return this.value;
187-
if (this.isCSS()) return cssToXPath(locator);
208+
if (this.isCSS()) return converter(locator);
188209

189210
throw new Error('Can\'t be converted to XPath');
190211
}

lib/mocha/ui.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import escapeRe from 'escape-string-regexp'
2+
import common from 'mocha/lib/interfaces/common.js'
23
import { test, setup, teardown, suiteSetup, suiteTeardown, injected } from './asyncWrapper.js'
34
import ScenarioConfig from './scenarioConfig.js'
45
import FeatureConfig from './featureConfig.js'
@@ -43,8 +44,6 @@ export default function (suite) {
4344
let afterEachHooksAreLoaded
4445

4546
suite.on('pre-require', (context, file, mocha) => {
46-
import common from 'mocha/lib/interfaces/common'
47-
4847
const addScenario = function (title, opts = {}, fn) {
4948
const suite = suites[0]
5049

test/unit/FileSystem_test.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import path from 'path'
22
import chai from 'chai'
33
import { fileURLToPath } from 'url'
4-
const { expect } = chai
5-
6-
7-
84
import FileSystem from '../../lib/helper/FileSystem.js'
95

6+
const { expect } = chai
107
const __dirname = path.dirname(fileURLToPath(import.meta.url))
118
global.codecept_dir = path.join(__dirname, '/..')
129

test/unit/ai_test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import AiAssistant from '../../lib/ai.js'
22
import config from '../../lib/config.js'
3-
43
import chai from 'chai'
5-
const { expect } = chai
64

5+
const { expect } = chai
76
describe('AI module', () => {
87
beforeEach(() => {
98
AiAssistant.enable({}) // clean up config

test/unit/assert/empty_test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import chai from 'chai'
2-
const { expect } = chai
3-
42
import { Assertion, empty } from '../../../lib/assert/empty.js'
53
import AssertionError from '../../../lib/assert/error.js'
64

5+
const { expect } = chai
76
let emptyAssertion
87

98
describe('empty assertion', () => {

test/unit/assert_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import chai from 'chai'
2-
const { expect } = chai
3-
42
import Assertion from '../../lib/assert.js'
53
import AssertionError from '../../lib/assert/error.js'
64

5+
const { expect } = chai
6+
77
const comparator = (a, b) => a === b
88

99
let assertion

test/unit/bdd_test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
import path from 'path'
22
import { fileURLToPath } from 'url'
3-
43
import Gherkin from '@cucumber/gherkin'
54
import * as Messages from '@cucumber/messages'
65
import chai from 'chai'
76

8-
97
const { expect } = chai
108
const uuidFn = Messages.IdGenerator.uuid()
119
const builder = new Gherkin.AstBuilder(uuidFn)
1210
const matcher = new Gherkin.GherkinClassicTokenMatcher()
1311

14-
import Config from '../../lib/config.js'
15-
import { Given, When, And, Then, matchStep, clearSteps, defineParameterType } from '../../lib/mocha/bdd.js'
16-
import run from '../../lib/mocha/gherkin.js'
17-
import recorder from '../../lib/recorder.js'
18-
import container from '../../lib/container.js'
19-
import actor from '../../lib/actor.js'
20-
import event from '../../lib/event.js'
2112

2213
const __dirname = path.dirname(fileURLToPath(import.meta.url))
2314
global.codecept_dir = path.join(__dirname, '/..')

test/unit/config_test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import chai from 'chai'
2-
const { expect } = chai
3-
42
import config from '../../lib/config.js'
53

4+
const { expect } = chai
65
describe('Config', () => {
76
beforeEach(() => config.reset())
87

test/unit/container_test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import chai from 'chai'
2-
const { expect } = chai
32
import path from 'path'
43
import { fileURLToPath } from 'url'
5-
64
import FileSystem from '../../lib/helper/FileSystem.js'
75
import actor from '../../lib/actor.js'
86
import container from '../../lib/container.js'
97
import Translation from '../../lib/translation.js'
108
import dummyPage from '../data/dummy_page.js'
119

10+
const { expect } = chai
1211
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1312

1413
describe('Container', () => {

0 commit comments

Comments
 (0)