From 1479610abdecc4be3efc095034eaf7853f739b48 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 10 Jun 2021 13:43:42 -0700 Subject: [PATCH] docs: start adding an API cheat sheet (#7045) --- docs/src/cheat-sheet.md | 302 ++++++++++++++++++ .../{extensions.md => chrome-extensions.md} | 4 +- docs/src/downloads.md | 2 +- 3 files changed, 305 insertions(+), 3 deletions(-) create mode 100644 docs/src/cheat-sheet.md rename docs/src/{extensions.md => chrome-extensions.md} (94%) diff --git a/docs/src/cheat-sheet.md b/docs/src/cheat-sheet.md new file mode 100644 index 0000000000..1d2705d626 --- /dev/null +++ b/docs/src/cheat-sheet.md @@ -0,0 +1,302 @@ +--- +id: cheat-sheet +title: "Cheat Sheet" +--- + + + +## Download & Upload + +### Download file + +```js +const [ download ] = await Promise.all([ + page.waitForEvent('download'), + page.click('button') +]); +const path = await download.path(); +``` + +```python async +async with page.expect_download() as download_info: + await page.click("button") +download = await download_info.value +path = await download.path() +``` + +```python sync +with page.expect_download() as download_info: + page.click("button") +download = download_info.value +path = download.path() +``` + +```csharp +var waitForDownloadTask = page.WaitForDownloadAsync(); +await page.ClickAsync("#downloadButton"); +var download = await waitForDownloadTask; +var path = await download.PathAsync(); +``` + +[Learn more](./downloads.md) + +### Upload file + +```js +await page.setInputFiles('input#upload', 'myfile.pdf'); +``` + +```java +page.setInputFiles("input#upload", Paths.get("myfile.pdf")); +``` + +```python async +await page.set_input_files('input#upload', 'myfile.pdf') +``` + +```python sync +page.set_input_files('input#upload', 'myfile.pdf') +``` + +```csharp +await page.SetInputFilesAsync("input#upload", "myfile.pdf"); +``` + +[Learn more](./input#upload-files) + +### Upload multiple files + +```js +await page.setInputFiles('input#upload', ['file1.txt', 'file2.txt']); +``` + +```java +page.setInputFiles("input#upload", new Path[] {Paths.get("file1.txt"), Paths.get("file2.txt")}); +``` + +```python async +await page.set_input_files('input#upload', ['file1.txt', 'file2.txt']) +``` + +```python sync +page.set_input_files('input#upload', ['file1.txt', 'file2.txt']) +``` + +```csharp +await page.SetInputFilesAsync("input#upload", new[] { "file1.txt", "file12.txt" }); +``` + +[Learn more](./input#upload-files) + +### Upload from memory + +```js +await page.setInputFiles('input#upload', { + name: 'file.txt', + mimeType: 'text/plain', + buffer: Buffer.from('this is test') +}); +``` + +```java +page.setInputFiles("input#upload", new FilePayload( + "file.txt", "text/plain", "this is test".getBytes(StandardCharsets.UTF_8))); +``` + +```python async +await page.set_input_files( + "input#upload", + files=[ + {"name": "test.txt", "mimeType": "text/plain", "buffer": b"this is a test"} + ], +) +``` + +```python sync +page.set_input_files( + "input#upload", + files=[ + {"name": "test.txt", "mimeType": "text/plain", "buffer": b"this is a test"} + ], +) +``` + +```csharp +await page.SetInputFilesAsync("input#upload", new FilePayload +{ + Name = "file.txt", + MimeType = "text/plain", + Buffer = "this is a test".getBytes(StandardCharsets.UTF_8), +}); +``` + +[Learn more](./input#upload-files) + +### Remove selected files + +```js +await page.setInputFiles('input#upload', []); +``` + +```java +page.setInputFiles("input#upload", new Path[0]); +``` + +```python async +await page.set_input_files('input#upload', []) +``` + +```python sync +page.set_input_files('input#upload', []) +``` + +```csharp +await page.SetInputFilesAsync("input#upload", new[] {}); +``` + +[Learn more](./input#upload-files) + +### Handle file picker + +```js +const [fileChooser] = await Promise.all([ + page.waitForEvent('filechooser'), + page.click('upload') +]); +await fileChooser.setFiles('myfile.pdf'); +``` + +```java +FileChooser fileChooser = page.waitForFileChooser(() -> { + page.click("upload"); +}); +fileChooser.setFiles(Paths.get("myfile.pdf")); +``` + +```python async +async with page.expect_file_chooser() as fc_info: + await page.click("upload") +file_chooser = await fc_info.value +await file_chooser.set_files("myfile.pdf") +``` + +```python sync +with page.expect_file_chooser() as fc_info: + page.click("upload") +file_chooser = fc_info.value +file_chooser.set_files("myfile.pdf") +``` + +```csharp +var fileChooser = page.RunAndWaitForFileChooserAsync(async () => +{ + await page.ClickAsync("upload"); +}); +await fileChooser.SetFilesAsync("myfile.pdf"); +``` + +[Learn more](./input#upload-files) + + +## Manage <iframe>s + +### List frames + +```js +const frames = page.frames(); +``` + +```java +List frames = page.frames(); +``` + +```python async +frames = page.frames +``` + +```python sync +frames = page.frames +``` + +```csharp +var frame = page.Frames; +``` + +[Learn more](./core-concepts#pages-and-frames) + +### Frame by `name` attribute + +```js +const frame = page.frame('frame-login'); +``` + +```java +Frame frame = page.frame("frame-login"); +``` + +```python async +frame = page.frame('frame-login') +``` + +```python sync +frame = page.frame('frame-login') +``` + +```csharp +var frame = page.Frame("frame-login"); +``` + +[Learn more](./core-concepts#pages-and-frames) + +### Frame by URL + +```js +const frame = page.frame({ url: /.*domain.*/ }); +``` + +```java +Frame frame = page.frameByUrl(Pattern.compile(".*domain.*")); +``` + +```python async +frame = page.frame(url=r'.*domain.*') +``` + +```python sync +frame = page.frame(url=r'.*domain.*') +``` + +```csharp +var frame = page.FrameByUrl("*domain."); +``` + +[Learn more](./core-concepts#pages-and-frames) + +### Frame by selector + +```js +const frameElementHandle = await page.$('.frame-class'); +const frame = await frameElementHandle.contentFrame(); +``` + +```java +ElementHandle frameElementHandle = page.querySelector(".frame-class"); +Frame frame = frameElementHandle.contentFrame(); +``` + +```python async +frame_element_handle = await page.query_selector('.frame-class') +frame = await frame_element_handle.content_frame() +``` + +```python sync +frame_element_handle = page.query_selector('.frame-class') +frame = frame_element_handle.content_frame() +``` + +```csharp +var frameElementHandle = await page.QuerySelectorAsync(".frame-class"); +var frame = await frameElementHandle.ContentFrameAsync(); +``` + +[Learn more](./core-concepts#pages-and-frames) diff --git a/docs/src/extensions.md b/docs/src/chrome-extensions.md similarity index 94% rename from docs/src/extensions.md rename to docs/src/chrome-extensions.md index 406a8343bd..feec932065 100644 --- a/docs/src/extensions.md +++ b/docs/src/chrome-extensions.md @@ -1,6 +1,6 @@ --- -id: extensions -title: "Testing extensions" +id: chrome-extensions +title: "Chrome Extensions" --- :::note diff --git a/docs/src/downloads.md b/docs/src/downloads.md index f1b4ee0c3b..065d2c3a10 100644 --- a/docs/src/downloads.md +++ b/docs/src/downloads.md @@ -57,7 +57,7 @@ var waitForDownloadTask = page.WaitForDownloadAsync(); await page.ClickAsync("#downloadButton"); // Wait for the download process to complete var download = await waitForDownloadTask; -Console.WriteLine(await download.PathAsync()); +var path = await download.PathAsync(); ``` #### Variations