elementGroups
UI Coverage provides logic to automatically group elements based on their structure in the DOM. However, there are scenarios where you may want to customize these groups to better align with your application's functionality or testing requirements. The elementGroups
configuration allows you to define custom logic for grouping elements, improving coverage clarity and simplifying analysis.
Why use element groups?â
- Improve Grouping Accuracy: Ensure elements with shared attributes but different roles are correctly grouped, avoiding misclassification.
- Simplify Test Coverage Reports: Grouping similar elements, like navigation buttons or list items, reduces clutter in reports and provides a more concise view of test coverage.
- Highlight Key Areas: Use meaningful group names to draw attention to critical application areas, such as form controls.
- Streamline Dynamic Elements: Consolidate dynamic or repeated elements, such as items in a carousel or list, into a single logical group.
Syntaxâ
{
"uiCoverage": {
"elementGroups": [
{
"selector": string,
"name": string
}
]
}
}
Optionsâ
For every element considered by UI Coverage, the first elementGroup
rule for which the selector
property matches the element is used to group the element. Elements that do not match any rules are grouped by the default UI Coverage element grouping rules.
Option | Required | Default | Description |
---|---|---|---|
selector | Required | A CSS selector to identify elements. Supports standard CSS selector syntax, including IDs, classes, attributes, and combinators. | |
name | Optional | selector | A human-readable name for the group, displayed in UI Coverage reports. |
Examplesâ
Groups elements by selectorâ
Configâ
{
"uiCoverage": {
"elementGroups": [
{
"selector": "[data-cy^='item-']"
}
]
}
}
HTMLâ
<body>
<button data-cy="item-1"></button> <!-- Group: [data-cy^='item-'] -->
<button data-cy="item-2"></button> <!-- Group: [data-cy^='item-'] -->
<button data-cy="item-3"></button> <!-- Group: [data-cy^='item-'] -->
</body>
Elements shown in UIâ
[data-cy^='item-'] (3 instances)
Groups all elements in a containerâ
Configâ
{
"uiCoverage": {
"elementGroups": [
{
"selector": "#calendar button"
}
]
}
}
HTMLâ
<body>
<div id="calendar">
<button id="jan"></button>
<button id="feb"></button>
<button id="mar"></button>
</div>
</body>
Elements shown in UIâ
#calendar button (3 instances)
Groups form controls with labelsâ
Configâ
{
"uiCoverage": {
"elementGroups": [
{
"selector": "input[name='animal'], label:has(input[name='animal'])",
"name": "Animal Option"
}
]
}
}
HTMLâ
<body>
<label>
<input id="bear" name="animal"></input>
</label>
<label>
<input id="lion" name="animal"></input>
</label>
</body>
Elements shown in UIâ
Animal Option (4 instances)
Groups dynamic elementsâ
Configâ
{
"uiCoverage": {
"elementGroups": [
{
"selector": "[id^='product']"
}
]
}
}
HTMLâ
<body>
<button id="product125">Product 1</button>
<button id="product514">Product 2</button>
<button id="product256">Product 3</button>
</body>
Elements shown in UIâ
[id^='product'] (3 instances)
Giving groups custom namesâ
Sometimes you may want to group elements by a common attribute but give the group a more descriptive name. In the following example, we group buttons with IDs starting with listbox-button-
and name the group Add Button
.
Configâ
{
"uiCoverage": {
"elementGroups": [
{
"selector": "[id^='listbox-button-']",
"name": "Add Button"
}
]
}
}
HTMLâ
<body>
<button id='listbox-button-1'>+</button>
<button id='listbox-button-2'>+</button>
<button id='listbox-button-3'>+</button>
<button id='listbox-button-4'>+</button>
</body>
Elements shown in UIâ
Add Button (4 instances)