-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(test): use @playwright/test instead of jasmine (#4260)
- Loading branch information
Showing
52 changed files
with
1,509 additions
and
989 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,24 @@ | ||
import {PlaywrightTestConfig} from '@playwright/test'; | ||
|
||
export const baseURL = "http://localhost:4200/#"; | ||
|
||
const config: PlaywrightTestConfig = { | ||
reporter: process.env.CI ? 'github' : 'list', | ||
testDir: "src", | ||
testMatch: /\.e2e-spec\.ts$/, | ||
timeout: 60000, | ||
forbidOnly: !!process.env.CI, | ||
retries: process.env.CI ? 2 : 0, | ||
webServer: { | ||
command: "yarn demo -c playwright", | ||
timeout: 300000, | ||
url: baseURL, | ||
reuseExistingServer: !process.env.CI, | ||
}, | ||
use: { | ||
baseURL, | ||
viewport: {width: 1280, height: 720}, | ||
} | ||
}; | ||
|
||
export default config; |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {Page, test as baseTest} from "@playwright/test"; | ||
import {randomBytes} from "crypto"; | ||
import {promises as fs} from "fs"; | ||
import {join} from "path"; | ||
|
||
let globalPage: Page | null = null; | ||
export const getPage = (): Page => { | ||
return globalPage; | ||
}; | ||
export const setPage = (page: Page | null) => { | ||
globalPage = page; | ||
}; | ||
let globalBrowserName: 'chromium' | 'firefox' | 'webkit' | null = null; | ||
export const getBrowserName = () => { | ||
return globalBrowserName; | ||
}; | ||
|
||
export interface Fixtures { | ||
testURL: string; | ||
testSelector: string; | ||
} | ||
|
||
export const test = baseTest.extend<Fixtures>({ | ||
testURL: "", | ||
testSelector: "", | ||
page: async({page, baseURL, testURL, testSelector, browserName}, use) => { | ||
globalBrowserName = browserName; | ||
if (!testURL || !testSelector) { | ||
throw new Error("testURL and testSelector must be defined!"); | ||
} | ||
|
||
// Listen for all console events and handle errors | ||
page.on('console', async msg => { | ||
const type = msg.type(); | ||
if (type === 'error' || type === 'warning') { | ||
test.fail(true, msg.text()); | ||
} | ||
}); | ||
|
||
// Log all uncaught errors to the terminal | ||
page.on('pageerror', exception => { console.log(`Uncaught exception:\n${exception}`); }); | ||
|
||
await page.goto(`${baseURL}/${testURL}`); | ||
await page.waitForSelector(testSelector); | ||
|
||
await use(page); | ||
|
||
const coverage: string = await page.evaluate('JSON.stringify(window.__coverage__);'); | ||
if (coverage) { | ||
const name = randomBytes(32).toString("hex"); | ||
await fs.writeFile(join(__dirname, '..', '.nyc_output', `${name}.json`), coverage); | ||
} | ||
globalPage = null; | ||
}, | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,45 @@ | ||
import {PlaywrightTestConfig, devices} from '@playwright/test'; | ||
|
||
export const baseURL = "http://localhost:4200/#"; | ||
|
||
const config: PlaywrightTestConfig = { | ||
reporter: process.env.CI ? 'github' : 'list', | ||
globalSetup: require.resolve('./setup.e2e-spec'), | ||
testDir: "src", | ||
testMatch: /\.e2e-spec\.ts$/, | ||
timeout: 60000, | ||
forbidOnly: !!process.env.CI, | ||
retries: process.env.CI ? 2 : 0, | ||
webServer: { | ||
command: "yarn e2e-app:serve -c playwright", | ||
timeout: 300000, | ||
url: baseURL, | ||
reuseExistingServer: !process.env.CI, | ||
}, | ||
use: { | ||
baseURL, | ||
viewport: {width: 1280, height: 720}, | ||
}, | ||
projects: [ | ||
{ | ||
name: 'chromium', | ||
use: { | ||
...devices['Desktop Chrome'], | ||
}, | ||
}, | ||
{ | ||
name: 'firefox', | ||
use: { | ||
...devices['Desktop Firefox'], | ||
}, | ||
}, | ||
{ | ||
name: 'webkit', | ||
use: { | ||
...devices['Desktop Safari'], | ||
}, | ||
}, | ||
] | ||
}; | ||
|
||
export default config; |
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,57 +1,16 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import {browserName, test, launchOptions} from './playwright.conf'; | ||
import {FullConfig} from "@playwright/test"; | ||
import {promises as fs} from "fs"; | ||
import {join} from "path"; | ||
import NYC from "nyc"; | ||
|
||
beforeAll(async() => { | ||
Error.stackTraceLimit = Infinity; | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000000; | ||
try { | ||
console.log(`Launch browser ${browserName} with`, launchOptions); | ||
await test.newPage(); | ||
} catch (e) { | ||
console.error('Unable to setup a new page with playwright', e); | ||
} | ||
async function globalSetup(config: FullConfig) { | ||
await fs.mkdir(join(__dirname, "..", ".nyc_output")); | ||
return async() => { | ||
const nycInstance = | ||
new NYC({cwd: join(__dirname, '..'), reportDir: `coverage-e2e`, reporter: ['lcov', 'json', 'text-summary']}); | ||
nycInstance.report(); | ||
nycInstance.cleanup(); | ||
}; | ||
} | ||
|
||
// Need to wait to avoid connection failures between Playwright and the browser. | ||
// To remove once Playwright test runner will be used. | ||
await test.page.waitForTimeout(500); | ||
|
||
// Listen for all console events and handle errors | ||
test.page.on('console', async msg => { | ||
const type = msg.type(); | ||
if (type === 'error' || type === 'warning') { | ||
fail(msg.text()); | ||
} | ||
}); | ||
|
||
// Log all uncaught errors to the terminal | ||
test.page.on('pageerror', exception => { console.log(`Uncaught exception:\n${exception}`); }); | ||
|
||
}); | ||
|
||
afterAll(async() => { | ||
if (browserName === 'chromium') { | ||
try { | ||
console.log('Retrieving coverage...'); | ||
const coverage: string = await test.page.evaluate('JSON.stringify(window.__coverage__);'); | ||
if (coverage) { | ||
console.log(`Coverage retrieved (${coverage.length} bytes)`); | ||
fs.mkdirSync(path.join(__dirname, '..', '.nyc_output')); | ||
fs.writeFileSync(path.join(__dirname, '..', '.nyc_output', 'out.json'), coverage); | ||
const NYC = require('nyc'); | ||
const nycInstance = new NYC( | ||
{cwd: path.join(__dirname, '..'), reportDir: `coverage-e2e`, reporter: ['lcov', 'json', 'text-summary']}); | ||
nycInstance.report(); | ||
nycInstance.cleanup(); | ||
console.log('Coverage saved successfully!'); | ||
} else { | ||
console.log('No coverage data!'); | ||
} | ||
} catch (error) { | ||
console.log('Error in onComplete:', error); | ||
process.exit(1); | ||
} | ||
} | ||
await test.destroy(); | ||
}, 120000); | ||
export default globalSetup; |
Oops, something went wrong.