Julian
Julian

Reputation: 36740

Custom selector in PlayWright

Is it possible to add a custom selector in Playwright? (preferabele in C#)

For example, our application uses the custom attribute [data-automation-name=SOMENAME] in the HTML and it's a bit cumbersome to write every time:

page.FillAsync("[data-automation-name=SOMENAME1]", "1");
page.FillAsync("[data-automation-name=SOMENAME2]", "2");
page.FillAsync("[data-automation-name=SOMENAME3]", "3");

I prefer to write something like this:

page.FillAsync("$SOMENAME1", "1");
page.FillAsync("$SOMENAME2", "2");
page.FillAsync("$SOMENAME3", "3");

I searched the docs and GitHub repos without success.

Upvotes: 1

Views: 1348

Answers (1)

Xen0byte
Xen0byte

Reputation: 109

What I would do is ...

private async Task FillNameAsync(string name, string value)
    => await page.FillAsync($"[data-automation-name={name}]", value);

await FillNameAsync("SOMENAME1", "1");
await FillNameAsync("SOMENAME2", "2");
await FillNameAsync("SOMENAME3", "3");

Upvotes: 1

Related Questions