-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(examples): add section on working with elements #1624
Conversation
docs/examples/README.md
Outdated
|
||
* Types of selectors: Playwright can query elements on a web page through multiple selector engines like CSS, HTML attributes, XPath and text contents. | ||
* See the [selectors guide](../selectors.md). | ||
* Actions on elements: Use methods like [`page.click`](../api.md#pageclickselector-options) or [`page.fill`](#pagefillselector-value-options) to execute actions on elements. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
page.fill
link is broken.
docs/examples/README.md
Outdated
* Use the [`page.waitForSelector`](../api.md#pagewaitforselectorselector-options) method to explicitly wait for elements. | ||
* Assertions on elements: Use [`page.$`](../api.md#pageselector) to fetch 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. | ||
* These can be used to assert the visibility or contents of the elements. | ||
* The semantics for these methods are the same as the `$` in the DevTools console or in jQuery. They fetch the element from the page without waiting. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's reiterate that most of the time you want waitForSelector
.
docs/examples/README.md
Outdated
@@ -43,6 +43,17 @@ It is also possible to open **browser developer tools** during execution, to ins | |||
* 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. | |||
* 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. | |||
|
|||
### Working with elements |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this section will benefit from small code snippet for each bullet.
2f2ce01
to
b4c510a
Compare
Thanks for the suggestions - made these changes. Please take another look |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dropped some code suggestions, otherwise looks great.
docs/examples/README.md
Outdated
|
||
```js | ||
// Explicitly wait for element to be present in the DOM | ||
await page.waitForSelector('data-test-id=foo'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Explicitly wait for the #search field to be present in the DOM
const search = await page.waitForSelector('#search');
// Retrieve the query
const query = await search.evaluate(element => element.value);
docs/examples/README.md
Outdated
|
||
```js | ||
// Get value of the #search field | ||
await page.$eval('#search', element => element.value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const query = await page.$eval('#search', element => element.value);
docs/examples/README.md
Outdated
|
||
* 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. | ||
* These can be used to assert element properties, like visibility or text contents. | ||
* 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` to explicitly wait here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If required, use page.waitForSelector
and evaluate
instead, as described above.
With the goal of introducing the API to new comers, I've added a section to the getting started guide on elements, selectors and introduced some key API methods.