Skip to content

Commit 80a7fcd

Browse files
committed
docs(verification): nits and typos
1 parent 948d51d commit 80a7fcd

File tree

2 files changed

+91
-96
lines changed

2 files changed

+91
-96
lines changed

docs/README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232
- [Modify requests](./network.md#modify-requests)
3333
- [Abort requests](./network.md#abort-requests)
3434
1. [Scraping and verification](./verification.md)
35-
- [Screenshots](./verification.md#capturing-screenshot)
36-
- [Evaluation](./verification.md#evaluating-javascript)
37-
- [Console messages](./verification.md#listening-console-messages)
38-
- [Uncaghut exceptions](./verification.md#uncaghut-exceptions)
39-
- [Page crashes](./verification.md#page-crashes)
35+
- [Evaluating JavaScript](./verification.md#evaluating-javascript)
36+
- [Capturing screenshot](./verification.md#capturing-screenshot)
37+
- [Page events](./verification.md#page-events)
38+
- [Handling exceptions](./verification.md#handling-exceptions)
4039
1. [Navigation and loading](./loading.md)
4140
- [Common scenarios](./loading.md#common-scenarios)
4241
- [Loading a popup](./loading.md#loading-a-popup)

docs/verification.md

Lines changed: 87 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,54 @@
11
# Scraping and verification
22

3-
Playwright allows verifiying state of the page and catching abnormal behavior by:
4-
- evaluating JavaScript code snippets in the page
5-
- capturing screenshots (png or jpeg)
6-
- listening console messages
7-
- observing uncaghut exceptions in the page
8-
- observing page crashes
9-
- etc
10-
113
#### Contents
124
- [Evaluating JavaScript](#evaluating-javascript)
135
- [Capturing screenshot](#capturing-screenshot)
14-
- [Listening console messages](#listening-console-messages)
15-
- [Uncaghut exceptions](#uncaghut-exceptions)
16-
- [Page crashes](#page-crashes)
6+
- [Page events](#page-events)
7+
- [Handling exceptions](#handling-exceptions)
178

189
<br/>
1910

2011
## Evaluating JavaScript
2112

2213
Execute JavaScript function in the page:
2314
```js
24-
const href = await page.evaluate(() => document.location.href);
15+
const href = await page.evaluate(() => document.location.href);
2516
```
2617

27-
If the result is a Promise or if the function is asynchronouse eveluate will automatically wait until it's resolved:
18+
If the result is a Promise or if the function is asynchronous evaluate will automatically wait until it's resolved:
2819
```js
29-
const status = await page.evaluate(async () => {
30-
const response = await fetch(location.href);
31-
return response.status;
32-
});
20+
const status = await page.evaluate(async () => {
21+
const response = await fetch(location.href);
22+
return response.status;
23+
});
3324
```
3425

3526
Get object handle and use it in multiple evaluations:
3627
```js
37-
// Create a new array in the page, wriate a reference to it in
38-
// window.myArray and get a handle to it.
39-
const myArrayHandle = await page.evaluateHandle(() => {
40-
window.myArray = [1];
41-
return myArray;
42-
});
43-
44-
// Get current length of the array using the handle.
45-
const length = await page.evaluate(
46-
(arg) => arg.myArray.length,
47-
{ myArray: myArrayHandle }
48-
);
49-
50-
// Add one more element to the array using the handle
51-
await page.evaluate((arg) => arg.myArray.push(arg.newElement), {
52-
myArray:myArrayHandle,
53-
newElement: 2
54-
});
55-
56-
// Get current length of the array using window.myArray reference.
57-
const newLength = await page.evaluate(() => window.myArray.length);
58-
59-
// Release the object when it's no longer needed.
60-
await myArrayHandle.dispose();
28+
// Create a new array in the page, write a reference to it in
29+
// window.myArray and get a handle to it.
30+
const myArrayHandle = await page.evaluateHandle(() => {
31+
window.myArray = [1];
32+
return myArray;
33+
});
34+
35+
// Get current length of the array using the handle.
36+
const length = await page.evaluate(
37+
(arg) => arg.myArray.length,
38+
{ myArray: myArrayHandle }
39+
);
40+
41+
// Add one more element to the array using the handle
42+
await page.evaluate((arg) => arg.myArray.push(arg.newElement), {
43+
myArray: myArrayHandle,
44+
newElement: 2
45+
});
46+
47+
// Get current length of the array using window.myArray reference.
48+
const newLength = await page.evaluate(() => window.myArray.length);
49+
50+
// Release the object when it's no longer needed.
51+
await myArrayHandle.dispose();
6152
```
6253

6354
#### API reference
@@ -75,90 +66,95 @@ Get object handle and use it in multiple evaluations:
7566

7667
Take screenshot of the page's viewport and save it in a png file:
7768
```js
78-
await page.screenshot({path: 'screenshot.png'});
69+
await page.screenshot({path: 'screenshot.png'});
7970
```
80-
Capture entire scrollable area of the page:
71+
72+
#### Variations
73+
74+
Capture particular element:
8175
```js
82-
await page.screenshot({path: 'screenshot.png', fullPage: true});
76+
const elementHandle = await page.$('.header');
77+
await elementHandle.screenshot({ path: 'screenshot.png' });
8378
```
84-
Save screenshot in an in-memory buffer (the content is base64-encoded image bytes):
79+
80+
Capture full page screenshot:
81+
```js
82+
await page.screenshot({path: 'screenshot.png', fullPage: true});
83+
```
84+
85+
Capture screenshot into a Node [Buffer](https://nodejs.org/api/buffer.html#buffer_class_buffer).
8586
```js
86-
const buffer = await page.screenshot();
87-
// Log the length.
88-
console.log(buffer.length);
87+
const buffer = await page.screenshot();
88+
// Log the length.
89+
console.log(buffer.toString('base64'));
8990
```
9091

9192

9293
#### API reference
9394

9495
- [page.screenshot([options])](./api.md#pagescreenshotoptions)
95-
- [Node.js Buffer](https://nodejs.org/api/buffer.html)
96+
- [elementHandle.screenshot([options])](./api.md#elementhandlescreenshotoptions)
9697

9798
<br/>
9899

99-
## Listening console messages
100+
## Page events
100101

101-
Listen all console messages in a page and dump _errors_ in to the terminal:
102+
You can listen for various events on the `page` object. Following are just some of the examples of the events you can assert and handle:
103+
104+
#### "`console`" - get all console messages from the page
102105

103106
```js
104-
// Get all console messages from the page.
105-
page.on('console', msg => {
106-
// Handle only errors.
107-
if (msg.type() !== 'error')
108-
return;
109-
console.log(`text: "${msg.text()}"`);
110-
});
111-
112-
await page.evaluate(() => console.error('Page error message'));
113-
```
114-
Get access to the console message arguments:
115-
```js
116-
page.on('console', msg => {
117-
for (let i = 0; i < msg.args().length; ++i)
118-
console.log(`${i}: ${msg.args()[i]}`);
119-
});
107+
page.on('console', msg => {
108+
// Handle only errors.
109+
if (msg.type() !== 'error')
110+
return;
111+
console.log(`text: "${msg.text()}"`);
112+
});
120113
```
121114

122-
#### API reference
123-
124-
- [class: ConsoleMessage](./api.md#class-consolemessage)
125-
- [class `JSHandle`](./api.md#class-jshandle)
126-
- [event: 'console'](./api.md#event-console)
127-
128-
<br/>
115+
#### "`dialog`" - Handle assert, confirm, prompt
129116

117+
```js
118+
page.on('dialog', dialog => {
119+
dialog.accept();
120+
});
121+
```
130122

131-
## Uncaghut exceptions
123+
#### "`popup`" - Handle assert, confirm, prompt
132124

133-
Listen uncaghut exceptions in the page:
134125
```js
135-
// Log all uncaught errors to the terminal
136-
page.on('pageerror', exception => {
137-
console.log(`Uncaught exception: "${exception}"`);
138-
});
139-
140-
// Navigate to a page with an exception.
141-
await page.goto('data:text/html,<script>throw new Error("Test")</script>');
126+
const [popup] = await Promise.all([
127+
page.waitForEvent('popup'),
128+
page.click('#open')
129+
]);
142130
```
143131

144132
#### API reference
145133

146-
- [event: 'pageerror'](./api.md#event-pageerror)
134+
- [class: Page](./api.md#class-page)
135+
- [event: 'console'](./api.md#event-console)
136+
- [event: 'dialog'](./api.md#event-dialog)
137+
- [event: 'popup'](./api.md#event-popup)
138+
- [class: ConsoleMessage](./api.md#class-consolemessage)
147139

148140
<br/>
149141

150-
## Page crashes
151142

152-
Listen to page crashes:
143+
## Handling exceptions
144+
145+
Listen uncaught exceptions in the page:
153146
```js
154-
page.on('crash', exception => {
155-
console.log(`Page crashed`);
156-
});
147+
// Log all uncaught errors to the terminal
148+
page.on('pageerror', exception => {
149+
console.log(`Uncaught exception: "${exception}"`);
150+
});
151+
152+
// Navigate to a page with an exception.
153+
await page.goto('data:text/html,<script>throw new Error("Test")</script>');
157154
```
158-
It's very unusual for page to crash but might happen if a page allocates too much memory or due to a bug in a browser.
159155

160156
#### API reference
161157

162-
- [event: 'crash'](./api.md#event-crash)
158+
- [event: 'pageerror'](./api.md#event-pageerror)
163159

164160
<br/>

0 commit comments

Comments
 (0)