pull:初次提交
This commit is contained in:
48
n8n-n8n-1.109.2/packages/testing/playwright/utils/path-helper.ts
Executable file
48
n8n-n8n-1.109.2/packages/testing/playwright/utils/path-helper.ts
Executable file
@@ -0,0 +1,48 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
import { TestError } from '../Types';
|
||||
|
||||
/**
|
||||
* Finds the project root by searching upwards for a marker file.
|
||||
* @param marker The file that identifies the project root (e.g., 'playwright.config.ts' or 'package.json').
|
||||
* @returns The absolute path to the project root.
|
||||
*/
|
||||
function findProjectRoot(marker: string): string {
|
||||
let dir = __dirname;
|
||||
while (!fs.existsSync(path.join(dir, marker))) {
|
||||
const parentDir = path.dirname(dir);
|
||||
if (parentDir === dir) {
|
||||
throw new TestError('Could not find project root');
|
||||
}
|
||||
dir = parentDir;
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a folder root by searching upwards for a marker folder named 'packages'.
|
||||
* @returns The absolute path to the folder root.
|
||||
*/
|
||||
export function findPackagesRoot(marker: string): string {
|
||||
let dir = __dirname;
|
||||
while (!fs.existsSync(path.join(dir, marker))) {
|
||||
const parentDir = path.dirname(dir);
|
||||
if (parentDir === dir) {
|
||||
throw new TestError('Could not find packages root');
|
||||
}
|
||||
dir = parentDir;
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
const playwrightRoot = findProjectRoot('playwright.config.ts');
|
||||
|
||||
/**
|
||||
* Resolves a path relative to the Playwright project root.
|
||||
* @param pathSegments Segments of the path starting from the project root.
|
||||
* @returns An absolute path to the file or directory.
|
||||
*/
|
||||
export function resolveFromRoot(...pathSegments: string[]): string {
|
||||
return path.join(playwrightRoot, ...pathSegments);
|
||||
}
|
||||
30
n8n-n8n-1.109.2/packages/testing/playwright/utils/performance-helper.ts
Executable file
30
n8n-n8n-1.109.2/packages/testing/playwright/utils/performance-helper.ts
Executable file
@@ -0,0 +1,30 @@
|
||||
import type { Page } from '@playwright/test';
|
||||
|
||||
export async function measurePerformance(
|
||||
page: Page,
|
||||
actionName: string,
|
||||
actionFn: () => Promise<void>,
|
||||
): Promise<number> {
|
||||
// Mark start
|
||||
await page.evaluate((name) => performance.mark(`${name}-start`), actionName);
|
||||
|
||||
// Execute action
|
||||
await actionFn();
|
||||
|
||||
// Mark end and get duration
|
||||
return await page.evaluate((name) => {
|
||||
performance.mark(`${name}-end`);
|
||||
performance.measure(name, `${name}-start`, `${name}-end`);
|
||||
const measure = performance.getEntriesByName(name)[0] as PerformanceMeasure;
|
||||
return measure.duration;
|
||||
}, actionName);
|
||||
}
|
||||
|
||||
export async function getAllPerformanceMetrics(page: Page) {
|
||||
return await page.evaluate(() => {
|
||||
const metrics: Record<string, number> = {};
|
||||
const measures = performance.getEntriesByType('measure') as PerformanceMeasure[];
|
||||
measures.forEach((m) => (metrics[m.name] = m.duration));
|
||||
return metrics;
|
||||
});
|
||||
}
|
||||
70
n8n-n8n-1.109.2/packages/testing/playwright/utils/requirements.ts
Executable file
70
n8n-n8n-1.109.2/packages/testing/playwright/utils/requirements.ts
Executable file
@@ -0,0 +1,70 @@
|
||||
import type { Page, BrowserContext } from '@playwright/test';
|
||||
|
||||
import { setContextSettings } from '../config/intercepts';
|
||||
import { n8nPage } from '../pages/n8nPage';
|
||||
import { ApiHelpers } from '../services/api-helper';
|
||||
import { TestError, type TestRequirements } from '../Types';
|
||||
|
||||
export async function setupTestRequirements(
|
||||
page: Page,
|
||||
context: BrowserContext,
|
||||
requirements: TestRequirements,
|
||||
): Promise<void> {
|
||||
const n8n = new n8nPage(page);
|
||||
const api = new ApiHelpers(context.request);
|
||||
|
||||
// 1. Setup frontend settings override
|
||||
if (requirements.config?.settings) {
|
||||
// Store settings for this context
|
||||
setContextSettings(context, requirements.config.settings);
|
||||
}
|
||||
|
||||
// 2. Setup feature flags
|
||||
if (requirements.config?.features) {
|
||||
for (const [feature, enabled] of Object.entries(requirements.config.features)) {
|
||||
if (enabled) {
|
||||
await api.enableFeature(feature);
|
||||
} else {
|
||||
await api.disableFeature(feature);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Setup API intercepts
|
||||
if (requirements.intercepts) {
|
||||
for (const [name, config] of Object.entries(requirements.intercepts)) {
|
||||
await page.route(config.url, async (route) => {
|
||||
await route.fulfill({
|
||||
status: config.status ?? 200,
|
||||
contentType: config.contentType ?? 'application/json',
|
||||
body:
|
||||
typeof config.response === 'string' ? config.response : JSON.stringify(config.response),
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Setup workflows
|
||||
if (requirements.workflow) {
|
||||
for (const [name, workflowData] of Object.entries(requirements.workflow)) {
|
||||
try {
|
||||
// Import workflow using the n8n page object
|
||||
await n8n.goHome();
|
||||
await n8n.workflows.clickAddWorkflowButton();
|
||||
await n8n.canvas.importWorkflow(name, workflowData);
|
||||
} catch (error) {
|
||||
throw new TestError(`Failed to create workflow ${name}: ${String(error)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Setup browser storage
|
||||
if (requirements.storage) {
|
||||
await context.addInitScript((storage) => {
|
||||
// Set localStorage items
|
||||
for (const [key, value] of Object.entries(storage)) {
|
||||
window.localStorage.setItem(key, value);
|
||||
}
|
||||
}, requirements.storage);
|
||||
}
|
||||
}
|
||||
7
n8n-n8n-1.109.2/packages/testing/playwright/utils/url-helper.ts
Executable file
7
n8n-n8n-1.109.2/packages/testing/playwright/utils/url-helper.ts
Executable file
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Extract port from a URL string
|
||||
*/
|
||||
export function getPortFromUrl(url: string): string {
|
||||
const parsedUrl = new URL(url);
|
||||
return parsedUrl.port || (parsedUrl.protocol === 'https:' ? '443' : '80');
|
||||
}
|
||||
Reference in New Issue
Block a user