Skip to content

Commit f4a50e7

Browse files
committed
fix helper tests
1 parent 34699bd commit f4a50e7

File tree

9 files changed

+143
-88
lines changed

9 files changed

+143
-88
lines changed

lib/helper/Puppeteer.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,11 +2704,20 @@ async function findElements(matcher, locator) {
27042704
if (locator.react) return findReactElements.call(this, locator)
27052705
locator = new Locator(locator, 'css')
27062706
if (!locator.isXPath()) return matcher.$$(locator.simplify())
2707-
// puppeteer version < 19.4.0 is no longer supported. This one is backward support.
2708-
if (puppeteer.default?.defaultBrowserRevision) {
2709-
return matcher.$$(`xpath/${locator.value}`)
2707+
2708+
// Handle backward compatibility for different Puppeteer versions
2709+
// Puppeteer >= 19.4.0 uses xpath/ syntax, older versions use $x
2710+
try {
2711+
// Try the new xpath syntax first (for Puppeteer >= 19.4.0)
2712+
return await matcher.$$(`xpath/${locator.value}`)
2713+
} catch (error) {
2714+
// Fall back to the old $x method for older Puppeteer versions
2715+
if (matcher.$x && typeof matcher.$x === 'function') {
2716+
return await matcher.$x(locator.value)
2717+
}
2718+
// If both methods fail, re-throw the original error
2719+
throw error
27102720
}
2711-
return matcher.$x(locator.value)
27122721
}
27132722

27142723
async function proceedClick(locator, context = null, options = {}) {

lib/helper/TestCafe.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
// @ts-nocheck
2-
const fs = require('fs')
3-
const assert = require('assert')
4-
const path = require('path')
5-
const qrcode = require('qrcode-terminal')
6-
const createTestCafe = require('testcafe')
2+
import fs from 'fs'
3+
import assert from 'assert'
4+
import path from 'path'
5+
import qrcode from 'qrcode-terminal'
6+
import testcafeModule from 'testcafe'
7+
import { createRequire } from 'module'
8+
const require = createRequire(import.meta.url)
9+
const createTestCafe = testcafeModule.default || testcafeModule
710
const { Selector, ClientFunction } = require('testcafe')
811

9-
const HelperModule = require('@codeceptjs/helper')
10-
const ElementNotFound = require('./errors/ElementNotFound')
11-
const testControllerHolder = require('./testcafe/testControllerHolder')
12-
const { mapError, createTestFile, createClientFunction } = require('./testcafe/testcafe-utils')
12+
import Helper from '@codeceptjs/helper'
13+
import ElementNotFound from './errors/ElementNotFound.js'
14+
import testControllerHolder from './testcafe/testControllerHolder.js'
15+
import { mapError, createTestFile, createClientFunction } from './testcafe/testcafe-utils.js'
1316

1417
const includeModule = require('../assert/include')
1518
const stringIncludes = (includeModule.default || includeModule).includes
1619
const equalModule = require('../assert/equal')
1720
const emptyModule = require('../assert/empty')
1821
const truthModule = require('../assert/truth')
19-
const { xpathLocator, normalizeSpacesInString } = require('../utils')
20-
const Locator = require('../locator')
22+
import { xpathLocator, normalizeSpacesInString } from '../utils.js'
23+
import Locator from '../locator.js'
2124

2225
/**
2326
* Client Functions
@@ -1389,4 +1392,4 @@ async function findFields(locator) {
13891392
return this._locate({ css: locator })
13901393
}
13911394

1392-
module.exports = TestCafe
1395+
export default TestCafe
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const testControllerHolder = {
2+
testController: undefined,
3+
captureResolver: undefined,
4+
getResolver: undefined,
5+
6+
capture(t) {
7+
testControllerHolder.testController = t
8+
9+
if (testControllerHolder.getResolver) {
10+
// @ts-ignore
11+
testControllerHolder.getResolver(t)
12+
}
13+
14+
return new Promise(resolve => {
15+
// @ts-ignore
16+
testControllerHolder.captureResolver = resolve
17+
})
18+
},
19+
20+
free() {
21+
testControllerHolder.testController = undefined
22+
23+
if (testControllerHolder.captureResolver) {
24+
// @ts-ignore
25+
testControllerHolder.captureResolver()
26+
}
27+
},
28+
29+
get() {
30+
return new Promise(resolve => {
31+
if (testControllerHolder.testController) {
32+
resolve(testControllerHolder.testController)
33+
} else {
34+
// @ts-ignore
35+
testControllerHolder.getResolver = resolve
36+
}
37+
})
38+
},
39+
}
40+
41+
module.exports = testControllerHolder
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
11
const testControllerHolder = {
2-
32
testController: undefined,
43
captureResolver: undefined,
54
getResolver: undefined,
65

76
capture(t) {
8-
testControllerHolder.testController = t;
7+
testControllerHolder.testController = t
98

109
if (testControllerHolder.getResolver) {
1110
// @ts-ignore
12-
testControllerHolder.getResolver(t);
11+
testControllerHolder.getResolver(t)
1312
}
1413

15-
return new Promise((resolve) => {
14+
return new Promise(resolve => {
1615
// @ts-ignore
17-
testControllerHolder.captureResolver = resolve;
18-
});
16+
testControllerHolder.captureResolver = resolve
17+
})
1918
},
2019

2120
free() {
22-
testControllerHolder.testController = undefined;
21+
testControllerHolder.testController = undefined
2322

2423
if (testControllerHolder.captureResolver) {
2524
// @ts-ignore
26-
testControllerHolder.captureResolver();
25+
testControllerHolder.captureResolver()
2726
}
2827
},
2928

3029
get() {
31-
return new Promise((resolve) => {
30+
return new Promise(resolve => {
3231
if (testControllerHolder.testController) {
33-
resolve(testControllerHolder.testController);
32+
resolve(testControllerHolder.testController)
3433
} else {
3534
// @ts-ignore
36-
testControllerHolder.getResolver = resolve;
35+
testControllerHolder.getResolver = resolve
3736
}
38-
});
37+
})
3938
},
40-
};
39+
}
4140

42-
module.exports = testControllerHolder;
41+
export default testControllerHolder

lib/helper/testcafe/testcafe-utils.js

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import { createRequire } from 'module'
2+
import { fileURLToPath } from 'url'
3+
import { dirname } from 'path'
4+
const require = createRequire(import.meta.url)
15
const { ClientFunction } = require('testcafe')
6+
const __filename = fileURLToPath(import.meta.url)
7+
const __dirname = dirname(__filename)
28

3-
const assert = require('assert')
4-
const fs = require('fs')
5-
const path = require('path')
6-
const { getParamNames } = require('../../utils')
9+
import assert from 'assert'
10+
import fs from 'fs'
11+
import path from 'path'
12+
import { getParamNames } from '../../utils.js'
713

814
const createTestFile = () => {
915
assert(global.output_dir, 'global.output_dir must be set')
@@ -13,7 +19,7 @@ const createTestFile = () => {
1319

1420
fs.writeFileSync(
1521
testFile,
16-
`import testControllerHolder from "${testControllerHolderDir}/testControllerHolder.js";\n\n
22+
`const testControllerHolder = require("${testControllerHolderDir}/testControllerHolder.cjs");\n\n
1723
fixture("fixture")\n
1824
test\n
1925
("test", testControllerHolder.capture)`,
@@ -22,40 +28,26 @@ const createTestFile = () => {
2228
return testFile
2329
}
2430

25-
// TODO Better error mapping (actual, expected)
26-
const mapError = testcafeError => {
27-
// console.log('TODO map error better', JSON.stringify(testcafeError, null, 2));
28-
if (testcafeError.errMsg) {
29-
throw new Error(testcafeError.errMsg)
30-
}
31-
const errorInfo = `${testcafeError.callsite ? JSON.stringify(testcafeError.callsite) : ''} ${testcafeError.apiFnChain || JSON.stringify(testcafeError)}`
32-
throw new Error(`TestCafe Error: ${errorInfo}`)
33-
}
31+
const mapError = testcafeErr => {
32+
if (!testcafeErr) return new Error('Unknown error')
3433

35-
function createClientFunction(func, args) {
36-
if (!args || !args.length) {
37-
return ClientFunction(func)
34+
const code = testcafeErr.code
35+
if (code && testcafeErr.callsite) {
36+
return testcafeErr
3837
}
39-
const paramNames = getParamNames(func)
40-
const dependencies = {}
41-
paramNames.forEach((param, i) => (dependencies[param] = args[i]))
4238

43-
return ClientFunction(getFuncBody(func), { dependencies })
39+
const stack = testcafeErr.stack || testcafeErr.toString()
40+
const message = testcafeErr.message || testcafeErr.toString()
41+
const error = new Error(message)
42+
error.stack = stack
43+
return error
4444
}
4545

46-
function getFuncBody(func) {
47-
let fnStr = func.toString()
48-
const arrowIndex = fnStr.indexOf('=>')
49-
if (arrowIndex >= 0) {
50-
fnStr = fnStr.slice(arrowIndex + 2)
46+
const createClientFunction = clientFn => {
47+
const fnString = clientFn.toString()
48+
const params = getParamNames(clientFn)
5149

52-
return eval(`() => ${fnStr}`)
53-
}
54-
// TODO: support general functions
50+
return ClientFunction(fnString, { dependencies: {} })
5551
}
5652

57-
module.exports = {
58-
createTestFile,
59-
mapError,
60-
createClientFunction,
61-
}
53+
export { createTestFile, mapError, createClientFunction }

test/helper/Playwright_test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const expect = chai.expect
66
import path from 'path'
77
import fs from 'fs'
88

9-
import playwright from 'playwright'
9+
import playwright, { devices } from 'playwright'
10+
import electron from 'electron'
1011

1112
import TestHelper from '../support/TestHelper.js'
1213
import Playwright from '../../lib/helper/Playwright.js'
@@ -31,6 +32,7 @@ const formContents = testUtils.submittedData(dataFile)
3132

3233
let I
3334
let page
35+
let browser
3436
let FS
3537
const siteUrl = TestHelper.siteUrl()
3638

@@ -1248,7 +1250,6 @@ describe('Playwright - BasicAuth', function () {
12481250

12491251
describe('Playwright - Emulation', () => {
12501252
before(() => {
1251-
const { devices } = require('playwright')
12521253
global.codecept_dir = path.join(__dirname, '/../data')
12531254

12541255
I = new Playwright({
@@ -1333,7 +1334,7 @@ describe('Playwright - Electron', () => {
13331334
restart: true,
13341335
browser: 'electron',
13351336
electron: {
1336-
executablePath: require('electron'),
1337+
executablePath: electron,
13371338
args: [path.join(codecept_dir, '/electron/')],
13381339
},
13391340
})

test/helper/TestCafe_test.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
1-
const path = require('path')
2-
const assert = require('assert')
1+
import path from 'path'
2+
import assert from 'assert'
3+
import { fileURLToPath } from 'url'
4+
import { dirname } from 'path'
35

4-
const TestHelper = require('../support/TestHelper')
5-
const TestCafe = require('../../lib/helper/TestCafe')
6-
const webApiTests = require('./webapi')
7-
global.codeceptjs = require('../../lib')
6+
import TestHelper from '../support/TestHelper.js'
7+
import TestCafe from '../../lib/helper/TestCafe.js'
8+
import * as webApiTests from './webapi.js'
9+
import codeceptjs from '../../lib/index.js'
10+
global.codeceptjs = codeceptjs
11+
12+
const __filename = fileURLToPath(import.meta.url)
13+
const __dirname = dirname(__filename)
814

915
let I
1016
const siteUrl = TestHelper.siteUrl()
1117

1218
describe('TestCafe', function () {
13-
this.timeout(35000)
19+
this.timeout(120000) // Increased timeout for TestCafe browser setup
1420
this.retries(1)
1521

1622
before(() => {
1723
global.codecept_dir = path.join(__dirname, '/../data')
1824
global.output_dir = path.join(__dirname, '/../data/output')
19-
global.codeceptjs = require('../../lib/index')
25+
global.codeceptjs = codeceptjs
2026

2127
I = new TestCafe({
2228
url: siteUrl,
2329
windowSize: '1000x700',
2430
show: false,
25-
browser: 'chromium',
31+
browser: 'chrome',
2632
restart: false,
2733
waitForTimeout: 5000,
2834
})

test/helper/WebDriver.noSeleniumServer_test.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
const assert = require('assert')
2-
3-
const chai = require('chai')
1+
import chai from 'chai'
2+
import path from 'path'
3+
import fs from 'fs'
4+
import { fileURLToPath } from 'url'
45

56
const expect = chai.expect
7+
const assert = chai.assert
8+
const __filename = fileURLToPath(import.meta.url)
9+
const __dirname = path.dirname(__filename)
610

7-
const path = require('path')
8-
const fs = require('fs')
11+
import TestHelper from '../support/TestHelper.js'
12+
import WebDriver from '../../lib/helper/WebDriver.js'
13+
import AssertionFailedError from '../../lib/assert/error.js'
14+
import { secret as Secret } from '../../lib/secret.js'
15+
import * as codeceptjs from '../../lib/index.js'
16+
global.codeceptjs = codeceptjs
917

10-
const TestHelper = require('../support/TestHelper')
11-
const WebDriver = require('../../lib/helper/WebDriver')
12-
const AssertionFailedError = require('../../lib/assert/error')
13-
const Secret = require('../../lib/secret')
14-
global.codeceptjs = require('../../lib')
18+
const dataFile = path.join(__dirname, '/../data/app/db')
1519

1620
const siteUrl = TestHelper.siteUrl()
1721
let wd
@@ -382,7 +386,6 @@ describe('WebDriver - No Selenium server started', function () {
382386
})
383387
})
384388

385-
386389
describe('#seeTitleEquals', () => {
387390
it('should check that title is equal to provided one', async () => {
388391
await wd.amOnPage('/')

test/helper/webapi.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const store = storeModule.default || storeModule
44
const expect = chai.expect
55
const assert = chai.assert
66
import path from 'path'
7+
import fs from 'fs'
78
import { fileURLToPath } from 'url'
89
import { dirname } from 'path'
910

@@ -33,7 +34,7 @@ export function tests() {
3334
beforeEach(() => {
3435
I = data.I
3536
siteUrl = data.siteUrl
36-
if (fileExists(dataFile)) require('fs').unlinkSync(dataFile)
37+
if (fileExists(dataFile)) fs.unlinkSync(dataFile)
3738
})
3839

3940
describe('#saveElementScreenshot', () => {

0 commit comments

Comments
 (0)