docs(readme): add network interception example (#1216)

This commit is contained in:
Andrey Lushnikov 2020-03-04 11:03:49 -08:00 committed by GitHub
parent 14a7d1c656
commit d6e265f443
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -74,30 +74,7 @@ const iPhone11 = devices['iPhone 11 Pro'];
})();
```
And here is the same script for Chrome on Android.
```js
const { chromium, devices } = require('playwright');
const pixel2 = devices['Pixel 2'];
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext({
viewport: pixel2.viewport,
userAgent: pixel2.userAgent,
geolocation: { longitude: 12.492507, latitude: 41.889938 },
permissions: { 'https://www.google.com': ['geolocation'] }
});
const page = await context.newPage();
await page.goto('https://maps.google.com');
await page.click('text="Your location"');
await page.waitForRequest(/.*pwa\/net.js.*/);
await page.screenshot({ path: 'colosseum-android.png' });
await browser.close();
})();
```
#### Evaluate script
#### Evaluate in browser context
This code snippet navigates to example.com in Firefox, and executes a script in the page context.
@ -122,6 +99,29 @@ const { firefox } = require('playwright');
})();
```
#### Intercept network requests
This code snippet sets up network interception for a WebKit page to log all network requests.
```js
const { webkit } = require('playwright');
(async () => {
const browser = await webkit.launch();
const context = await browser.newContext();
const page = await context.newPage();
// Log and continue all network requests
page.route('**', request => {
console.log(request.url());
request.continue();
});
await page.goto('http://todomvc.com');
await browser.close();
})();
```
## Contributing
Check out our [contributing guide](https://github.com/microsoft/playwright/blob/master/CONTRIBUTING.md).