Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: support esm imports (#3125)
Dmitry Gozmand234dac752f2
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.ymlindex 5ea2f4e013..d3fa7ed854 100644--- a/.github/workflows/tests.yml+++ b/.github/workflows/tests.yml@@ -46,7 +46,7 @@ jobs:if: ${{ always() }}with:name: ${{ matrix.browser }}-linux-jest-report- path: jest-report.json+ path: jest-report.json- uses: actions/upload-artifact@v1if: failure()with:@@ -82,7 +82,7 @@ jobs:if: ${{ always() }}with:name: ${{ matrix.browser }}-mac-jest-report- path: jest-report.json+ path: jest-report.json- uses: actions/upload-artifact@v1if: failure()with:@@ -134,6 +134,7 @@ jobs:test-package-installations:runs-on: ubuntu-lateststrategy:+ fail-fast: falsematrix:node_version:- "^10.17.0" # pre 10.17, --unhandled-rejections=strict was not an option (https://github.com/nodejs/node/pull/26599) which we need in our testsdiff --git a/.npmignore b/.npmignoreindex bdf2eb3503..39806bed37 100644--- a/.npmignore+++ b/.npmignore@@ -14,6 +14,7 @@ lib/injected/!index.d.ts!index.js+!index.mjs!install.js!README.md!LICENSEdiff --git a/packages/build_package.js b/packages/build_package.jsindex 0eef7cd1af..be20aa6d5b 100755--- a/packages/build_package.js+++ b/packages/build_package.js@@ -83,7 +83,7 @@ if (args.some(arg => arg === '--help')) {// 2. Setup cleanup if neededif (!args.some(arg => arg === '--no-cleanup')) {- process.on('exit', () => {+ process.on('exit', () => {cleanupPaths.forEach(cleanupPath => rmSync(cleanupPath, {}));});process.on('SIGINT', () => process.exit(2));@@ -123,6 +123,10 @@ if (!package) {engines: pwInternalJSON.engines,homepage: pwInternalJSON.homepage,main: 'index.js',+ exports: {+ import: './index.mjs',+ require: './index.js',+ },scripts: {install: 'node install.js',},diff --git a/packages/installation-tests/esm-playwright-chromium.mjs b/packages/installation-tests/esm-playwright-chromium.mjsnew file mode 100644index 0000000000..a14c843ba5--- /dev/null+++ b/packages/installation-tests/esm-playwright-chromium.mjs@@ -0,0 +1,35 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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 { chromium, selectors, devices, errors } from 'playwright-chromium';+import playwright from 'playwright-chromium';++if (playwright.chromium !== chromium)+ process.exit(1);++(async () => {+ for (const browserType of [chromium]) {+ const browser = await browserType.launch();+ const context = await browser.newContext();+ const page = await context.newPage();+ await page.evaluate(() => navigator.userAgent);+ await browser.close();+ }+ console.log(`esm SUCCESS`);+})().catch(err => {+ console.error(err);+ process.exit(1);+});diff --git a/packages/installation-tests/esm-playwright-firefox.mjs b/packages/installation-tests/esm-playwright-firefox.mjsnew file mode 100644index 0000000000..102c65b4ad--- /dev/null+++ b/packages/installation-tests/esm-playwright-firefox.mjs@@ -0,0 +1,35 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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 { firefox, selectors, devices, errors } from 'playwright-firefox';+import playwright from 'playwright-firefox';++if (playwright.firefox !== firefox)+ process.exit(1);++(async () => {+ for (const browserType of [firefox]) {+ const browser = await browserType.launch();+ const context = await browser.newContext();+ const page = await context.newPage();+ await page.evaluate(() => navigator.userAgent);+ await browser.close();+ }+ console.log(`esm SUCCESS`);+})().catch(err => {+ console.error(err);+ process.exit(1);+});diff --git a/packages/installation-tests/esm-playwright-webkit.mjs b/packages/installation-tests/esm-playwright-webkit.mjsnew file mode 100644index 0000000000..2706589070--- /dev/null+++ b/packages/installation-tests/esm-playwright-webkit.mjs@@ -0,0 +1,35 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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 { webkit, selectors, devices, errors } from 'playwright-webkit';+import playwright from 'playwright-webkit';++if (playwright.webkit !== webkit)+ process.exit(1);++(async () => {+ for (const browserType of [webkit]) {+ const browser = await browserType.launch();+ const context = await browser.newContext();+ const page = await context.newPage();+ await page.evaluate(() => navigator.userAgent);+ await browser.close();+ }+ console.log(`esm SUCCESS`);+})().catch(err => {+ console.error(err);+ process.exit(1);+});diff --git a/packages/installation-tests/esm-playwright.mjs b/packages/installation-tests/esm-playwright.mjsnew file mode 100644index 0000000000..526792b648--- /dev/null+++ b/packages/installation-tests/esm-playwright.mjs@@ -0,0 +1,39 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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 { chromium, firefox, webkit, selectors, devices, errors } from 'playwright';+import playwright from 'playwright';++if (playwright.chromium !== chromium)+ process.exit(1);+if (playwright.firefox !== firefox)+ process.exit(1);+if (playwright.webkit !== webkit)+ process.exit(1);++(async () => {+ for (const browserType of [chromium, firefox, webkit]) {+ const browser = await browserType.launch();+ const context = await browser.newContext();+ const page = await context.newPage();+ await page.evaluate(() => navigator.userAgent);+ await browser.close();+ }+ console.log(`esm SUCCESS`);+})().catch(err => {+ console.error(err);+ process.exit(1);+});diff --git a/packages/installation-tests/installation-tests.sh b/packages/installation-tests/installation-tests.shindex 4c4513d26e..fe13886bd6 100755--- a/packages/installation-tests/installation-tests.sh+++ b/packages/installation-tests/installation-tests.sh@@ -22,8 +22,17 @@ PLAYWRIGHT_CHROMIUM_TGZ="$(node ${PACKAGE_BUILDER} playwright-chromium ./playwriPLAYWRIGHT_WEBKIT_TGZ="$(node ${PACKAGE_BUILDER} playwright-webkit ./playwright-webkit.tgz)"PLAYWRIGHT_FIREFOX_TGZ="$(node ${PACKAGE_BUILDER} playwright-firefox ./playwright-firefox.tgz)"-SANITY_JS="$(pwd -P)/../sanity.js"+SCRIPTS_PATH="$(pwd -P)/.."TEST_ROOT="$(pwd -P)"+NODE_VERSION="$(node --version)"++function copy_test_scripts {+ cp "${SCRIPTS_PATH}/sanity.js" .+ cp "${SCRIPTS_PATH}/esm-playwright.mjs" .+ cp "${SCRIPTS_PATH}/esm-playwright-chromium.mjs" .+ cp "${SCRIPTS_PATH}/esm-playwright-firefox.mjs" .+ cp "${SCRIPTS_PATH}/esm-playwright-webkit.mjs" .+}function run_tests {test_typescript_types@@ -67,7 +76,7 @@ function test_playwright_global_installation {echo "Directory for shared browsers was not created!"exit 1fi- cp ${SANITY_JS} .+ copy_test_scriptsif node sanity.js playwright chromium 2>/dev/null; thenecho "Should not be able to launch chromium without PLAYWRIGHT_BROWSERS_PATH variable!"exit 1@@ -111,28 +120,44 @@ function test_playwright_should_work {initialize_test "${FUNCNAME[0]}"npm install ${PLAYWRIGHT_TGZ}- cp ${SANITY_JS} . && node sanity.js playwright chromium firefox webkit+ copy_test_scripts+ node sanity.js playwright chromium firefox webkit+ if [[ "${NODE_VERSION}" == *"v14."* ]]; then+ node esm-playwright.mjs+ fi}function test_playwright_chromium_should_work {initialize_test "${FUNCNAME[0]}"npm install ${PLAYWRIGHT_CHROMIUM_TGZ}- cp ${SANITY_JS} . && node sanity.js playwright-chromium chromium+ copy_test_scripts+ node sanity.js playwright-chromium chromium+ if [[ "${NODE_VERSION}" == *"v14."* ]]; then+ node esm-playwright-chromium.mjs+ fi}function test_playwright_webkit_should_work {initialize_test "${FUNCNAME[0]}"npm install ${PLAYWRIGHT_WEBKIT_TGZ}- cp ${SANITY_JS} . && node sanity.js playwright-webkit webkit+ copy_test_scripts+ node sanity.js playwright-webkit webkit+ if [[ "${NODE_VERSION}" == *"v14."* ]]; then+ node esm-playwright-webkit.mjs+ fi}function test_playwright_firefox_should_work {initialize_test "${FUNCNAME[0]}"npm install ${PLAYWRIGHT_FIREFOX_TGZ}- cp ${SANITY_JS} . && node sanity.js playwright-firefox firefox+ copy_test_scripts+ node sanity.js playwright-firefox firefox+ if [[ "${NODE_VERSION}" == *"v14."* ]]; then+ node esm-playwright-firefox.mjs+ fi}function initialize_test {diff --git a/packages/installation-tests/sanity.js b/packages/installation-tests/sanity.jsindex 300cd14d83..b37278f41f 100644--- a/packages/installation-tests/sanity.js+++ b/packages/installation-tests/sanity.js@@ -1,3 +1,19 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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.+ */+const requireName = process.argv[2];const browsers = process.argv.slice(3);@@ -11,6 +27,7 @@ const playwright = require(requireName);await page.evaluate(() => navigator.userAgent);await browser.close();}+ console.log(`require SUCCESS`);})().catch(err => {console.error(err);process.exit(1);diff --git a/packages/playwright-chromium/index.mjs b/packages/playwright-chromium/index.mjsnew file mode 100644index 0000000000..8365d8453b--- /dev/null+++ b/packages/playwright-chromium/index.mjs@@ -0,0 +1,23 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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 playwright from './index.js';++export const chromium = playwright.chromium;+export const selectors = playwright.selectors;+export const devices = playwright.devices;+export const errors = playwright.errors;+export default playwright;diff --git a/packages/playwright-core/index.mjs b/packages/playwright-core/index.mjsnew file mode 100644index 0000000000..337c589660--- /dev/null+++ b/packages/playwright-core/index.mjs@@ -0,0 +1,25 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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 playwright from './index.js';++export const chromium = playwright.chromium;+export const firefox = playwright.firefox;+export const webkit = playwright.webkit;+export const selectors = playwright.selectors;+export const devices = playwright.devices;+export const errors = playwright.errors;+export default playwright;diff --git a/packages/playwright-electron/index.mjs b/packages/playwright-electron/index.mjsnew file mode 100644index 0000000000..619e0026ec--- /dev/null+++ b/packages/playwright-electron/index.mjs@@ -0,0 +1,23 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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 playwright from './index.js';++export const electron = playwright.electron;+export const selectors = playwright.selectors;+export const devices = playwright.devices;+export const errors = playwright.errors;+export default playwright;diff --git a/packages/playwright-firefox/index.mjs b/packages/playwright-firefox/index.mjsnew file mode 100644index 0000000000..824f5c8af8--- /dev/null+++ b/packages/playwright-firefox/index.mjs@@ -0,0 +1,23 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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 playwright from './index.js';++export const firefox = playwright.firefox;+export const selectors = playwright.selectors;+export const devices = playwright.devices;+export const errors = playwright.errors;+export default playwright;diff --git a/packages/playwright-webkit/index.mjs b/packages/playwright-webkit/index.mjsnew file mode 100644index 0000000000..5f44947f37--- /dev/null+++ b/packages/playwright-webkit/index.mjs@@ -0,0 +1,23 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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 playwright from './index.js';++export const webkit = playwright.webkit;+export const selectors = playwright.selectors;+export const devices = playwright.devices;+export const errors = playwright.errors;+export default playwright;diff --git a/packages/playwright/index.mjs b/packages/playwright/index.mjsnew file mode 100644index 0000000000..337c589660--- /dev/null+++ b/packages/playwright/index.mjs@@ -0,0 +1,25 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * 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 playwright from './index.js';++export const chromium = playwright.chromium;+export const firefox = playwright.firefox;+export const webkit = playwright.webkit;+export const selectors = playwright.selectors;+export const devices = playwright.devices;+export const errors = playwright.errors;+export default playwright;