docs: fix leftover broken Node.js code snippets (#23919)

This commit is contained in:
Max Schmitt 2023-06-27 14:39:44 +02:00 committed by GitHub
parent 9980f054bc
commit 138e143030
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 50 deletions

View file

@ -98,23 +98,23 @@ Here's how to polyfill `waitForAngular` function in Playwright Test:
```js ```js
async function waitForAngular(page) { async function waitForAngular(page) {
const clientSideScripts = require('protractor/built/clientsidescripts.js'); const clientSideScripts = require('protractor/built/clientsidescripts.js');
async function executeScriptAsync(page, script, ...scriptArgs) { async function executeScriptAsync(page, script, ...scriptArgs) {
await page.evaluate(` await page.evaluate(`
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
const callback = (errMessage) => { const callback = (errMessage) => {
if (errMessage) if (errMessage)
reject(new Error(errMessage)); reject(new Error(errMessage));
else else
resolve(); resolve();
}; };
(function() {${script}}).apply(null, [...${JSON.stringify(scriptArgs)}, callback]); (function() {${script}}).apply(null, [...${JSON.stringify(scriptArgs)}, callback]);
}) })
`); `);
} }
await executeScriptAsync(page, clientSideScripts.waitForAngular, ''); await executeScriptAsync(page, clientSideScripts.waitForAngular, '');
} }
``` ```
If you don't want to keep a version protractor around, you can also use this simpler approach using this function (only works for Angular 2+): If you don't want to keep a version protractor around, you can also use this simpler approach using this function (only works for Angular 2+):
@ -130,9 +130,9 @@ Here's how to polyfill `waitForAngular` function in Playwright Test:
return new Promise(res => testability.whenStable(res)); return new Promise(res => testability.whenStable(res));
} }
} }
}); });
} }
``` ```
1. Polyfill usage 1. Polyfill usage
```js ```js

View file

@ -135,7 +135,7 @@ self.addEventListener('fetch', event => {
event.respondWith(responsePromise); event.respondWith(responsePromise);
}); });
self.addEventListener('activate', (event) => { self.addEventListener('activate', event => {
event.waitUntil(clients.claim()); event.waitUntil(clients.claim());
}); });
``` ```
@ -180,40 +180,41 @@ Consider the code snippets below to understand Playwright's view into the Reques
```js title="complex-service-worker.js" ```js title="complex-service-worker.js"
self.addEventListener('install', function (event) { self.addEventListener('install', function(event) {
event.waitUntil( event.waitUntil(
caches.open('v1').then(function (cache) { caches.open('v1').then(function(cache) {
// 1. Pre-fetches and caches /addressbook.json // 1. Pre-fetches and caches /addressbook.json
return cache.add('/addressbook.json'); return cache.add('/addressbook.json');
}) })
); );
}); });
// Opt to handle FetchEvent's from the page // Opt to handle FetchEvent's from the page
self.addEventListener('fetch', (event) => { self.addEventListener('fetch', event => {
event.respondWith( event.respondWith(
(async () => { (async () => {
// 1. Try to first serve directly from caches // 1. Try to first serve directly from caches
let response = await caches.match(event.request); const response = await caches.match(event.request);
if (response) return response; if (response) return response;
// 2. Re-write request for /foo to /bar // 2. Re-write request for /foo to /bar
if (event.request.url.endsWith('foo')) return fetch('./bar'); if (event.request.url.endsWith('foo')) return fetch('./bar');
// 3. Prevent tracker.js from being retrieved, and returns a placeholder response // 3. Prevent tracker.js from being retrieved, and returns a placeholder response
if (event.request.url.endsWith('tracker.js')) if (event.request.url.endsWith('tracker.js')) {
return new Response('console.log('no trackers!')', { return new Response('console.log("no trackers!")', {
status: 200, status: 200,
headers: { 'Content-Type': 'text/javascript' }, headers: { 'Content-Type': 'text/javascript' },
}); });
}
// 4. Otherwise, fallthrough, perform the fetch and respond // 4. Otherwise, fallthrough, perform the fetch and respond
return fetch(event.request); return fetch(event.request);
})() })()
); );
}); });
self.addEventListener('activate', (event) => { self.addEventListener('activate', event => {
event.waitUntil(clients.claim()); event.waitUntil(clients.claim());
}); });
``` ```

View file

@ -130,9 +130,9 @@ export default defineConfig({
projects: [ projects: [
{ {
name: 'Microsoft Edge', name: 'Microsoft Edge',
use: { use: {
...devices['Desktop Edge'], ...devices['Desktop Edge'],
channel: 'msedge' channel: 'msedge'
}, },
}, },
] ]

View file

@ -389,7 +389,7 @@ export default defineConfig({
```js ```js
test('…', async ({ mount, page, context }) => { test('…', async ({ mount, page, context }) => {
// … // …
}); });
``` ```

View file

@ -137,7 +137,7 @@ export default defineConfig({
maxDiffPixelRatio: 0.1, maxDiffPixelRatio: 0.1,
}, },
}, },
}); });
``` ```

View file

@ -327,7 +327,7 @@ async function globalSetup(config: FullConfig) {
await context.storageState({ path: storageState as string }); await context.storageState({ path: storageState as string });
await context.tracing.stop({ await context.tracing.stop({
path: './test-results/setup-trace.zip', path: './test-results/setup-trace.zip',
}) });
await browser.close(); await browser.close();
} catch (error) { } catch (error) {
await context.tracing.stop({ await context.tracing.stop({

View file

@ -51,7 +51,7 @@ export default defineConfig({
}, },
{ {
name: 'Google Chrome', name: 'Google Chrome',
use: { use: {
...devices['Desktop Chrome'], ...devices['Desktop Chrome'],
channel: 'chrome' channel: 'chrome'
}, },
@ -189,7 +189,7 @@ When working with tests that have a dependency, the dependency will always run f
Running order: Running order:
1. Tests in 'setup' project run 1. Tests in 'setup' project run
2. Tests in 'chromium', 'webkit' and 'firefox' projects run in parallel 2. Tests in 'chromium', 'webkit' and 'firefox' projects run in parallel
<img width="70%" style={{display: 'flex', margin: 'auto'}} alt="chromium, webkit and firefox projects depend on setup project" loading="lazy" src="https://user-images.githubusercontent.com/13063165/225937080-327b1e63-431f-40e0-90d7-35f21d7a92cb.jpg" /> <img width="70%" style={{display: 'flex', margin: 'auto'}} alt="chromium, webkit and firefox projects depend on setup project" loading="lazy" src="https://user-images.githubusercontent.com/13063165/225937080-327b1e63-431f-40e0-90d7-35f21d7a92cb.jpg" />
@ -199,8 +199,8 @@ If there are more than one dependency then these project dependencies will be ru
Running order: Running order:
1. Tests in 'Browser Login' and 'DataBase' projects run in parallel 1. Tests in 'Browser Login' and 'DataBase' projects run in parallel
- 'Browser Login' passes - 'Browser Login' passes
- ❌ 'DataBase' fails! - ❌ 'DataBase' fails!
1. “e2e tests” is not run! 1. “e2e tests” is not run!
<img width="70%" style={{display: 'flex', margin: 'auto'}} alt="Browser login project is blue, database is red and e2e tests relies on both" loading="lazy" src="https://user-images.githubusercontent.com/13063165/225938262-33c1b78f-f092-4762-a478-7f8cbc1e3b21.jpg" /> <img width="70%" style={{display: 'flex', margin: 'auto'}} alt="Browser login project is blue, database is red and e2e tests relies on both" loading="lazy" src="https://user-images.githubusercontent.com/13063165/225938262-33c1b78f-f092-4762-a478-7f8cbc1e3b21.jpg" />

View file

@ -209,7 +209,7 @@ test('runs first', async () => {
await page.goto('https://playwright.dev/'); await page.goto('https://playwright.dev/');
}); });
test('runs second', async () => { test('runs second', async () => {
await page.getByText('Get Started').click(); await page.getByText('Get Started').click();
}); });
``` ```