CSS Selectors for Automated Tests · CSS-02
CSS selectors should be added by any developer contributing to front-end code, inline with the following standards.
Definition
CSS selectors are unique identifiers added to HTML elements, enabling Test Engineers to reliably target and interact with specific UI components, when doing automated UI testing.
CSS selectors should be added to facilitate UI automation · CSS-02.1 · SHOULD
Adding CSS selectors to UI elements is required for effective UI automation but need not exist on every element.
Input fields () including text input, checkboxes, radio buttons and date pickers often require CSS selectors to ensure stability.
CSS selectors should be added, to enable automated tests to consistently identify and interact with specific components. Without selectors, tests become fragile and may break when the page structure changes e.g. Andy is no longer shown as the first member of an alphabetized list of names.
CSS selectors must follow a consistent format · CSS-02.2 · MUST
- Use the
data-testattribute for selectors. - Use standardized wording throughout the selector, adding to the provided lists over time.
- Mirror user-visible wording where possible in selectors.
- Structure selectors in three parts (verb-selection-element), separated by double underscores (__).
- Write each part in kebab-case.
Part 1: The Component Verb
- Represent the Angular (or Vue, etc.) component using a verb.
- Add the
-modalsuffix for modal components only.
Common verbs like add-, view-, edit-, search-, or select- should be used, for example:
- view- in
view-sales-contract - edit- in
edit-sales-invoice
Part 2: The Section
- Employ standard section types e.g.,
-search-form,-input-form,-results-table. - Provide sufficient detail to distinguish similar page sections.
- Use the
-modalsuffix in the second part for components containing a modal.
Part 3: The Element
- Use meaningful suffixes to clarify the element type, preferring standard element types.
- Add selectors to table rows when targeting elements within the row.
- Avoid adding loop index numbers to repeating elements. Optionally include database IDs.
- Use the
-iconsuffix for icons, adding a clarifying word if multiple icons are present.
CSS selectors must use standardized element types · CSS-02.3 · MUST
Employ these standardized element types in the third part of the CSS selectors to ensure consistency and clarity across the codebase. These suffixes clearly indicate the type of UI element being targeted, reducing ambiguity and improving readability.
-header- A title at the top of a section-heading- A table heading-table-table-row- A less ambiguous way to document a-row-checkbox-radio-button-dropdown- A dropdown input i.e. the