mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
docs: delete verification guide (#12698)
This commit is contained in:
parent
adcd32fc6e
commit
19de9cacc3
@ -1,6 +1,109 @@
|
||||
# class: ConsoleMessage
|
||||
|
||||
[ConsoleMessage] objects are dispatched by page via the [`event: Page.console`] event.
|
||||
For each console messages logged in the page there will be corresponding event in the Playwright
|
||||
context.
|
||||
|
||||
```js
|
||||
// Listen for all console logs
|
||||
page.on('console', msg => console.log(msg.text()))
|
||||
|
||||
// Listen for all console events and handle errors
|
||||
page.on('console', msg => {
|
||||
if (msg.type() === 'error')
|
||||
console.log(`Error text: "${msg.text()}"`);
|
||||
});
|
||||
|
||||
// Get the next console log
|
||||
const [msg] = await Promise.all([
|
||||
page.waitForEvent('console'),
|
||||
// Issue console.log inside the page
|
||||
page.evaluate(() => {
|
||||
console.log('hello', 42, { foo: 'bar' });
|
||||
}),
|
||||
]);
|
||||
|
||||
// Deconstruct console log arguments
|
||||
await msg.args[0].jsonValue() // hello
|
||||
await msg.args[1].jsonValue() // 42
|
||||
```
|
||||
|
||||
```java
|
||||
// Listen for all System.out.printlns
|
||||
page.onConsoleMessage(msg -> System.out.println(msg.text()));
|
||||
|
||||
// Listen for all console events and handle errors
|
||||
page.onConsoleMessage(msg -> {
|
||||
if ("error".equals(msg.type()))
|
||||
System.out.println("Error text: " + msg.text());
|
||||
});
|
||||
|
||||
// Get the next System.out.println
|
||||
ConsoleMessage msg = page.waitForConsoleMessage(() -> {
|
||||
// Issue console.log inside the page
|
||||
page.evaluate("console.log('hello', 42, { foo: 'bar' });");
|
||||
});
|
||||
|
||||
// Deconstruct console.log arguments
|
||||
msg.args().get(0).jsonValue() // hello
|
||||
msg.args().get(1).jsonValue() // 42
|
||||
```
|
||||
|
||||
```python async
|
||||
# Listen for all console logs
|
||||
page.on("console", lambda msg: print(msg.text))
|
||||
|
||||
# Listen for all console events and handle errors
|
||||
page.on("console", lambda msg: print(f"error: {msg.text}") if msg.type == "error" else None)
|
||||
|
||||
# Get the next console log
|
||||
async with page.expect_console_message() as msg_info:
|
||||
# Issue console.log inside the page
|
||||
await page.evaluate("console.log('hello', 42, { foo: 'bar' })")
|
||||
msg = await msg_info.value
|
||||
|
||||
# Deconstruct print arguments
|
||||
await msg.args[0].json_value() # hello
|
||||
await msg.args[1].json_value() # 42
|
||||
```
|
||||
|
||||
```python sync
|
||||
# Listen for all console logs
|
||||
page.on("console", lambda msg: print(msg.text))
|
||||
|
||||
# Listen for all console events and handle errors
|
||||
page.on("console", lambda msg: print(f"error: {msg.text}") if msg.type == "error" else None)
|
||||
|
||||
# Get the next console log
|
||||
with page.expect_console_message() as msg_info:
|
||||
# Issue console.log inside the page
|
||||
page.evaluate("console.log('hello', 42, { foo: 'bar' })")
|
||||
msg = msg_info.value
|
||||
|
||||
# Deconstruct print arguments
|
||||
msg.args[0].json_value() # hello
|
||||
msg.args[1].json_value() # 42
|
||||
```
|
||||
|
||||
```csharp
|
||||
// Listen for all System.out.printlns
|
||||
page.Console += (_, msg) => Console.WriteLine(msg.Text);
|
||||
|
||||
// Listen for all console events and handle errors
|
||||
page.Console += (_, msg) =>
|
||||
{
|
||||
if ("error".Equals(msg.Type))
|
||||
Console.WriteLine("Error text: " + msg.Text);
|
||||
};
|
||||
|
||||
// Get the next System.out.println
|
||||
var waitForMessageTask = page.WaitForConsoleMessageAsync();
|
||||
await page.EvaluateAsync("console.log('hello', 42, { foo: 'bar' });");
|
||||
var message = await waitForMessageTask;
|
||||
// Deconstruct console.log arguments
|
||||
await message.Args.ElementAt(0).JsonValueAsync<string>(); // hello
|
||||
await message.Args.ElementAt(1).JsonValueAsync<int>(); // 42
|
||||
```
|
||||
|
||||
## method: ConsoleMessage.args
|
||||
- returns: <[Array]<[JSHandle]>>
|
||||
|
@ -281,6 +281,29 @@ try {
|
||||
|
||||
Emitted when a JavaScript dialog appears, such as `alert`, `prompt`, `confirm` or `beforeunload`. Listener **must** either [`method: Dialog.accept`] or [`method: Dialog.dismiss`] the dialog - otherwise the page will [freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#never_blocking) waiting for the dialog, and actions like click will never finish.
|
||||
|
||||
```js
|
||||
page.on('dialog', dialog => {
|
||||
dialog.accept();
|
||||
});
|
||||
```
|
||||
|
||||
```java
|
||||
page.onDialog(dialog -> {
|
||||
dialog.accept();
|
||||
});
|
||||
```
|
||||
|
||||
```python
|
||||
page.on("dialog", lambda dialog: dialog.accept())
|
||||
```
|
||||
|
||||
```csharp
|
||||
page.RequestFailed += (_, request) =>
|
||||
{
|
||||
Console.WriteLine(request.Url + " " + request.Failure);
|
||||
};
|
||||
```
|
||||
|
||||
:::note
|
||||
When no [`event: Page.dialog`] listeners are present, all dialogs are automatically dismissed.
|
||||
:::
|
||||
@ -351,6 +374,50 @@ Emitted when the JavaScript [`load`](https://developer.mozilla.org/en-US/docs/We
|
||||
|
||||
Emitted when an uncaught exception happens within the page.
|
||||
|
||||
```js
|
||||
// Log all uncaught errors to the terminal
|
||||
page.on('pageerror', exception => {
|
||||
console.log(`Uncaught exception: "${exception}"`);
|
||||
});
|
||||
|
||||
// Navigate to a page with an exception.
|
||||
await page.goto('data:text/html,<script>throw new Error("Test")</script>');
|
||||
```
|
||||
|
||||
```java
|
||||
// Log all uncaught errors to the terminal
|
||||
page.onPageError(exception -> {
|
||||
System.out.println("Uncaught exception: " + exception);
|
||||
});
|
||||
|
||||
// Navigate to a page with an exception.
|
||||
page.navigate("data:text/html,<script>throw new Error('Test')</script>");
|
||||
```
|
||||
|
||||
```python async
|
||||
# Log all uncaught errors to the terminal
|
||||
page.on("pageerror", lambda exc: print(f"uncaught exception: {exc}"))
|
||||
|
||||
# Navigate to a page with an exception.
|
||||
await page.goto("data:text/html,<script>throw new Error('test')</script>")
|
||||
```
|
||||
|
||||
```python sync
|
||||
# Log all uncaught errors to the terminal
|
||||
page.on("pageerror", lambda exc: print(f"uncaught exception: {exc}"))
|
||||
|
||||
# Navigate to a page with an exception.
|
||||
page.goto("data:text/html,<script>throw new Error('test')</script>")
|
||||
```
|
||||
|
||||
```csharp
|
||||
// Log all uncaught errors to the terminal
|
||||
page.PageError += (_, exception) =>
|
||||
{
|
||||
Console.WriteLine("Uncaught exception: " + exception);
|
||||
};
|
||||
```
|
||||
|
||||
## event: Page.pageError
|
||||
* langs: csharp, java
|
||||
- argument: <[string]>
|
||||
@ -422,6 +489,22 @@ Emitted when a page issues a request. The [request] object is read-only. In orde
|
||||
|
||||
Emitted when a request fails, for example by timing out.
|
||||
|
||||
```js
|
||||
page.on('requestfailed', request => {
|
||||
console.log(request.url() + ' ' + request.failure().errorText);
|
||||
});
|
||||
```
|
||||
|
||||
```java
|
||||
page.onRequestFailed(request -> {
|
||||
System.out.println(request.url() + " " + request.failure());
|
||||
});
|
||||
```
|
||||
|
||||
```python
|
||||
page.on("requestfailed", lambda request: print(request.url + " " + request.failure.error_text))
|
||||
```
|
||||
|
||||
:::note
|
||||
HTTP Error responses, such as 404 or 503, are still successful responses from HTTP standpoint, so request will complete
|
||||
with [`event: Page.requestFinished`] event and not with [`event: Page.requestFailed`]. A request will only be considered
|
||||
|
@ -63,7 +63,7 @@ You can use browser developer tools in Chromium, Firefox and WebKit while runnin
|
||||
a Playwright script in headed mode. Developer tools help to:
|
||||
|
||||
* Inspect the DOM tree and **find element selectors**
|
||||
* **See console logs** during execution (or learn how to [read logs via API](./verification.md#console-logs))
|
||||
* **See console logs** during execution (or learn how to [read logs via API](./api/class-page.md#page-event-console))
|
||||
* Check **network activity** and other developer tools features
|
||||
|
||||
<a href="https://user-images.githubusercontent.com/284612/77234134-5f21a500-6b69-11ea-92ec-1c146e1333ec.png"><img src="https://user-images.githubusercontent.com/284612/77234134-5f21a500-6b69-11ea-92ec-1c146e1333ec.png" width="500" alt="Chromium Developer Tools"></img></a>
|
||||
|
@ -153,7 +153,7 @@ You can use browser developer tools in Chromium, Firefox and WebKit while runnin
|
||||
a Playwright script, with or without Playwright inspector. Developer tools help to:
|
||||
|
||||
* Inspect the DOM tree
|
||||
* **See console logs** during execution (or learn how to [read logs via API](./verification.md#console-logs))
|
||||
* **See console logs** during execution (or learn how to [read logs via API](./api/class-page.md#page-event-console))
|
||||
* Check **network activity** and other developer tools features
|
||||
|
||||
:::note
|
||||
|
@ -1,602 +0,0 @@
|
||||
---
|
||||
id: verification
|
||||
title: "Verification"
|
||||
---
|
||||
|
||||
:::caution
|
||||
We recommend [Web-First Assertions](./test-assertions) that automatically retry until the expected condition is met instead. This helps reducing the flakiness of the tests.
|
||||
:::
|
||||
|
||||
<!-- TOC -->
|
||||
|
||||
## Text content
|
||||
|
||||
```js
|
||||
const content = await page.locator('nav:first-child').textContent();
|
||||
expect(content).toBe('home');
|
||||
```
|
||||
|
||||
```java
|
||||
String content = page.locator("nav:first-child").textContent();
|
||||
assertEquals("home", content);
|
||||
```
|
||||
|
||||
```python async
|
||||
content = await page.locator("nav:first-child").text_content()
|
||||
assert content == "home"
|
||||
```
|
||||
|
||||
```python sync
|
||||
content = page.locator("nav:first-child").text_content()
|
||||
assert content == "home"
|
||||
```
|
||||
|
||||
```csharp
|
||||
var content = await page.Locator("nav:first-child").TextContentAsync();
|
||||
Assert.AreEqual("home", content);
|
||||
```
|
||||
|
||||
### API reference
|
||||
- [`method: Page.textContent`]
|
||||
- [`method: ElementHandle.textContent`]
|
||||
|
||||
## Inner text
|
||||
|
||||
```js
|
||||
const text = await page.locator('.selected').innerText();
|
||||
expect(text).toBe('value');
|
||||
```
|
||||
|
||||
```java
|
||||
String text = page.locator(".selected").innerText();
|
||||
assertEquals("value", text);
|
||||
```
|
||||
|
||||
```python async
|
||||
text = await page.locator(".selected").inner_text()
|
||||
assert text == "value"
|
||||
```
|
||||
|
||||
```python sync
|
||||
text = page.locator(".selected").inner_text()
|
||||
assert text == "value"
|
||||
```
|
||||
|
||||
```csharp
|
||||
var content = await page.Locator(".selected").InnerTextAsync();
|
||||
Assert.AreEqual("value", content);
|
||||
```
|
||||
|
||||
### API reference
|
||||
- [`method: Page.innerText`]
|
||||
- [`method: ElementHandle.innerText`]
|
||||
|
||||
## Attribute value
|
||||
|
||||
```js
|
||||
const alt = await page.locator('input').getAttribute('alt');
|
||||
expect(alt).toBe('Text');
|
||||
```
|
||||
|
||||
```java
|
||||
String alt = page.locator("input").getAttribute("alt");
|
||||
assertEquals("Text", alt);
|
||||
```
|
||||
|
||||
```python async
|
||||
alt = await page.locator("input").get_attribute("alt")
|
||||
assert alt == "Text"
|
||||
```
|
||||
|
||||
```python sync
|
||||
alt = page.locator("input").get_attribute("alt")
|
||||
assert alt == "Text"
|
||||
```
|
||||
|
||||
```csharp
|
||||
var value = await page.Locator("input").GetAttributeAsync("alt");
|
||||
Assert.AreEqual("Text", value);
|
||||
```
|
||||
|
||||
## Checkbox state
|
||||
|
||||
```js
|
||||
const checked = await page.locator('input').isChecked();
|
||||
expect(checked).toBeTruthy();
|
||||
```
|
||||
|
||||
```java
|
||||
boolean checked = page.locator("input").isChecked();
|
||||
assertTrue(checked);
|
||||
```
|
||||
|
||||
```python async
|
||||
checked = await page.locator("input").is_checked()
|
||||
assert checked
|
||||
```
|
||||
|
||||
```python sync
|
||||
checked = page.locator("input").is_checked()
|
||||
assert checked
|
||||
```
|
||||
|
||||
```csharp
|
||||
var checked = await page.Locator("input").IsCheckedAsync();
|
||||
Assert.True(checked);
|
||||
```
|
||||
|
||||
### API reference
|
||||
- [`method: Page.isChecked`]
|
||||
- [`method: ElementHandle.isChecked`]
|
||||
|
||||
## Text content
|
||||
|
||||
```js
|
||||
const content = await page.locator('nav:first-child').textContent();
|
||||
expect(content).toBe('home');
|
||||
```
|
||||
|
||||
```java
|
||||
Object content = page.locator("nav:first-child").textContent();
|
||||
assertEquals("home", content);
|
||||
```
|
||||
|
||||
```python async
|
||||
content = await page.locator("nav:first-child").text_content()
|
||||
assert content == "home"
|
||||
```
|
||||
|
||||
```python sync
|
||||
content = page.locator("nav:first-child").text_content()
|
||||
assert content == "home"
|
||||
```
|
||||
|
||||
```csharp
|
||||
var content = await page.locator("nav:first-child").TextContentAsync();
|
||||
Assert.AreEqual("home", content);
|
||||
```
|
||||
|
||||
### API reference
|
||||
- [`method: Page.evalOnSelector`]
|
||||
- [`method: JSHandle.evaluate`]
|
||||
|
||||
## Inner HTML
|
||||
|
||||
```js
|
||||
const html = await page.locator('div.result').innerHTML();
|
||||
expect(html).toBe('<p>Result</p>');
|
||||
```
|
||||
|
||||
```java
|
||||
String html = page.locator("div.result").innerHTML();
|
||||
assertEquals("<p>Result</p>", html);
|
||||
```
|
||||
|
||||
```python async
|
||||
html = await page.locator("div.result").inner_html()
|
||||
assert html == "<p>Result</p>"
|
||||
```
|
||||
|
||||
```python sync
|
||||
html = page.locator("div.result").inner_html()
|
||||
assert html == "<p>Result</p>"
|
||||
```
|
||||
|
||||
```csharp
|
||||
var html = await page.Locator("div.result").InnerHTMLAsync();
|
||||
Assert.AreEqual("<p>Result</p>", html);
|
||||
```
|
||||
|
||||
### API reference
|
||||
- [`method: Page.innerHTML`]
|
||||
- [`method: ElementHandle.innerHTML`]
|
||||
|
||||
## Visibility
|
||||
|
||||
```js
|
||||
const visible = await page.locator('input').isVisible();
|
||||
expect(visible).toBeTruthy();
|
||||
```
|
||||
|
||||
```java
|
||||
boolean visible = page.locator("input").isVisible();
|
||||
assertTrue(visible);
|
||||
```
|
||||
|
||||
```python async
|
||||
visible = await page.locator("input").is_visible()
|
||||
assert visible
|
||||
```
|
||||
|
||||
```python sync
|
||||
visible = page.locator("input").is_visible()
|
||||
assert visible
|
||||
```
|
||||
|
||||
```csharp
|
||||
var visibility = await page.Locator("input").IsVisibleAsync();
|
||||
Assert.True(visibility);
|
||||
```
|
||||
|
||||
### API reference
|
||||
- [`method: Page.isVisible`]
|
||||
- [`method: ElementHandle.isVisible`]
|
||||
|
||||
## Enabled state
|
||||
|
||||
```js
|
||||
const enabled = await page.locator('input').isEnabled();
|
||||
expect(enabled).toBeTruthy();
|
||||
```
|
||||
|
||||
```java
|
||||
boolean enabled = page.locator("input").isEnabled();
|
||||
assertTrue(enabled);
|
||||
```
|
||||
|
||||
```python async
|
||||
enabled = await page.locator("input").is_enabled()
|
||||
assert enabled
|
||||
```
|
||||
|
||||
```python sync
|
||||
enabled = page.locator("input").is_enabled()
|
||||
assert enabled
|
||||
```
|
||||
|
||||
```csharp
|
||||
var enabled = await page.Locator("input").IsEnabledAsync();
|
||||
Assert.True(enabled);
|
||||
```
|
||||
|
||||
### API reference
|
||||
- [`method: Page.isEnabled`]
|
||||
- [`method: ElementHandle.isEnabled`]
|
||||
|
||||
## Custom assertions
|
||||
|
||||
With Playwright, you can also write custom JavaScript to run in the context of
|
||||
the browser. This is useful in situations where you want to assert for values
|
||||
that are not covered by the convenience APIs above.
|
||||
|
||||
```js
|
||||
// Assert local storage value
|
||||
const userId = page.evaluate(() => window.localStorage.getItem('userId'));
|
||||
expect(userId).toBeTruthy();
|
||||
|
||||
// Assert value for input element
|
||||
const value = await page.locator('#search').inputValue();
|
||||
expect(value === 'query').toBeTruthy();
|
||||
|
||||
// Assert computed style
|
||||
const fontSize = await page.locator('div').evaluate(el => window.getComputedStyle(el).fontSize);
|
||||
expect(fontSize === '16px').toBeTruthy();
|
||||
|
||||
// Assert list length
|
||||
const length = await page.locator('li.selected').count();
|
||||
expect(length === 3).toBeTruthy();
|
||||
```
|
||||
|
||||
```java
|
||||
// Assert local storage value
|
||||
Object userId = page.evaluate("() => window.localStorage.getItem('userId')");
|
||||
assertNotNull(userId);
|
||||
|
||||
// Assert value for input element
|
||||
Object value = page.locator("#search").inputValue();
|
||||
assertEquals("query", value);
|
||||
|
||||
// Assert computed style
|
||||
Object fontSize = page.locator("div").evaluate("el => window.getComputedStyle(el).fontSize");
|
||||
assertEquals("16px", fontSize);
|
||||
|
||||
// Assert list length
|
||||
Object length = page.locator("li.selected").count();
|
||||
assertEquals(3, length);
|
||||
```
|
||||
|
||||
```python async
|
||||
# Assert local storage value
|
||||
user_id = page.evaluate("() => window.localStorage.getItem('user_id')")
|
||||
assert user_id
|
||||
|
||||
# Assert value for input element
|
||||
value = await page.locator('#search').input_value()
|
||||
assert value == 'query'
|
||||
|
||||
# Assert computed style
|
||||
font_size = await page.locator('div').evaluate('el => window.getComputedStyle(el).fontSize')
|
||||
assert font_size == '16px'
|
||||
|
||||
# Assert list length
|
||||
length = await page.locator('li.selected').count()
|
||||
assert length == 3
|
||||
```
|
||||
|
||||
```python sync
|
||||
# Assert local storage value
|
||||
user_id = page.evaluate("() => window.localStorage.getItem('user_id')")
|
||||
assert user_id
|
||||
|
||||
# Assert value for input element
|
||||
value = page.locator('#search').input_value()
|
||||
assert value == 'query'
|
||||
|
||||
# Assert computed style
|
||||
font_size = page.locator('div').evaluate('el => window.getComputedStyle(el).fontSize')
|
||||
assert font_size == '16px'
|
||||
|
||||
# Assert list length
|
||||
length = page.locator('li.selected').count()
|
||||
assert length == 3
|
||||
```
|
||||
|
||||
```csharp
|
||||
// Assert local storage value
|
||||
var userId = await page.EvaluateAsync<string>("() => window.localStorage.getItem('userId')");
|
||||
Assert.NotNull(userId);
|
||||
|
||||
// Assert value for input element
|
||||
var value = await page.Locator("#search").InputValueAsync();
|
||||
Assert.AreEqual("query", value);
|
||||
|
||||
// Assert computed style
|
||||
var fontSize = await page.Locator("div").EvalOnSelectorAsync<string>("el => window.getComputedStyle(el).fontSize");
|
||||
Assert.AreEqual("16px", fontSize);
|
||||
|
||||
// Assert list length
|
||||
var length = await page.Locator("li.selected").CountAsync();
|
||||
Assert.AreEqual(3, length);
|
||||
```
|
||||
|
||||
|
||||
## Console logs
|
||||
|
||||
Console messages logged in the page can be brought into the Playwright context.
|
||||
|
||||
```js
|
||||
// Listen for all console logs
|
||||
page.on('console', msg => console.log(msg.text()))
|
||||
|
||||
// Listen for all console events and handle errors
|
||||
page.on('console', msg => {
|
||||
if (msg.type() === 'error')
|
||||
console.log(`Error text: "${msg.text()}"`);
|
||||
});
|
||||
|
||||
// Get the next console log
|
||||
const [msg] = await Promise.all([
|
||||
page.waitForEvent('console'),
|
||||
// Issue console.log inside the page
|
||||
page.evaluate(() => {
|
||||
console.log('hello', 42, { foo: 'bar' });
|
||||
}),
|
||||
]);
|
||||
|
||||
// Deconstruct console log arguments
|
||||
await msg.args[0].jsonValue() // hello
|
||||
await msg.args[1].jsonValue() // 42
|
||||
```
|
||||
|
||||
```java
|
||||
// Listen for all System.out.printlns
|
||||
page.onConsoleMessage(msg -> System.out.println(msg.text()));
|
||||
|
||||
// Listen for all console events and handle errors
|
||||
page.onConsoleMessage(msg -> {
|
||||
if ("error".equals(msg.type()))
|
||||
System.out.println("Error text: " + msg.text());
|
||||
});
|
||||
|
||||
// Get the next System.out.println
|
||||
ConsoleMessage msg = page.waitForConsoleMessage(() -> {
|
||||
// Issue console.log inside the page
|
||||
page.evaluate("console.log('hello', 42, { foo: 'bar' });");
|
||||
});
|
||||
|
||||
// Deconstruct console.log arguments
|
||||
msg.args().get(0).jsonValue() // hello
|
||||
msg.args().get(1).jsonValue() // 42
|
||||
```
|
||||
|
||||
```python async
|
||||
# Listen for all console logs
|
||||
page.on("console", lambda msg: print(msg.text))
|
||||
|
||||
# Listen for all console events and handle errors
|
||||
page.on("console", lambda msg: print(f"error: {msg.text}") if msg.type == "error" else None)
|
||||
|
||||
# Get the next console log
|
||||
async with page.expect_console_message() as msg_info:
|
||||
# Issue console.log inside the page
|
||||
await page.evaluate("console.log('hello', 42, { foo: 'bar' })")
|
||||
msg = await msg_info.value
|
||||
|
||||
# Deconstruct print arguments
|
||||
await msg.args[0].json_value() # hello
|
||||
await msg.args[1].json_value() # 42
|
||||
```
|
||||
|
||||
```python sync
|
||||
# Listen for all console logs
|
||||
page.on("console", lambda msg: print(msg.text))
|
||||
|
||||
# Listen for all console events and handle errors
|
||||
page.on("console", lambda msg: print(f"error: {msg.text}") if msg.type == "error" else None)
|
||||
|
||||
# Get the next console log
|
||||
with page.expect_console_message() as msg_info:
|
||||
# Issue console.log inside the page
|
||||
page.evaluate("console.log('hello', 42, { foo: 'bar' })")
|
||||
msg = msg_info.value
|
||||
|
||||
# Deconstruct print arguments
|
||||
msg.args[0].json_value() # hello
|
||||
msg.args[1].json_value() # 42
|
||||
```
|
||||
|
||||
```csharp
|
||||
// Listen for all System.out.printlns
|
||||
page.Console += (_, msg) => Console.WriteLine(msg.Text);
|
||||
|
||||
// Listen for all console events and handle errors
|
||||
page.Console += (_, msg) =>
|
||||
{
|
||||
if ("error".Equals(msg.Type))
|
||||
Console.WriteLine("Error text: " + msg.Text);
|
||||
};
|
||||
|
||||
// Get the next System.out.println
|
||||
var waitForMessageTask = page.WaitForConsoleMessageAsync();
|
||||
await page.EvaluateAsync("console.log('hello', 42, { foo: 'bar' });");
|
||||
var message = await waitForMessageTask;
|
||||
// Deconstruct console.log arguments
|
||||
await message.Args.ElementAt(0).JsonValueAsync<string>(); // hello
|
||||
await message.Args.ElementAt(1).JsonValueAsync<int>(); // 42
|
||||
```
|
||||
|
||||
### API reference
|
||||
- [ConsoleMessage]
|
||||
- [Page]
|
||||
- [`event: Page.console`]
|
||||
|
||||
<br/>
|
||||
|
||||
## Page errors
|
||||
|
||||
Listen for uncaught exceptions in the page with the `pagerror` event.
|
||||
|
||||
```js
|
||||
// Log all uncaught errors to the terminal
|
||||
page.on('pageerror', exception => {
|
||||
console.log(`Uncaught exception: "${exception}"`);
|
||||
});
|
||||
|
||||
// Navigate to a page with an exception.
|
||||
await page.goto('data:text/html,<script>throw new Error("Test")</script>');
|
||||
```
|
||||
|
||||
```java
|
||||
// Log all uncaught errors to the terminal
|
||||
page.onPageError(exception -> {
|
||||
System.out.println("Uncaught exception: " + exception);
|
||||
});
|
||||
|
||||
// Navigate to a page with an exception.
|
||||
page.navigate("data:text/html,<script>throw new Error('Test')</script>");
|
||||
```
|
||||
|
||||
```python async
|
||||
# Log all uncaught errors to the terminal
|
||||
page.on("pageerror", lambda exc: print(f"uncaught exception: {exc}"))
|
||||
|
||||
# Navigate to a page with an exception.
|
||||
await page.goto("data:text/html,<script>throw new Error('test')</script>")
|
||||
```
|
||||
|
||||
```python sync
|
||||
# Log all uncaught errors to the terminal
|
||||
page.on("pageerror", lambda exc: print(f"uncaught exception: {exc}"))
|
||||
|
||||
# Navigate to a page with an exception.
|
||||
page.goto("data:text/html,<script>throw new Error('test')</script>")
|
||||
```
|
||||
|
||||
```csharp
|
||||
// Log all uncaught errors to the terminal
|
||||
page.PageError += (_, exception) =>
|
||||
{
|
||||
Console.WriteLine("Uncaught exception: " + exception);
|
||||
};
|
||||
```
|
||||
|
||||
### API reference
|
||||
- [Page]
|
||||
- [`event: Page.pageError`]
|
||||
|
||||
<br/>
|
||||
|
||||
## Page events
|
||||
|
||||
#### `"requestfailed"`
|
||||
|
||||
```js
|
||||
page.on('requestfailed', request => {
|
||||
console.log(request.url() + ' ' + request.failure().errorText);
|
||||
});
|
||||
```
|
||||
|
||||
```java
|
||||
page.onRequestFailed(request -> {
|
||||
System.out.println(request.url() + " " + request.failure());
|
||||
});
|
||||
```
|
||||
|
||||
```python
|
||||
page.on("requestfailed", lambda request: print(request.url + " " + request.failure.error_text))
|
||||
```
|
||||
|
||||
#### `"dialog"` - handle alert, confirm, prompt
|
||||
|
||||
```js
|
||||
page.on('dialog', dialog => {
|
||||
dialog.accept();
|
||||
});
|
||||
```
|
||||
|
||||
```java
|
||||
page.onDialog(dialog -> {
|
||||
dialog.accept();
|
||||
});
|
||||
```
|
||||
|
||||
```python
|
||||
page.on("dialog", lambda dialog: dialog.accept())
|
||||
```
|
||||
|
||||
```csharp
|
||||
page.RequestFailed += (_, request) =>
|
||||
{
|
||||
Console.WriteLine(request.Url + " " + request.Failure);
|
||||
};
|
||||
```
|
||||
|
||||
#### `"popup"` - handle popup windows
|
||||
|
||||
```js
|
||||
const [popup] = await Promise.all([
|
||||
page.waitForEvent('popup'),
|
||||
page.click('#open')
|
||||
]);
|
||||
```
|
||||
|
||||
```java
|
||||
Page popup = page.waitForPopup(() -> {
|
||||
page.click("#open");
|
||||
});
|
||||
```
|
||||
|
||||
```python async
|
||||
async with page.expect_popup() as popup_info:
|
||||
await page.click("#open")
|
||||
popup = await popup_info.value
|
||||
```
|
||||
|
||||
```python sync
|
||||
with page.expect_popup() as popup_info:
|
||||
page.click("#open")
|
||||
popup = popup_info.value
|
||||
```
|
||||
|
||||
```csharp
|
||||
var popup = await page.RunAndWaitForPopupAsync(async () =>
|
||||
{
|
||||
await page.ClickAsync("#open");
|
||||
});
|
||||
```
|
||||
|
||||
### API reference
|
||||
- [Page]
|
||||
- [`event: Page.requestFailed`]
|
||||
- [`event: Page.dialog`]
|
||||
- [`event: Page.popup`]
|
97
packages/playwright-core/types/types.d.ts
vendored
97
packages/playwright-core/types/types.d.ts
vendored
@ -888,6 +888,12 @@ export interface Page {
|
||||
* [freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#never_blocking) waiting for the dialog, and
|
||||
* actions like click will never finish.
|
||||
*
|
||||
* ```js
|
||||
* page.on('dialog', dialog => {
|
||||
* dialog.accept();
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* > NOTE: When no [page.on('dialog')](https://playwright.dev/docs/api/class-page#page-event-dialog) listeners are present,
|
||||
* all dialogs are automatically dismissed.
|
||||
*/
|
||||
@ -942,6 +948,17 @@ export interface Page {
|
||||
|
||||
/**
|
||||
* Emitted when an uncaught exception happens within the page.
|
||||
*
|
||||
* ```js
|
||||
* // Log all uncaught errors to the terminal
|
||||
* page.on('pageerror', exception => {
|
||||
* console.log(`Uncaught exception: "${exception}"`);
|
||||
* });
|
||||
*
|
||||
* // Navigate to a page with an exception.
|
||||
* await page.goto('data:text/html,<script>throw new Error("Test")</script>');
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
on(event: 'pageerror', listener: (error: Error) => void): this;
|
||||
|
||||
@ -982,6 +999,12 @@ export interface Page {
|
||||
/**
|
||||
* Emitted when a request fails, for example by timing out.
|
||||
*
|
||||
* ```js
|
||||
* page.on('requestfailed', request => {
|
||||
* console.log(request.url() + ' ' + request.failure().errorText);
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* > NOTE: HTTP Error responses, such as 404 or 503, are still successful responses from HTTP standpoint, so request will
|
||||
* complete with [page.on('requestfinished')](https://playwright.dev/docs/api/class-page#page-event-request-finished) event
|
||||
* and not with [page.on('requestfailed')](https://playwright.dev/docs/api/class-page#page-event-request-failed). A request
|
||||
@ -1161,6 +1184,12 @@ export interface Page {
|
||||
* [freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#never_blocking) waiting for the dialog, and
|
||||
* actions like click will never finish.
|
||||
*
|
||||
* ```js
|
||||
* page.on('dialog', dialog => {
|
||||
* dialog.accept();
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* > NOTE: When no [page.on('dialog')](https://playwright.dev/docs/api/class-page#page-event-dialog) listeners are present,
|
||||
* all dialogs are automatically dismissed.
|
||||
*/
|
||||
@ -1215,6 +1244,17 @@ export interface Page {
|
||||
|
||||
/**
|
||||
* Emitted when an uncaught exception happens within the page.
|
||||
*
|
||||
* ```js
|
||||
* // Log all uncaught errors to the terminal
|
||||
* page.on('pageerror', exception => {
|
||||
* console.log(`Uncaught exception: "${exception}"`);
|
||||
* });
|
||||
*
|
||||
* // Navigate to a page with an exception.
|
||||
* await page.goto('data:text/html,<script>throw new Error("Test")</script>');
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
addListener(event: 'pageerror', listener: (error: Error) => void): this;
|
||||
|
||||
@ -1255,6 +1295,12 @@ export interface Page {
|
||||
/**
|
||||
* Emitted when a request fails, for example by timing out.
|
||||
*
|
||||
* ```js
|
||||
* page.on('requestfailed', request => {
|
||||
* console.log(request.url() + ' ' + request.failure().errorText);
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* > NOTE: HTTP Error responses, such as 404 or 503, are still successful responses from HTTP standpoint, so request will
|
||||
* complete with [page.on('requestfinished')](https://playwright.dev/docs/api/class-page#page-event-request-finished) event
|
||||
* and not with [page.on('requestfailed')](https://playwright.dev/docs/api/class-page#page-event-request-failed). A request
|
||||
@ -3528,6 +3574,12 @@ export interface Page {
|
||||
* [freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#never_blocking) waiting for the dialog, and
|
||||
* actions like click will never finish.
|
||||
*
|
||||
* ```js
|
||||
* page.on('dialog', dialog => {
|
||||
* dialog.accept();
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* > NOTE: When no [page.on('dialog')](https://playwright.dev/docs/api/class-page#page-event-dialog) listeners are present,
|
||||
* all dialogs are automatically dismissed.
|
||||
*/
|
||||
@ -3582,6 +3634,17 @@ export interface Page {
|
||||
|
||||
/**
|
||||
* Emitted when an uncaught exception happens within the page.
|
||||
*
|
||||
* ```js
|
||||
* // Log all uncaught errors to the terminal
|
||||
* page.on('pageerror', exception => {
|
||||
* console.log(`Uncaught exception: "${exception}"`);
|
||||
* });
|
||||
*
|
||||
* // Navigate to a page with an exception.
|
||||
* await page.goto('data:text/html,<script>throw new Error("Test")</script>');
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
waitForEvent(event: 'pageerror', optionsOrPredicate?: { predicate?: (error: Error) => boolean | Promise<boolean>, timeout?: number } | ((error: Error) => boolean | Promise<boolean>)): Promise<Error>;
|
||||
|
||||
@ -3622,6 +3685,12 @@ export interface Page {
|
||||
/**
|
||||
* Emitted when a request fails, for example by timing out.
|
||||
*
|
||||
* ```js
|
||||
* page.on('requestfailed', request => {
|
||||
* console.log(request.url() + ' ' + request.failure().errorText);
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* > NOTE: HTTP Error responses, such as 404 or 503, are still successful responses from HTTP standpoint, so request will
|
||||
* complete with [page.on('requestfinished')](https://playwright.dev/docs/api/class-page#page-event-request-finished) event
|
||||
* and not with [page.on('requestfailed')](https://playwright.dev/docs/api/class-page#page-event-request-failed). A request
|
||||
@ -13079,7 +13148,33 @@ export interface BrowserServer {
|
||||
|
||||
/**
|
||||
* [ConsoleMessage] objects are dispatched by page via the
|
||||
* [page.on('console')](https://playwright.dev/docs/api/class-page#page-event-console) event.
|
||||
* [page.on('console')](https://playwright.dev/docs/api/class-page#page-event-console) event. For each console messages
|
||||
* logged in the page there will be corresponding event in the Playwright context.
|
||||
*
|
||||
* ```js
|
||||
* // Listen for all console logs
|
||||
* page.on('console', msg => console.log(msg.text()))
|
||||
*
|
||||
* // Listen for all console events and handle errors
|
||||
* page.on('console', msg => {
|
||||
* if (msg.type() === 'error')
|
||||
* console.log(`Error text: "${msg.text()}"`);
|
||||
* });
|
||||
*
|
||||
* // Get the next console log
|
||||
* const [msg] = await Promise.all([
|
||||
* page.waitForEvent('console'),
|
||||
* // Issue console.log inside the page
|
||||
* page.evaluate(() => {
|
||||
* console.log('hello', 42, { foo: 'bar' });
|
||||
* }),
|
||||
* ]);
|
||||
*
|
||||
* // Deconstruct console log arguments
|
||||
* await msg.args[0].jsonValue() // hello
|
||||
* await msg.args[1].jsonValue() // 42
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
export interface ConsoleMessage {
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user