|
| 1 | +import { |
| 2 | + cleanupProject, |
| 3 | + newProject, |
| 4 | + uniq, |
| 5 | + createFile, |
| 6 | + runCLI, |
| 7 | + packageInstall, |
| 8 | + runCommand, |
| 9 | +} from '@nx/e2e/utils'; |
| 10 | + |
| 11 | +const TEN_MINS_MS = 600_000; |
| 12 | +describe('Playwright E2E Test runner', () => { |
| 13 | + beforeAll(() => { |
| 14 | + newProject({ name: uniq('playwright') }); |
| 15 | + packageInstall('@playwright/test', undefined, '^1.30.0', 'dev'); |
| 16 | + runCommand('npx playwright install --with-deps'); |
| 17 | + }); |
| 18 | + |
| 19 | + afterAll(() => cleanupProject()); |
| 20 | + |
| 21 | + it( |
| 22 | + 'should test example app', |
| 23 | + () => { |
| 24 | + //TODO: remove when generators are setup.Need to have basic workspace deps setup |
| 25 | + runCLI(`g @nx/js:init`); |
| 26 | + addSampleProject(); |
| 27 | + |
| 28 | + // NOTE: playwright throws errors if it detects running inside jest process. tmp remove and restore the env var for playwright to run |
| 29 | + const results = runCLI(`e2e demo-e2e`); |
| 30 | + expect(results).toContain('6 passed'); |
| 31 | + expect(results).toContain('Successfully ran target e2e for project'); |
| 32 | + }, |
| 33 | + TEN_MINS_MS |
| 34 | + ); |
| 35 | +}); |
| 36 | + |
| 37 | +// TODO: remove this when there are project generators |
| 38 | +function addSampleProject() { |
| 39 | + createFile( |
| 40 | + 'apps/demo-e2e/src/example.spec.ts', |
| 41 | + ` |
| 42 | +import { test, expect } from '@playwright/test'; |
| 43 | +
|
| 44 | +test('has title', async ({ page }) => { |
| 45 | + await page.goto('https://playwright.dev/'); |
| 46 | +
|
| 47 | + // Expect a title "to contain" a substring. |
| 48 | + await expect(page).toHaveTitle(/Playwright/); |
| 49 | +}); |
| 50 | +
|
| 51 | +test('get started link', async ({ page }) => { |
| 52 | + await page.goto('https://playwright.dev/'); |
| 53 | +
|
| 54 | + // Click the get started link. |
| 55 | + await page.getByRole('link', { name: 'Get started' }).click(); |
| 56 | +
|
| 57 | + // Expects the URL to contain intro. |
| 58 | + await expect(page).toHaveURL(/.*intro/); |
| 59 | +}); |
| 60 | +` |
| 61 | + ); |
| 62 | + createFile( |
| 63 | + 'apps/demo-e2e/playwright.config.ts', |
| 64 | + ` |
| 65 | +import { defineConfig, devices } from '@playwright/test'; |
| 66 | +
|
| 67 | +/** |
| 68 | + * Read environment variables from file. |
| 69 | + * https://github.com/motdotla/dotenv |
| 70 | + */ |
| 71 | +// require('dotenv').config(); |
| 72 | +
|
| 73 | +/** |
| 74 | + * See https://playwright.dev/docs/test-configuration. |
| 75 | + */ |
| 76 | +export default defineConfig({ |
| 77 | + testDir: './src', |
| 78 | + outputDir: '../../dist/playwright/apps/demo-e2e/output', |
| 79 | + /* Run tests in files in parallel */ |
| 80 | + fullyParallel: true, |
| 81 | + /* Fail the build on CI if you accidentally left test.only in the source code. */ |
| 82 | + forbidOnly: !!process.env.CI, |
| 83 | + /* Retry on CI only */ |
| 84 | + retries: process.env.CI ? 2 : 0, |
| 85 | + /* Opt out of parallel tests on CI. */ |
| 86 | + workers: process.env.CI ? 1 : undefined, |
| 87 | + /* Reporter to use. See https://playwright.dev/docs/test-reporters */ |
| 88 | + reporter: [ |
| 89 | + [ |
| 90 | + 'html', |
| 91 | + { |
| 92 | + outputFolder: |
| 93 | + '../../dist/playwright/apps/demo-e2e/playwright-report', |
| 94 | + }, |
| 95 | + ], |
| 96 | + ], |
| 97 | + /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ |
| 98 | + use: { |
| 99 | + /* Base URL to use in actions like await page.goto('/'). */ |
| 100 | + // baseURL: 'http://127.0.0.1:3000', |
| 101 | +
|
| 102 | + /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ |
| 103 | + trace: 'on-first-retry', |
| 104 | + }, |
| 105 | +
|
| 106 | + /* Configure projects for major browsers */ |
| 107 | + projects: [ |
| 108 | + { |
| 109 | + name: 'chromium', |
| 110 | + use: { ...devices['Desktop Chrome'] }, |
| 111 | + }, |
| 112 | +
|
| 113 | + { |
| 114 | + name: 'firefox', |
| 115 | + use: { ...devices['Desktop Firefox'] }, |
| 116 | + }, |
| 117 | +
|
| 118 | + { |
| 119 | + name: 'webkit', |
| 120 | + use: { ...devices['Desktop Safari'] }, |
| 121 | + }, |
| 122 | +
|
| 123 | + /* Test against mobile viewports. */ |
| 124 | + // { |
| 125 | + // name: 'Mobile Chrome', |
| 126 | + // use: { ...devices['Pixel 5'] }, |
| 127 | + // }, |
| 128 | + // { |
| 129 | + // name: 'Mobile Safari', |
| 130 | + // use: { ...devices['iPhone 12'] }, |
| 131 | + // }, |
| 132 | +
|
| 133 | + /* Test against branded browsers. */ |
| 134 | + // { |
| 135 | + // name: 'Microsoft Edge', |
| 136 | + // use: { ...devices['Desktop Edge'], channel: 'msedge' }, |
| 137 | + // }, |
| 138 | + // { |
| 139 | + // name: 'Google Chrome', |
| 140 | + // use: { ...devices['Desktop Chrome'], channel: 'chrome' }, |
| 141 | + // }, |
| 142 | + ], |
| 143 | +
|
| 144 | + /* Run your local dev server before starting the tests */ |
| 145 | + // webServer: { |
| 146 | + // command: 'npm run start', |
| 147 | + // url: 'http://127.0.0.1:3000', |
| 148 | + // reuseExistingServer: !process.env.CI, |
| 149 | + // }, |
| 150 | +}); |
| 151 | +` |
| 152 | + ); |
| 153 | + createFile( |
| 154 | + 'apps/demo-e2e/project.json', |
| 155 | + JSON.stringify( |
| 156 | + { |
| 157 | + name: 'demo-e2e', |
| 158 | + root: 'apps/demo-e2e', |
| 159 | + sourceRoot: 'apps/demo-e2e/src', |
| 160 | + targets: { |
| 161 | + e2e: { |
| 162 | + executor: '@nx/playwright:playwright', |
| 163 | + options: {}, |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + null, |
| 168 | + 2 |
| 169 | + ) |
| 170 | + ); |
| 171 | +} |
0 commit comments