Search code examples
selenium-webdriverplaywright

How to use custom test-id in Playwright?


In the HTML under test I have added my own locators for testing purposes, for example.

<input type="email" inputmode="email" data-pid="UserString" name="UserString" aria-label="User Email" />

And data-pid="UserString" being the locator in use, so in my selenium test suite we have this.

protected By UserString = By.CssSelector("[data-pid='UserString']");

And this

FillTextBox(UserString, "tester@tester.com");

This all works very nicely in C#/Selenium but I am not sure how to implement such a strategy in PLaywright. I can see the standard locator methods such as getByText, name, id etc.

Is this possible and if yes can someone please tell me how.


Solution

  • Thanks to @AutomationAndy for his suggestion. That was spot on. So basicly what I now have is this

    private static ILocator UserString => Page.Locator("[data-pid='UserString']");
    

    And this is called thus

    await UserString.FillAsync("tester@testing.com");
    

    Works a dream