-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
220 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,26 @@ | ||
import {Runner, TestCase, Plugins} from '@cgauge/dtc' | ||
import {Runner, TestCaseExecution, Plugins} from '@cgauge/dtc' | ||
import {spawnSync} from 'node:child_process' | ||
import nodeAssert from 'node:assert' | ||
import * as url from 'url' | ||
|
||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) | ||
|
||
export class PlaywrightRunner implements Runner { | ||
constructor(private args: string[] = []) {} | ||
|
||
async run(path: string, _testCase: TestCase[], _plugins: Plugins, type: string, config?: string) { | ||
spawnSync(`npx playwright test ${this.args.join(' ')} ${__dirname}`, { | ||
async run(testCaseExecutions: TestCaseExecution[], _plugins: Plugins, type: string, config?: string) { | ||
const childProcess = spawnSync(`npx playwright test ${this.args.join(' ')} ${__dirname}`, { | ||
stdio: 'inherit', | ||
shell: true, | ||
cwd: process.cwd(), | ||
env: { | ||
...process.env, | ||
DTC_PLAYWRIGHT_FILE_PATH: path, | ||
...(testCaseExecutions.length === 1 && {DTC_PLAYWRIGHT_PATH: testCaseExecutions[0].filePath}), | ||
DTC_PLAYWRIGHT_TYPE: type, | ||
DTC_PLAYWRIGHT_CONFIG: config, | ||
}, | ||
}) | ||
|
||
nodeAssert.equal(childProcess.status, 0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import {executeTestCase, resolveConfig} from '@cgauge/dtc' | ||
import {test} from '@playwright/test' | ||
|
||
const path = String(process.env.DTC_PLAYWRIGHT_FILE_PATH) | ||
const path = process.env.DTC_PLAYWRIGHT_PATH | ||
const type = String(process.env.DTC_PLAYWRIGHT_TYPE) | ||
const config = String(process.env.DTC_PLAYWRIGHT_CONFIG) | ||
|
||
const {testCases, plugins} = await resolveConfig(path, config) | ||
const {testCaseExecutions, plugins} = await resolveConfig(path, config) | ||
|
||
for (const testCase of testCases) { | ||
test(testCase.name, async ({page}) => executeTestCase(testCase[type], plugins[type], {page})) | ||
for (const {filePath, testCase} of testCaseExecutions) { | ||
test(testCase.name, async ({page}) => executeTestCase(testCase[type], plugins[type], filePath, {page})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {fileURLToPath} from 'url' | ||
import {dirname} from 'path' | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
|
||
export default { | ||
name: 'Test case 2', | ||
playwright: { | ||
arrange: { | ||
url: `file://${__dirname}/login.html`, | ||
actions: [ | ||
{ | ||
target: 'Username', | ||
fill: '', | ||
}, | ||
{ | ||
target: { | ||
name: 'getByPlaceholder', | ||
args: ['Your password'], | ||
}, | ||
action: { | ||
name: 'fill', | ||
args: [''], | ||
}, | ||
}, | ||
{ | ||
target: { | ||
name: 'getByRole', | ||
args: [ | ||
'button', | ||
{ | ||
type: 'submit', | ||
}, | ||
], | ||
}, | ||
action: { | ||
name: 'click', | ||
}, | ||
}, | ||
{ | ||
target: "input#username[required]:invalid", | ||
toBeVisible: true, | ||
}, | ||
], | ||
}, | ||
act: { | ||
url: `file://${__dirname}/index.html`, | ||
}, | ||
assert: { | ||
playwright: [ | ||
{ | ||
target: "Index page", | ||
toBeVisible: true, | ||
}, | ||
], | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,80 @@ | ||
import type {Runner, Plugins, TestCase} from './domain' | ||
import {defaultPlugins, defaultTestRunner} from './index.js' | ||
import type {Runner, Plugins, TestCase, TestCaseExecution} from './domain' | ||
import {defaultLoader, defaultPlugins, defaultTestRunner} from './index.js' | ||
import {readdirSync, statSync} from 'fs' | ||
import {join} from 'path' | ||
|
||
export type Config = { | ||
plugins: Plugins | ||
loader: (filePath: string) => TestCase | ||
loader: (filePath: string) => Promise<TestCase> | ||
runner: Runner | ||
testDir: string | ||
testRegex: string | ||
} | ||
|
||
export const resolveConfig = async (fileNamePath: string | null, config?: string) => { | ||
export const resolveConfig = async (filePath?: string, configPath?: string) => { | ||
const projectPath = process.cwd() | ||
let runner: Runner = defaultTestRunner | ||
let loader: ((filePath: string) => TestCase) | null = null | ||
let testRegex = /.*\.dtc\.[jt]s?$/ | ||
let plugins = defaultPlugins | ||
let loader = defaultLoader | ||
let testRegex = /.*\.dtc\.[jt]s?$/ | ||
|
||
if (config) { | ||
if (configPath) { | ||
const { | ||
plugins: customPlugins, | ||
loader: customLoad, | ||
runner: customTestRunner, | ||
testRegex: customTestRegex, | ||
} = (await import(`${process.cwd()}/${config}`)).default | ||
} = await defaultLoader(`${process.cwd()}/${configPath}`) | ||
|
||
runner = customTestRunner ?? defaultTestRunner | ||
testRegex = customTestRegex ?? testRegex | ||
loader = customLoad ?? loader | ||
plugins = customPlugins ? customPlugins : defaultPlugins | ||
testRegex = customTestRegex ?? testRegex | ||
} | ||
|
||
const testCases = await getTestCases(projectPath, fileNamePath, loader, testRegex) | ||
return {testCases, plugins, runner} | ||
const testCaseExecutions = await getTestCases(projectPath, loader, testRegex, filePath) | ||
|
||
return {testCaseExecutions, plugins, runner} | ||
} | ||
|
||
const generateTestCaseExecution = async (filePath: string, loader: Config['loader']): Promise<TestCaseExecution> => ({ | ||
filePath, | ||
testCase: await loader(filePath), | ||
}) | ||
|
||
const getTestCases = async ( | ||
projectPath: string, | ||
fileNamePath: string | null, | ||
loader: ((filePath: string) => TestCase) | null, | ||
loader: Config['loader'] | null, | ||
testRegex: RegExp, | ||
): Promise<TestCase[]> => { | ||
if (fileNamePath) { | ||
fileNamePath = `${projectPath}/${fileNamePath}` | ||
const testCase = loader ? await loader(fileNamePath) : (await import(fileNamePath)).default | ||
|
||
return [ | ||
{ | ||
...testCase, | ||
fileName: fileNamePath, | ||
}, | ||
] | ||
filePath?: string, | ||
): Promise<TestCaseExecution[]> => { | ||
if (filePath) { | ||
return loader | ||
? [await generateTestCaseExecution(filePath, loader)] | ||
: [await generateTestCaseExecution(filePath, defaultLoader)] | ||
} | ||
|
||
const files = loadTestFiles(projectPath, testRegex) | ||
return await Promise.all( | ||
files.map(async (file) => { | ||
const testCase = loader ? await loader(file) : (await import(file)).default | ||
|
||
return { | ||
...testCase, | ||
fileName: file, | ||
} | ||
}), | ||
return await Promise.all( | ||
files.map((file) => | ||
loader ? generateTestCaseExecution(file, loader) : generateTestCaseExecution(file, defaultLoader), | ||
), | ||
) | ||
} | ||
|
||
export const loadTestFiles = (dir: string, testRegex: RegExp, fileList: string[] = []): string[] => { | ||
readdirSync(dir).forEach((file) => { | ||
const filePath = join(dir, file) | ||
export const loadTestFiles = (currentPath: string, testRegex: RegExp): string[] => | ||
readdirSync(currentPath) | ||
.map((file) => { | ||
const filePath = join(currentPath, file) | ||
|
||
if (statSync(filePath).isDirectory()) { | ||
loadTestFiles(filePath, testRegex, fileList) | ||
} else if (testRegex.test(filePath)) { | ||
fileList.push(filePath) | ||
} | ||
}) | ||
if (statSync(filePath).isDirectory()) { | ||
return loadTestFiles(filePath, testRegex) | ||
} else if (testRegex.test(filePath)) { | ||
return filePath | ||
} | ||
|
||
return fileList | ||
} | ||
return null | ||
}) | ||
.filter((item) => item !== null) | ||
.flat() |
Oops, something went wrong.