e2e-test-utils: add playwright utilities

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-09-25 10:55:31 +02:00
parent f5b41b27a9
commit 8d11fed246
5 changed files with 163 additions and 7 deletions
+23 -4
View File
@@ -6,9 +6,21 @@
"types": "src/index.ts",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
"main": "dist/index.cjs.js",
"types": "dist/index.d.ts"
"access": "public"
},
"exports": {
"./playwright": "./src/playwright/index.ts",
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"playwright": [
"src/playwright/index.ts"
],
"package.json": [
"package.json"
]
}
},
"backstage": {
"role": "node-library"
@@ -22,8 +34,15 @@
"prepack": "backstage-cli package prepack",
"postpack": "backstage-cli package postpack"
},
"dependencies": {
"@manypkg/get-packages": "^1.1.3",
"@playwright/test": "^1.32.3",
"fs-extra": "^10.1.0"
},
"devDependencies": {
"@backstage/cli": "workspace:^"
"@backstage/cli": "workspace:^",
"@backstage/cli-node": "workspace:^",
"@types/fs-extra": "^9.0.1"
},
"files": [
"dist"
@@ -0,0 +1,37 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test } from '@playwright/test';
/**
* Calling this at the top of your test file will ensure that tests fail if the browser
* logs any errors or if there are any uncaught exceptions.
*
* @public
*/
export function failOnBrowserErrors(): void {
test.beforeEach(async ({ page }) => {
page.on('pageerror', error => {
throw new Error(`Uncaught exception on page, ${error}`);
});
page.on('console', msg => {
if (msg.type() === 'error') {
throw new Error(`Unexpected console error message "${msg.text()}"`);
}
});
});
}
@@ -0,0 +1,42 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fs from 'fs-extra';
import { resolve as resolvePath } from 'path';
import { PlaywrightTestConfig } from '@playwright/test';
import { getPackagesSync } from '@manypkg/get-packages';
import type { BackstagePackage } from '@backstage/cli-node';
/**
* Generates a list of playwright projects by scanning the monorepo for packages with an `e2e-tests/` folder.
*
* @public
*/
export function generateProjects(): PlaywrightTestConfig['projects'] {
// TODO(Rugvip): Switch this over to use @backstage/cli-node once released, and support SINCE=origin/main
const { root, packages } = getPackagesSync(process.cwd());
const e2eTestPackages = [...(root ? [root] : []), ...packages].filter(pkg => {
return fs.pathExistsSync(resolvePath(pkg.dir, 'e2e-tests'));
}) as BackstagePackage[];
return e2eTestPackages.map(pkg => ({
name: pkg.packageJson.name,
testDir: resolvePath(pkg.dir, 'e2e-tests'),
use: {
channel: 'chrome',
},
}));
}
@@ -0,0 +1,18 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { generateProjects } from './generateProjects';
export { failOnBrowserErrors } from './failOnBrowserErrors';