You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Playwright API can be used to read element contents and properties for test assertions. These values are fetched from the browser page and asserted in
4
+
Node.js.
5
+
6
+
The examples in this guide use the built-in [`assert` module](https://nodejs.org/api/assert.html), but they can be used with any assertion library (like [Expect](https://www.npmjs.com/package/expect) or [Chai](https://www.npmjs.com/package/chai)). See [Test runners](test-runners.md) for more info.
7
+
8
+
<!-- GEN:toc-top-level -->
9
+
-[Common patterns](#common-patterns)
10
+
-[Element Handles](#element-handles)
11
+
-[Custom assertions](#custom-assertions)
12
+
<!-- GEN:stop -->
13
+
14
+
<br/>
15
+
16
+
## Common patterns
17
+
18
+
Playwright provides convenience APIs for common assertion tasks, like finding the
19
+
text content of an element. These APIs require a [selector](selectors.md) to locate
Playwright scripts run in your Node.js environment. You page scripts run in the page environment. Those environments don't intersect, they are running in different virtual machines in different processes and potentially on different computers.
233
-
234
-
IMAGE PLACEHOLDER
232
+
Playwright scripts run in your Node.js environment. You page scripts run in the browser page environment. Those environments don't intersect, they are running in different virtual machines in different processes and even potentially on different computers.
235
233
236
234
The [`page.evaluate`](https://github.com/microsoft/playwright/blob/master/docs/api.md#pageevaluatepagefunction-arg) API can run a JavaScript function in the context
237
-
of the web page and bring results back to the Node.js environment. Globals like
238
-
`window` and `document` along with the web page runtime can be used in `evaluate`.
235
+
of the web page and bring results back to the Node.js environment. Browser globals like
236
+
`window` and `document` can be used in `evaluate`.
If the result is a Promise or if the function is asynchronous evaluate will automatically wait until it's resolved:
243
+
```js
244
+
conststatus=awaitpage.evaluate(async () => {
245
+
constresponse=awaitfetch(location.href);
246
+
returnresponse.status;
247
+
});
248
+
```
249
+
250
+
### Evaluation
251
+
252
+
Functions passed inside `page.evaluate` can accept parameters. These parameters are
253
+
serialized and sent into your web page over the wire. You can pass primitive types, JSON-alike objects and remote object handles received from the page.
Playwright has an API to create Node-side handles to the page DOM elements or any other objects inside the page.
268
-
These handles live in the Node.js process, whereas the actual objects reside in browser.
282
+
## Object & Element handles
269
283
270
-
IMAGE PLACEHOLDER
284
+
Playwright can create Node-side handles to the page DOM elements or any other objects inside the page. These handles live in the Node.js process, whereas the actual objects reside in browser.
271
285
272
286
There are two types of handles:
273
-
-[`JSHandle`](./api.md#class-jshandle) to reference any javascript objects in the page
287
+
-[`JSHandle`](./api.md#class-jshandle) to reference any JavaScript objects in the page
274
288
-[`ElementHandle`](./api.md#class-elementhandle) to reference DOM elements in the page
275
289
276
-
Note that since any DOM element in the page is also a javascript object,
290
+
Note that since any DOM element in the page is also a JavaScript object,
- Handles can be acquired using the page methods [`page.evaluateHandle`](./api.md#pageevaluatehandlepagefunction-arg), [`page.$`](./api.md#pageselector) or [`page.$$`](./api.md#pageselector-1) or
282
296
their frame counterparts [`frame.evaluateHandle`](./api.md#frameevaluatehandlepagefunction-arg), [`frame.$`](./api.md#frameselector) or [`frame.$$`](./api.md#frameselector-1).
283
-
- Once created, handles will retain object from [garbage collection](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management)
297
+
- Once created, handles will retain object from [garbage collection](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management).
284
298
- Handles will be **automatically disposed** once the page or frame they belong to navigates or closes.
285
299
- Handles can be **manually disposed** using [`jsHandle.dispose`](./api.md#jshandledispose) method.
286
300
287
-
Here is how you can use these handles:
301
+
### Example: ElementHandle
288
302
289
303
```js
290
304
// The first parameter of the elementHandle.evaluate callback is the element handle points to.
0 commit comments