Skip to content
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

[Documentation] Custom selector engines before page is created #2750

Open
Exoow opened this issue Nov 9, 2023 · 3 comments
Open

[Documentation] Custom selector engines before page is created #2750

Exoow opened this issue Nov 9, 2023 · 3 comments

Comments

@Exoow
Copy link
Contributor

Exoow commented Nov 9, 2023

The documentation (both in code and https://playwright.dev/dotnet/docs/extensibility) states:
Selectors must be registered before creating the page.

However the code below works, in a TestClass inheriting from PageTest.
Isn't the page already created when entering this method, given that actions are performed on it?

[TestInitialize]
public async Task InitializeTest()
{
    await RegisterCustomSelectors(); // register here
    await Page.GotoAsync("/");
    await Page.Locator("myengine=test").FillAsync("hi"); // this works!
}
@Exoow
Copy link
Contributor Author

Exoow commented Nov 9, 2023

I found the caveat though: when running more than 1 test, Playwright throws an exception because the selector is already registered...

@mxschmitt
Copy link
Member

Yes, there is currently no great way of adding a custom selector engine in .NET. TestInitialize is too late.

What kind of selector engine are you creating? We learned that for most users the integrated selector engines are enough.

@Exoow
Copy link
Contributor Author

Exoow commented Nov 13, 2023

In TestInitialize works after adding a try/catch so it won't fail from the second test onwards.

We have tests running against Microsoft Dynamics, and it's easiest to select components by data-dyn-role or data-dyn-controlname attributes.
So instead of css=[data-dyn-controlname=something] I can write ddc=something.

An alternative could be to write extension methods on IPage and ILocator for this, but I like the brevity of the custom selectors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants