From 19bd32f6c74a363ad162ca83128e3500c5567319 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Mon, 1 Mar 2021 20:14:19 -0800 Subject: [PATCH] docs: add video and proxy docs (#5668) --- docs/src/network.md | 73 ++++++++++++++++++++++++++++++++++++++ docs/src/videos.md | 85 ++++++++++++++++++++++++--------------------- 2 files changed, 119 insertions(+), 39 deletions(-) diff --git a/docs/src/network.md b/docs/src/network.md index 318f33d7eb..29698d9a3b 100644 --- a/docs/src/network.md +++ b/docs/src/network.md @@ -50,6 +50,79 @@ page.goto("https://example.com") ### API reference - [`method: Browser.newContext`] +## HTTP Proxy + +You can configure pages to load over the HTTP(S) proxy or SOCKSv5. Proxy can be either set globally +for the entire browser, or for each browser context individually. + +You can optionally specify username and password for HTTP(S) proxy, you can also specify hosts to +bypass proxy for. + +Here is an example of a global proxy: + +```js +const browser = await chromium.launch({ + proxy: { + server: 'http://myproxy.com:3128', + user: 'usr', + password: 'pwd' + } +}); +``` + +```java +Browser browser = chromium.launch(new BrowserType.LaunchOptions() + .withProxy(new Proxy("http://myproxy.com:3128") + .withUsername('usr') + .withPassword('pwd')); +``` + +```python async +browser = await chromium.launch(proxy={ + "server": "http://myproxy.com:3128", + "user": "usr", + "password": "pwd" +}) +``` + +```python sync +browser = chromium.launch(proxy={ + "server": "http://myproxy.com:3128", + "user": "usr", + "password": "pwd" +}) +``` + +When specifying proxy for each context individually, you need to give Playwright +a hint that proxy will be set. This is done via passing a non-empty proxy server +to the browser itself. Here is an example of a context-specific proxy: + +```js +const browser = await chromium.launch({ + proxy: { server: 'per-context' } +}); +const context = await browser.newContext({ + proxy: { server: 'http://myproxy.com:3128' } +}) +``` + +```java +Browser browser = chromium.launch(new BrowserType.LaunchOptions() + .withProxy(new Proxy("per-context")); +BrowserContext context = chromium.launch(new Browser.NewContextOptions() + .withProxy(new Proxy("http://myproxy.com:3128")); +``` + +```python async +browser = await chromium.launch(proxy={"server": "per-context"}) +context = await browser.new_context(proxy={"server": "http://myproxy.com:3128"}) +``` + +```python sync +browser = chromium.launch(proxy={"server": "per-context"}) +context = browser.new_context(proxy={"server": "http://myproxy.com:3128"}) +``` + ## Network events You can monitor all the requests and responses: diff --git a/docs/src/videos.md b/docs/src/videos.md index c3b81aa304..75298b7097 100644 --- a/docs/src/videos.md +++ b/docs/src/videos.md @@ -9,17 +9,32 @@ Playwright can record videos for all pages in a [browser context](./core-concept upon context closure, so make sure to await [`method: BrowserContext.close`]. ```js -// With browser.newContext() const context = await browser.newContext({ recordVideo: { dir: 'videos/' } }); // Make sure to await close, so that videos are saved. await context.close(); +``` -// With browser.newPage() -const page = await browser.newPage({ recordVideo: { dir: 'videos/' } }); -// Make sure to await close, so that videos are saved. -await page.close(); +```java +context = browser.newContext(new Browser.NewContextOptions().withRecordVideoDir(Paths.get("videos/"))); +// Make sure to close, so that videos are saved. +context.close(); +``` -// [Optional] Specify video size; defaults to viewport size scaled down to fit 800x800 +```python async +context = await browser.new_context(record_video_dir="videos/") +# Make sure to await close, so that videos are saved. +await context.close() +``` + +```python sync +context = browser.new_context(record_video_dir="videos/") +# Make sure to close, so that videos are saved. +context.close() +``` + +You can also specify video size, it defaults to viewport size scaled down to fit 800x800. + +```js const context = await browser.newContext({ recordVideo: { dir: 'videos/', @@ -29,34 +44,12 @@ const context = await browser.newContext({ ``` ```java -// With browser.newContext() -context = browser.newContext(new Browser.NewContextOptions().withRecordVideoDir(Paths.get("videos/"))); -// Make sure to close, so that videos are saved. -context.close(); - -// With browser.newPage() -Page page = browser.newPage(new Browser.NewPageOptions().withRecordVideoDir(Paths.get("videos/"))); -// Make sure to close, so that videos are saved. -page.close(); - -// [Optional] Specify video size; defaults to viewport size scaled down to fit 800x800 BrowserContext context = browser.newContext(new Browser.NewContextOptions() .withRecordVideoDir(Paths.get("videos/")) .withRecordVideoSize(1024, 768)); ``` ```python async -# With browser.new_context() -context = await browser.new_context(record_video_dir="videos/") -# Make sure to await close, so that videos are saved. -await context.close() - -# With browser.new_page() -page = await browser.new_page(record_video_dir="videos/") -# Make sure to await close, so that videos are saved. -await page.close() - -# [Optional] specify video size; defaults to viewport size scaled down to fit 800x800 context = await browser.new_context( record_video_dir="videos/", record_video_size={"width": 1024, "height": 768} @@ -64,23 +57,37 @@ context = await browser.new_context( ``` ```python sync -# With browser.new_context() -context = browser.new_context(record_video_dir="videos/") -# Make sure to close, so that videos are saved. -context.close() - -# With browser.new_page() -page = browser.new_page(record_video_dir="videos/") -# Make sure to close, so that videos are saved. -page.close() - -# [Optional] specify video size; defaults to viewport size scaled down to fit 800x800 context = browser.new_context( record_video_dir="videos/", record_video_size={"width": 1024, "height": 768} ) ``` +Saved video files will appear in the specified folder. They all have generated unique names. +For the multi-page scenarios, you can access the video file associated with the page via the +[`method: Page.video`]. + + +```js +const path = await page.video().path(); +``` + +```java +path = page.video().path(); +``` + +```python async +path = await page.video.path() +``` + +```python sync +path = page.video.path() +``` + +:::note +Note that the video is only available after the page or browser context is closed. +::: + ### API reference - [BrowserContext] - [`method: Browser.newContext`]