Skip to content

Commit 52492cf

Browse files
committed
docs(examples): working with selectors
1 parent b24262b commit 52492cf

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

docs/examples/README.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Get started with examples
22

3-
Learn how to install Playwright, set up your dev environment to author Playwright scripts, and example recipes to bootstrap your scripts.
3+
Learn how to install Playwright, set up your dev environment, and read through example recipes. Use the [API reference](../api.md) for more exhaustive documentation.
44

55
## Installing Playwright
66

@@ -25,24 +25,54 @@ const { chromium } = require('playwright');
2525

2626
Playwright scripts can be developed just like any other Node.js script. For example, you can use the [Node.js debugger](https://nodejs.org/api/debugger.html) or [VS Code debugging](https://code.visualstudio.com/docs/nodejs/nodejs-debugging) to set breakpoints and get fine grained control over execution.
2727

28-
### Running browsers for debugging
28+
#### Running browsers for debugging
29+
30+
<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="300" alt="Chromium Developer Tools" align="right"></a>
2931

3032
By default, Playwright runs the browsers in headless mode. To see the browser UI, pass the `headless: false` flag while launching the browser. You can also use `slowMo` to slow down execution.
3133

3234
```js
33-
chromium.launch({ headless: false, slowMo: 50 });
35+
chromium.launch({ headless: false, slowMo: 50 });
3436
```
3537

3638
It is also possible to open **browser developer tools** during execution, to inspect the DOM tree or network activity. This is possible in Chromium, Firefox and WebKit.
3739

38-
<p align="center"><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"></a></p>
3940

4041
## Core concepts
4142

4243
* A [`Browser`](../api.md#class-browser) refers to an instance of Chromium, Firefox or WebKit browsers.
4344
* A [`BrowserContext`](../api.md#class-browsercontext) is an isolated incognito session within a browser instance. Browser contexts are fast to create and can be used to parallelize isolated test executions.
4445
* A [`Page`](../api.md#class-page) refers to a single tab within a browser context, which includes one or more [`Frame`](../api.md#class-frame) objects.
4546

47+
#### Working with elements
48+
49+
* Selector engines: Playwright can query elements on a web page through [multiple selector engines](../selectors.md) like CSS, HTML attributes, XPath and text contents.
50+
* Actions on elements: Use methods like [`page.click`](../api.md#pageclickselector-options) or [`page.fill`](../api.md#pagefillselector-value-options) to execute actions on elements.
51+
* **Auto-wait** for elements: These actions auto-wait for the element to be ready. If these actions result in page navigations, they are also awaited automatically.
52+
53+
```js
54+
// Wait for element to be ready, click it and wait for navigations (if any)
55+
await page.click('text="Login"');
56+
```
57+
58+
* Use the [`page.waitForSelector`](../api.md#pagewaitforselectorselector-options) method to explicitly wait for elements.
59+
60+
```js
61+
// Explicitly wait for the #search field to be present in the DOM
62+
const search = await page.waitForSelector('#search');
63+
// Retrieve the query value from the element
64+
const query = await search.evaluate(element => element.value);
65+
```
66+
67+
* Assertions on elements: Use [`page.$`](../api.md#pageselector) to get the element from the page and [`page.$eval`](../api.md#pageevalselector-pagefunction-arg-1) to run a JS function in the execution context of the page.
68+
* These can be used to assert element properties, like visibility or text contents.
69+
* These methods behave similar to the `$` operator in DevTools console or jQuery. They fetch the element from the page without waiting. If required, use `page.waitForSelector` and `evaluate` instead, as described above.
70+
71+
```js
72+
// Get value of the #search field
73+
const query = await page.$eval('#search', element => element.value);
74+
```
75+
4676
## Example recipes
4777

4878
### [Authentication](authentication.js)
@@ -60,4 +90,4 @@ Other examples
6090
* Handling a popup, eg, accept dialog
6191
* Page navigation and wait for load
6292
* Async page load (see #662)
63-
-->
93+
-->

docs/showcase.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* [react-app-playwright](https://github.com/KyleADay/react-app-playwright): Using Playwright with a create-react-app project
3737
* [playwright-react-typescript-jest-example](https://github.com/azemetre/playwright-react-typescript-jest-example): Using Playwright + Jest with a custom webpack configuration for React + Typescript project
3838
* [playwright-mocha](https://github.com/roggerfe/playwright-mocha): Using Playwright with Mocha and Chai
39+
* [playwright-cljs](https://github.com/apeckham/playwright-cljs): Playwright examples in ClojureScript
3940
* [playwright-github-actions](https://github.com/arjun27/playwright-github-actions/actions): Playwright setup on GitHub Actions
4041
* [playwright-azure-functions](https://github.com/arjun27/playwright-azure-functions): Playwright setup on Azure Functions
4142
* [playwright-aws-lambda](https://github.com/austinkelleher/playwright-aws-lambda): Playwright setup on AWS Lambda

0 commit comments

Comments
 (0)