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

Enforce single data-test-subj #208260

Open
nickofthyme opened this issue Jan 24, 2025 · 1 comment
Open

Enforce single data-test-subj #208260

nickofthyme opened this issue Jan 24, 2025 · 1 comment
Assignees
Labels
Team:Operations Team label for Operations Team

Comments

@nickofthyme
Copy link
Contributor

nickofthyme commented Jan 24, 2025

The idea of a data-test-subj it for precise element targeting in complex UIs. This should function much like the id attribute, meaning that it only has a single value (i.e. no spaces) and excluding the uniqueness requirement. There should be no reason to include modifiers in the data-test-subj for the same element in order to change its behavior. Changing behavior should be done with an additional attribute to modify the behavior.

Case in point, the example below uses the test subject of kbnAppWrapper [modifier] even though both visibleChrome and hiddenChrome, when visible, will point to the same DOM element.

test.describe('Maps', () => {
  test('Full screen mode', async ({ page }) => {
    // ...
    const visibleChrome = page.getByTestId('kbnAppWrapper visibleChrome');
    const hiddenChrome = page.getByTestId('kbnAppWrapper hiddenChrome');

    await expect(visibleChrome).toBeVisible();
    await expect(hiddenChrome).not.toBeVisible();
  });
});

Instead, the more canonical approach would be to get the element and test if it has the correct attributes.

test.describe('Maps', () => {
  test('Full screen mode', async ({ page }) => {
    // ...
    const kbnAppWrapper = page.getByTestId('kbnAppWrapper');

    await expect(kbnAppWrapper).toBeVisible();
    await expect(kbnAppWrapper).toHaveAttribute('chrome', 'visible');
  });
});

Given this I see no good reason to allow multiple test subject ids on the same element. For example...

<div data-test-subj="foo bar">Desired element</div>

I'm open to other reasons why multiple test ids would be appropriate but I cannot think of any.

@botelastic botelastic bot added the needs-team Issues missing a team label label Jan 24, 2025
@nickofthyme nickofthyme self-assigned this Jan 24, 2025
@nickofthyme nickofthyme added the Team:Operations Team label for Operations Team label Jan 24, 2025
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-operations (Team:Operations)

@botelastic botelastic bot removed the needs-team Issues missing a team label label Jan 24, 2025
@nickofthyme nickofthyme changed the title Enforce single test subject Enforce single data-test-subj Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team:Operations Team label for Operations Team
Projects
None yet
Development

No branches or pull requests

2 participants