diff --git a/packages/playwright/package.json b/packages/playwright/package.json index 620cb529022caf..f85b790be75b23 100644 --- a/packages/playwright/package.json +++ b/packages/playwright/package.json @@ -16,8 +16,8 @@ "Playwright", "CLI" ], - "main": "./index", - "typings": "./index.d.ts", + "main": "./src/index", + "typings": "./src/index.d.ts", "author": "Victor Savkin", "license": "MIT", "bugs": { diff --git a/packages/playwright/src/generators/configuration/configuration.ts b/packages/playwright/src/generators/configuration/configuration.ts index a0c04a8cbd2c97..ab01817d9f3af5 100644 --- a/packages/playwright/src/generators/configuration/configuration.ts +++ b/packages/playwright/src/generators/configuration/configuration.ts @@ -22,6 +22,8 @@ export async function configurationGenerator( generateFiles(tree, path.join(__dirname, 'files'), projectConfig.root, { offsetFromRoot: offsetFromRoot(projectConfig.root), projectRoot: projectConfig.root, + webServerCommand: options.webServerCommand ?? null, + webServerAddress: options.webServerAddress ?? null, ...options, }); @@ -67,8 +69,10 @@ function addE2eTarget(tree: Tree, options: ConfigurationGeneratorSchema) { throw new Error(`Project ${options.project} already has an e2e target. Rename or remove the existing e2e target.`); } + projectConfig.targets ??= {}; projectConfig.targets.e2e = { executor: '@nx/playwright:playwright', + outputs: [`dist/playwright/${projectConfig.root}`], options: {}, }; updateProjectConfiguration(tree, options.project, projectConfig); diff --git a/packages/playwright/src/generators/configuration/files/playwright.config.ts.template b/packages/playwright/src/generators/configuration/files/playwright.config.ts.template index f5067f8a646241..95630a9b6d0951 100644 --- a/packages/playwright/src/generators/configuration/files/playwright.config.ts.template +++ b/packages/playwright/src/generators/configuration/files/playwright.config.ts.template @@ -11,6 +11,8 @@ import { defineConfig, devices } from '@playwright/test'; */ export default defineConfig({ testDir: './<%= directory %>', + /* When updating this path, make sure to update the outputs in the project.json */ + outputDir: '<%= offsetFromRoot %>dist/playwright/<%= projectRoot %>/test-output', /* Run tests in files in parallel */ fullyParallel: true, /* Fail the build on CI if you accidentally left test.only in the source code. */ @@ -25,7 +27,7 @@ export default defineConfig({ 'html', { outputFolder: - '<%= offsetFromRoot %>/dist/playwright/<%= projectRoot %>/playwright-report', + '<%= offsetFromRoot %>dist/playwright/<%= projectRoot %>/playwright-report', }, ], ], @@ -76,10 +78,14 @@ export default defineConfig({ // }, ], - /* Run your local dev server before starting the tests */ - // webServer: { + /* Run your local dev server before starting the tests */<% if(webServerCommand && webServerAddress) {%> + webServer: { + command: '<%= webServerCommand %>', + url: '<%= webServerAddress %>', + reuseExistingServer: !process.env.CI, + },<% } else {%>// webServer: { // command: 'npm run start', // url: 'http://127.0.0.1:3000', // reuseExistingServer: !process.env.CI, - // }, + // },<% } %> }); diff --git a/packages/playwright/src/generators/configuration/schema.d.ts b/packages/playwright/src/generators/configuration/schema.d.ts index 99213e3ed939fa..73fb2258298f36 100644 --- a/packages/playwright/src/generators/configuration/schema.d.ts +++ b/packages/playwright/src/generators/configuration/schema.d.ts @@ -4,4 +4,14 @@ export interface ConfigurationGeneratorSchema { js: boolean; // default is false skipFormat: boolean; skipPackageJson: boolean; + /** + * command to give playwright to run the web server + * @example: "npx nx serve my-fe-app" + **/ + webServerCommand?: string; + /** + * address + * @example: "http://localhost:4200" + **/ + webServerAddress?: string; } diff --git a/packages/playwright/src/generators/configuration/schema.json b/packages/playwright/src/generators/configuration/schema.json index 26fded6567b080..6d3b2ad2e66322 100644 --- a/packages/playwright/src/generators/configuration/schema.json +++ b/packages/playwright/src/generators/configuration/schema.json @@ -25,6 +25,14 @@ "description": "Generate JavaScript files rather than TypeScript files.", "default": false }, + "webServerCommand": { + "type": "string", + "description": "The command to start the web server." + }, + "webServerAddress": { + "type": "string", + "description": "The address of the web server." + }, "skipFormat": { "description": "Skip formatting files.", "type": "boolean", diff --git a/packages/playwright/src/generators/init/init.ts b/packages/playwright/src/generators/init/init.ts index f904c5d10b7274..085d4b083ab03c 100644 --- a/packages/playwright/src/generators/init/init.ts +++ b/packages/playwright/src/generators/init/init.ts @@ -5,6 +5,7 @@ import { GeneratorCallback, runTasksInSerial, Tree, + updateJson, } from '@nx/devkit'; import { InitGeneratorSchema } from './schema'; import { nxVersion, playwrightVersion } from '../../utils/versions'; @@ -26,6 +27,30 @@ export async function initGenerator(tree: Tree, options: InitGeneratorSchema) { if (!options.skipFormat) { await formatFiles(tree); } + + if (tree.exists('.vscode/extensions.json')) { + updateJson(tree, '.vscode/extensions.json', (json) => { + json.recommendations ??= []; + + const recs = new Set(json.recommendations); + recs.add('ms-playwright.playwright'); + + json.recommendations = Array.from(recs); + return json; + }); + } else { + tree.write( + '.vscode/extensions.json', + JSON.stringify( + { + recommendations: ['ms-playwright.playwright'], + }, + null, + 2 + ) + ); + } + return runTasksInSerial(...tasks); } diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.ts index 8b96dbdf4ae12a..d404de56a7403e 100644 --- a/packages/playwright/src/index.ts +++ b/packages/playwright/src/index.ts @@ -2,3 +2,5 @@ export { playwrightExecutor, PlaywrightExecutorSchema, } from './executors/playwright/playwright'; +export { initGenerator } from './generators/init/init'; +export { configurationGenerator } from './generators/configuration/configuration';