Skip to content

Commit

Permalink
Add WaitForFunctionAsync doc (#1504)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored Jun 19, 2020
1 parent 44bc751 commit 66491e0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docfx_project/examples/Page.WaitForFunctionAsync.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# How to wait for a javascript expression to be true
_Contributors: [Darío Kondratiuk](https://www.hardkoded.com)_

## Problem

`WaitForSelectorAsync` is not enough, you want to wait for a more complex javascript expression to be truthly.

## Solution

Use [Page.WaitForExpressionAsync](https://www.puppeteersharp.com/api/PuppeteerSharp.Page.html#PuppeteerSharp_Page_WaitForExpressionAsync_System_String_PuppeteerSharp_WaitForFunctionOptions_) or [Page.WaitForFunctionAsync](https://www.puppeteersharp.com/api/PuppeteerSharp.Page.html#PuppeteerSharp_Page_WaitForFunctionAsync_System_String_PuppeteerSharp_WaitForFunctionOptions_System_Object___) to delay execution until the result of a javascription expression is truthly.

If it's a simple expression you can use `WaitForFunctionAsync`:

```cs
using (var browser = await Puppeteer.LaunchAsync(options))
using (var page = await browser.NewPageAsync())
{
await page.GoToAsync("https://www.somepage.com");
await Page.WaitForExpressionAsync("document.queryselector('#status_info').innerText.match('^Showing ([1-9][0-9]*?) to ([1-9][0-9]*?)') of ([1-9][0-9]*?) entries') != null");
}
```

If the evaluation is more complex, you could wrap it inside a function and use `WaitForFunctionAsync`:

```cs
var waitTask = Page.WaitForFunctionAsync(@"() =>
{
return document.queryselector('#status_info').innerText.match('^Showing ([1-9][0-9]*?) to ([1-9][0-9]*?)') of ([1-9][0-9]*?) entries') != null;
}");
```
2 changes: 2 additions & 0 deletions docfx_project/examples/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
href: ReuseChrome.md
- name: How to test a Chrome Extension
href: ChromeExtension.md
- name: How to wait for a javascript expression to be true
- href: Page.WaitForFunctionAsync.md
- name: Advanced
items:
- name: How to log CDP communication
Expand Down

0 comments on commit 66491e0

Please sign in to comment.