@browserflow-ai/generator 0.0.6
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.
- package/dist/config-emit.d.ts +41 -0
- package/dist/config-emit.d.ts.map +1 -0
- package/dist/config-emit.js +191 -0
- package/dist/config-emit.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/locator-emit.d.ts +76 -0
- package/dist/locator-emit.d.ts.map +1 -0
- package/dist/locator-emit.js +239 -0
- package/dist/locator-emit.js.map +1 -0
- package/dist/locator-emit.test.d.ts +6 -0
- package/dist/locator-emit.test.d.ts.map +1 -0
- package/dist/locator-emit.test.js +425 -0
- package/dist/locator-emit.test.js.map +1 -0
- package/dist/playwright-ts.d.ts +97 -0
- package/dist/playwright-ts.d.ts.map +1 -0
- package/dist/playwright-ts.js +373 -0
- package/dist/playwright-ts.js.map +1 -0
- package/dist/playwright-ts.test.d.ts +6 -0
- package/dist/playwright-ts.test.d.ts.map +1 -0
- package/dist/playwright-ts.test.js +548 -0
- package/dist/playwright-ts.test.js.map +1 -0
- package/dist/visual-checks.d.ts +76 -0
- package/dist/visual-checks.d.ts.map +1 -0
- package/dist/visual-checks.js +195 -0
- package/dist/visual-checks.js.map +1 -0
- package/dist/visual-checks.test.d.ts +6 -0
- package/dist/visual-checks.test.d.ts.map +1 -0
- package/dist/visual-checks.test.js +188 -0
- package/dist/visual-checks.test.js.map +1 -0
- package/package.json +34 -0
- package/src/config-emit.ts +253 -0
- package/src/index.ts +57 -0
- package/src/locator-emit.test.ts +533 -0
- package/src/locator-emit.ts +310 -0
- package/src/playwright-ts.test.ts +704 -0
- package/src/playwright-ts.ts +519 -0
- package/src/visual-checks.test.ts +232 -0
- package/src/visual-checks.ts +294 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* config-emit.ts
|
|
3
|
+
* Generates playwright.config.ts content.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
PlaywrightConfigOptions,
|
|
8
|
+
PlaywrightProject,
|
|
9
|
+
GeneratedConfig,
|
|
10
|
+
} from '@browserflow-ai/core';
|
|
11
|
+
import Handlebars from 'handlebars';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Configuration for generating playwright.config.ts.
|
|
15
|
+
*/
|
|
16
|
+
export interface ConfigGeneratorOptions {
|
|
17
|
+
/** Base URL for tests */
|
|
18
|
+
baseUrl?: string;
|
|
19
|
+
/** Test directory */
|
|
20
|
+
testDir?: string;
|
|
21
|
+
/** Output directory for results */
|
|
22
|
+
outputDir?: string;
|
|
23
|
+
/** Playwright config options */
|
|
24
|
+
config?: PlaywrightConfigOptions;
|
|
25
|
+
/** Custom Handlebars template */
|
|
26
|
+
template?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Default playwright.config.ts template.
|
|
31
|
+
*/
|
|
32
|
+
const DEFAULT_CONFIG_TEMPLATE = `import { defineConfig, devices } from '@playwright/test';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Playwright configuration for BrowserFlow-generated tests.
|
|
36
|
+
* Generated: {{generatedAt}}
|
|
37
|
+
*/
|
|
38
|
+
export default defineConfig({
|
|
39
|
+
testDir: '{{testDir}}',
|
|
40
|
+
{{#if outputDir}}
|
|
41
|
+
outputDir: '{{outputDir}}',
|
|
42
|
+
{{/if}}
|
|
43
|
+
|
|
44
|
+
/* Run tests in files in parallel */
|
|
45
|
+
fullyParallel: true,
|
|
46
|
+
|
|
47
|
+
/* Fail the build on CI if you accidentally left test.only in the source code */
|
|
48
|
+
forbidOnly: !!process.env.CI,
|
|
49
|
+
|
|
50
|
+
/* Retry on CI only */
|
|
51
|
+
retries: process.env.CI ? {{retries}} : 0,
|
|
52
|
+
|
|
53
|
+
/* Opt out of parallel tests on CI */
|
|
54
|
+
workers: process.env.CI ? 1 : {{workers}},
|
|
55
|
+
|
|
56
|
+
/* Reporter to use */
|
|
57
|
+
reporter: '{{reporter}}',
|
|
58
|
+
|
|
59
|
+
/* Shared settings for all the projects below */
|
|
60
|
+
use: {
|
|
61
|
+
{{#if baseUrl}}
|
|
62
|
+
/* Base URL to use in actions like \`await page.goto('/')\` */
|
|
63
|
+
baseURL: '{{baseUrl}}',
|
|
64
|
+
{{/if}}
|
|
65
|
+
|
|
66
|
+
/* Collect trace when retrying the failed test */
|
|
67
|
+
trace: 'on-first-retry',
|
|
68
|
+
|
|
69
|
+
/* Take screenshot on failure */
|
|
70
|
+
screenshot: 'only-on-failure',
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
/* Test timeout */
|
|
74
|
+
timeout: {{timeout}},
|
|
75
|
+
|
|
76
|
+
/* Configure projects for major browsers */
|
|
77
|
+
projects: [
|
|
78
|
+
{{#each projects}}
|
|
79
|
+
{
|
|
80
|
+
name: '{{name}}',
|
|
81
|
+
use: {
|
|
82
|
+
...devices['{{deviceName}}'],
|
|
83
|
+
{{#if viewport}}
|
|
84
|
+
viewport: { width: {{viewport.width}}, height: {{viewport.height}} },
|
|
85
|
+
{{/if}}
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
{{/each}}
|
|
89
|
+
],
|
|
90
|
+
{{#if webServer}}
|
|
91
|
+
|
|
92
|
+
/* Run your local dev server before starting the tests */
|
|
93
|
+
webServer: {
|
|
94
|
+
command: '{{webServer.command}}',
|
|
95
|
+
url: '{{webServer.url}}',
|
|
96
|
+
reuseExistingServer: !process.env.CI,
|
|
97
|
+
},
|
|
98
|
+
{{/if}}
|
|
99
|
+
});
|
|
100
|
+
`;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Default browser projects configuration.
|
|
104
|
+
*/
|
|
105
|
+
const DEFAULT_PROJECTS: Array<{
|
|
106
|
+
name: string;
|
|
107
|
+
deviceName: string;
|
|
108
|
+
viewport?: { width: number; height: number };
|
|
109
|
+
}> = [
|
|
110
|
+
{ name: 'chromium', deviceName: 'Desktop Chrome' },
|
|
111
|
+
{ name: 'firefox', deviceName: 'Desktop Firefox' },
|
|
112
|
+
{ name: 'webkit', deviceName: 'Desktop Safari' },
|
|
113
|
+
];
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Generates a playwright.config.ts file.
|
|
117
|
+
*/
|
|
118
|
+
export function generatePlaywrightConfig(
|
|
119
|
+
options: ConfigGeneratorOptions = {}
|
|
120
|
+
): GeneratedConfig {
|
|
121
|
+
const {
|
|
122
|
+
baseUrl,
|
|
123
|
+
testDir = './tests',
|
|
124
|
+
outputDir,
|
|
125
|
+
config = {},
|
|
126
|
+
template = DEFAULT_CONFIG_TEMPLATE,
|
|
127
|
+
} = options;
|
|
128
|
+
|
|
129
|
+
const {
|
|
130
|
+
timeout = 30000,
|
|
131
|
+
retries = 2,
|
|
132
|
+
workers = 4,
|
|
133
|
+
reporter = 'html',
|
|
134
|
+
projects,
|
|
135
|
+
webServer,
|
|
136
|
+
} = config;
|
|
137
|
+
|
|
138
|
+
// Build projects data
|
|
139
|
+
const projectsData =
|
|
140
|
+
projects?.map((p) => ({
|
|
141
|
+
name: p.name,
|
|
142
|
+
deviceName: getDeviceName(p),
|
|
143
|
+
viewport: p.use.viewport,
|
|
144
|
+
})) ?? DEFAULT_PROJECTS;
|
|
145
|
+
|
|
146
|
+
// Compile template
|
|
147
|
+
const compiledTemplate = Handlebars.compile(template);
|
|
148
|
+
|
|
149
|
+
const content = compiledTemplate({
|
|
150
|
+
generatedAt: new Date().toISOString(),
|
|
151
|
+
testDir,
|
|
152
|
+
outputDir,
|
|
153
|
+
baseUrl,
|
|
154
|
+
timeout,
|
|
155
|
+
retries,
|
|
156
|
+
workers,
|
|
157
|
+
reporter,
|
|
158
|
+
projects: projectsData,
|
|
159
|
+
webServer,
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
path: 'playwright.config.ts',
|
|
164
|
+
content,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Maps a PlaywrightProject to a Playwright device name.
|
|
170
|
+
*/
|
|
171
|
+
function getDeviceName(project: PlaywrightProject): string {
|
|
172
|
+
const browserName = project.use.browserName ?? 'chromium';
|
|
173
|
+
|
|
174
|
+
const deviceMap: Record<string, string> = {
|
|
175
|
+
chromium: 'Desktop Chrome',
|
|
176
|
+
firefox: 'Desktop Firefox',
|
|
177
|
+
webkit: 'Desktop Safari',
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
return deviceMap[browserName] ?? 'Desktop Chrome';
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Generates a minimal playwright.config.ts for a single browser.
|
|
185
|
+
*/
|
|
186
|
+
export function generateMinimalConfig(options: {
|
|
187
|
+
baseUrl?: string;
|
|
188
|
+
testDir?: string;
|
|
189
|
+
browser?: 'chromium' | 'firefox' | 'webkit';
|
|
190
|
+
}): GeneratedConfig {
|
|
191
|
+
const { baseUrl, testDir = './tests', browser = 'chromium' } = options;
|
|
192
|
+
|
|
193
|
+
const deviceName = {
|
|
194
|
+
chromium: 'Desktop Chrome',
|
|
195
|
+
firefox: 'Desktop Firefox',
|
|
196
|
+
webkit: 'Desktop Safari',
|
|
197
|
+
}[browser];
|
|
198
|
+
|
|
199
|
+
const content = `import { defineConfig, devices } from '@playwright/test';
|
|
200
|
+
|
|
201
|
+
export default defineConfig({
|
|
202
|
+
testDir: '${testDir}',
|
|
203
|
+
fullyParallel: true,
|
|
204
|
+
forbidOnly: !!process.env.CI,
|
|
205
|
+
retries: process.env.CI ? 2 : 0,
|
|
206
|
+
workers: process.env.CI ? 1 : undefined,
|
|
207
|
+
reporter: 'html',
|
|
208
|
+
use: {
|
|
209
|
+
${baseUrl ? `baseURL: '${baseUrl}',` : ''}
|
|
210
|
+
trace: 'on-first-retry',
|
|
211
|
+
},
|
|
212
|
+
projects: [
|
|
213
|
+
{
|
|
214
|
+
name: '${browser}',
|
|
215
|
+
use: { ...devices['${deviceName}'] },
|
|
216
|
+
},
|
|
217
|
+
],
|
|
218
|
+
});
|
|
219
|
+
`;
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
path: 'playwright.config.ts',
|
|
223
|
+
content,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Generates package.json scripts for running Playwright tests.
|
|
229
|
+
*/
|
|
230
|
+
export function generatePlaywrightScripts(): Record<string, string> {
|
|
231
|
+
return {
|
|
232
|
+
'test:e2e': 'playwright test',
|
|
233
|
+
'test:e2e:headed': 'playwright test --headed',
|
|
234
|
+
'test:e2e:debug': 'playwright test --debug',
|
|
235
|
+
'test:e2e:ui': 'playwright test --ui',
|
|
236
|
+
'test:e2e:update-snapshots': 'playwright test --update-snapshots',
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Generates the commands needed to set up Playwright in a project.
|
|
242
|
+
*/
|
|
243
|
+
export function generateSetupInstructions(): string {
|
|
244
|
+
return `# Install Playwright
|
|
245
|
+
bun add -D @playwright/test
|
|
246
|
+
|
|
247
|
+
# Install browsers
|
|
248
|
+
bunx playwright install
|
|
249
|
+
|
|
250
|
+
# Run tests
|
|
251
|
+
bun run test:e2e
|
|
252
|
+
`;
|
|
253
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @browserflow-ai/generator
|
|
3
|
+
*
|
|
4
|
+
* Playwright test generator for BrowserFlow.
|
|
5
|
+
* Converts exploration lockfiles into deterministic Playwright Test code.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Main generator class
|
|
9
|
+
export { PlaywrightGenerator, generateTest } from './playwright-ts.js';
|
|
10
|
+
export type { TestGeneratorOptions } from './playwright-ts.js';
|
|
11
|
+
|
|
12
|
+
// Locator code generation
|
|
13
|
+
export {
|
|
14
|
+
generateLocatorCode,
|
|
15
|
+
resolveLocatorCode,
|
|
16
|
+
escapeString,
|
|
17
|
+
selectorToLocator,
|
|
18
|
+
roleLocator,
|
|
19
|
+
textLocator,
|
|
20
|
+
testIdLocator,
|
|
21
|
+
} from './locator-emit.js';
|
|
22
|
+
export type { LocatorEmitOptions } from './locator-emit.js';
|
|
23
|
+
|
|
24
|
+
// Visual check code generation
|
|
25
|
+
export {
|
|
26
|
+
generateScreenshotAssertion,
|
|
27
|
+
generateElementScreenshotAssertion,
|
|
28
|
+
generateScreenshotCapture,
|
|
29
|
+
generateScreenshotCompare,
|
|
30
|
+
generateVisualImports,
|
|
31
|
+
generateWaitForAnimations,
|
|
32
|
+
} from './visual-checks.js';
|
|
33
|
+
export type { ScreenshotOptions, VisualCheckEmitOptions } from './visual-checks.js';
|
|
34
|
+
|
|
35
|
+
// Config generation
|
|
36
|
+
export {
|
|
37
|
+
generatePlaywrightConfig,
|
|
38
|
+
generateMinimalConfig,
|
|
39
|
+
generatePlaywrightScripts,
|
|
40
|
+
generateSetupInstructions,
|
|
41
|
+
} from './config-emit.js';
|
|
42
|
+
export type { ConfigGeneratorOptions } from './config-emit.js';
|
|
43
|
+
|
|
44
|
+
// Re-export core types for convenience
|
|
45
|
+
export type {
|
|
46
|
+
ExplorationLockfile,
|
|
47
|
+
ExplorationStep,
|
|
48
|
+
GeneratorConfig,
|
|
49
|
+
GeneratedTest,
|
|
50
|
+
GeneratedConfig,
|
|
51
|
+
LocatorObject,
|
|
52
|
+
LocatorMethod,
|
|
53
|
+
LocatorArgs,
|
|
54
|
+
ReviewData,
|
|
55
|
+
PlaywrightConfigOptions,
|
|
56
|
+
PlaywrightProject,
|
|
57
|
+
} from '@browserflow-ai/core';
|