liveBook · Manning

Data attribute

As explained in BDD in Action, Second Edition

An HTML data attribute is a custom data-* attribute for storing additional information on a standard HTML element.

A data attribute stores additional information on a standard HTML element without taking over the usual role of attributes such as id or name. For automated UI tests, a stable data-testid convention can identify elements independently of page layout and JavaScript or form implementation details. section 10.2.12

A test-oriented login form

Add dedicated identifiers to semantically important controls: inputs can use data-testid="email" and data-testid="password", while the submit button uses data-testid="login". WebDriver can then locate these controls through their data attributes instead of relying on layout, event-handling, or form-related attributes. section 10.2.12

Listing 10.1 Using a data attribute to identify a field
<div class="login-container">
    <form [formGroup]="form" (submit)="login(form)">
        <mat-form-field appearance="fill">
            <mat-label>Email</mat-label>
            <input id="email" data-testid="email">
        </mat-form-field>
        <br>
        <mat-form-field appearance="fill">
            <mat-label>Password</mat-label>
            <input id="password" data-testid="password">
        </mat-form-field>
        <br>    
        <button type="submit" data-testid="login”>Login</button>
    </form>
</div>

Inspect the complete login-form example showing data-testid values on fields and the submit button. section 10.2.12

How BDD in Action, Second Edition covers it

How deep it goes
Defines the purpose, compares locator stability, and demonstrates HTML usage, but does not develop data-attribute internals.
Examples
Uses a login form with data-testid values for email, password, and login controls.
What it assumes
Assumes control over the HTML source and familiarity with standard element attributes such as id and name.
What you can do after
Apply it via WebDriver by adding data-testid markers to HTML controls and using them as test locators.
How it connects
Connects HTML attributes with WebDriver, automated web testing, JavaScript frameworks, HTML forms, and element locators.

Also explained in

Compare how 2 books explain data attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of BDD in Action, Second Edition. The section links point to the parts of the book it draws on.