From 7da1dfd21c21086b48cef68caad650cf0cf05ddb Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Wed, 27 Sep 2023 11:54:02 -0700 Subject: [PATCH] docs: add C# code snippet to extensibility.md (#27328) Fixes #27160. --- docs/src/extensibility.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/src/extensibility.md b/docs/src/extensibility.md index 05065d95fc..cb5b9170ce 100644 --- a/docs/src/extensibility.md +++ b/docs/src/extensibility.md @@ -156,3 +156,29 @@ page.locator("tag=div").get_by_text("click me").click() # we can use it in any methods supporting selectors. button_count = page.locator("tag=button").count() ``` + +```csharp +// Register the engine. Selectors will be prefixed with "tag=". +// The script is evaluated in the page context. +await playwright.Selectors.Register("tag", new() { + Script = @" + // Must evaluate to a selector engine instance. + { + // Returns the first element matching given selector in the root's subtree. + query(root, selector) { + return root.querySelector(selector); + }, + + // Returns all elements matching given selector in the root's subtree. + queryAll(root, selector) { + return Array.from(root.querySelectorAll(selector)); + } + }" +}); + +// Now we can use "tag=" selectors. +await page.Locator("tag=button").ClickAsync(); + +// We can combine it with built-in locators. +await page.Locator("tag=div").GetByText("Click me").ClickAsync(); +```