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

Suggestion: For web UI, prefer data-test-id attributes #39

Open
yairEO opened this issue May 29, 2024 · 0 comments
Open

Suggestion: For web UI, prefer data-test-id attributes #39

yairEO opened this issue May 29, 2024 · 0 comments

Comments

@yairEO
Copy link

yairEO commented May 29, 2024

Example (overly simplified) login page HTML:

<input data-test-id='username' placeholder='username'/>
<input data-test-id='password' type='password' placeholder='password'/>

playwright.config.ts

use: {
  screenshot: "only-on-failure",

  /* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
  actionTimeout: 0,

  /* Base URL to use in actions like `await page.goto('/')`. */
  baseURL: process.env.TESTS_URL,

  /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
  trace: "on-first-retry",

  browserName: "chromium",
  headless: true,

  testIdAttribute: "data-test-id",    // <---------
},

tests/login.ts

Prefer page.getByTestId over unstable Locator (because DOM-based selectors might change & break tests)

this.username = page.getByTestId("username");
this.password = page.getByTestId("passpord");

References:

https://playwright.dev/docs/locators#set-a-custom-test-id-attribute
https://playwrightsolutions.com/getbytestid/

@yairEO yairEO changed the title Suggestion - For web UI, prefer data-test-id attributes Suggestion: For web UI, prefer data-test-id attributes May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant