From 953f19538a6ea5a5f41b7d0faa63007b1f8a6f83 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Mon, 23 Aug 2021 13:57:38 +0200 Subject: [PATCH] docs(errors): add example for catching Timeout errors (#8362) --- docs/src/api/class-timeouterror.md | 97 ++++++++++++++++++++++++++++++ types/types.d.ts | 20 ++++++ 2 files changed, 117 insertions(+) diff --git a/docs/src/api/class-timeouterror.md b/docs/src/api/class-timeouterror.md index e10529a12f..8d6ccaba45 100644 --- a/docs/src/api/class-timeouterror.md +++ b/docs/src/api/class-timeouterror.md @@ -3,3 +3,100 @@ TimeoutError is emitted whenever certain operations are terminated due to timeout, e.g. [`method: Page.waitForSelector`] or [`method: BrowserType.launch`]. + +```js +const playwright = require('playwright'); + +(async () => { + const browser = await playwright.chromium.launch(); + const context = await browser.newContext(); + const page = await context.newPage(); + try { + await page.click("text=Foo", { + timeout: 100, + }) + } catch (error) { + if (error instanceof playwright.errors.TimeoutError) + console.log("Timeout!") + } + await browser.close(); +})(); +``` + +```python sync +from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeoutError + +with sync_playwright() as p: + browser = p.chromium.launch() + page = browser.new_page() + try: + page.click("text=Example", timeout=100) + except PlaywrightTimeoutError: + print("Timeout!") + browser.close() +``` + +```python async +import asyncio +from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError + +async def run(playwright): + browser = await playwright.chromium.launch() + page = await browser.new_page() + try: + await page.click("text=Example", timeout=100) + except PlaywrightTimeoutError: + print("Timeout!") + await browser.close() + +async def main(): + async with async_playwright() as playwright: + await run(playwright) + +asyncio.run(main()) +``` + +```java +package org.example; + +import com.microsoft.playwright.*; + +public class TimeoutErrorExample { + public static void main(String[] args) { + try (Playwright playwright = Playwright.create()) { + Browser browser = playwright.firefox().launch(); + BrowserContext context = browser.newContext(); + Page page = context.newPage(); + try { + page.click("text=Example", new Page.ClickOptions().setTimeout(100)); + } catch (TimeoutError e) { + System.out.println("Timeout!"); + } + } + } +} +``` + +```csharp +using System.Threading.Tasks; +using Microsoft.Playwright; +using System; + +class Program +{ + public static async Task Main() + { + using var playwright = await Playwright.CreateAsync(); + await using var browser = await playwright.Chromium.LaunchAsync(); + var page = await browser.NewPageAsync(); + try + { + await page.ClickAsync("text=Example", new() { Timeout = 100 }); + } + catch (TimeoutException) + { + Console.WriteLine("Timeout!"); + } + } +} +``` diff --git a/types/types.d.ts b/types/types.d.ts index 33c6ef99bc..17f1329366 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -8802,6 +8802,26 @@ export namespace errors { * TimeoutError is emitted whenever certain operations are terminated due to timeout, e.g. * [page.waitForSelector(selector[, options])](https://playwright.dev/docs/api/class-page#page-wait-for-selector) or * [browserType.launch([options])](https://playwright.dev/docs/api/class-browsertype#browser-type-launch). + * + * ```js + * const playwright = require('playwright'); + * + * (async () => { + * const browser = await playwright.chromium.launch(); + * const context = await browser.newContext(); + * const page = await context.newPage(); + * try { + * await page.click("text=Foo", { + * timeout: 100, + * }) + * } catch (error) { + * if (error instanceof playwright.errors.TimeoutError) + * console.log("Timeout!") + * } + * await browser.close(); + * })(); + * ``` + * */ class TimeoutError extends Error {}