|
| 1 | +--- |
| 2 | +title: 'Allowed Interaction Commands | Cypress UI Coverage' |
| 3 | +description: 'The `allowedInteractionCommands` configuration property allows users to limit the interaction commands that are tracked for specific elements in UI Coverage.' |
| 4 | +sidebar_label: allowedInteractionCommands |
| 5 | +sidebar_position: 100 |
| 6 | +--- |
| 7 | + |
| 8 | +<ProductHeading product="ui-coverage" /> |
| 9 | + |
| 10 | +# allowedInteractionCommands |
| 11 | + |
| 12 | +UI Coverage tracks all interaction commands by default for comprehensive coverage reporting. The `allowedInteractionCommands` configuration allows you to limit which interaction commands are tracked for specific elements by defining rules based on CSS selectors. |
| 13 | + |
| 14 | +This is particularly useful for filtering out irrelevant interactions or focusing coverage tracking on specific interaction patterns for different types of elements. |
| 15 | + |
| 16 | +## Why use allowedInteractionCommands? |
| 17 | + |
| 18 | +- **Focused tracking**: Limit coverage tracking to relevant interaction types for specific elements or components to reduce noise in reports. |
| 19 | +- **Component-specific rules**: Apply different interaction tracking rules based on element types or component categories. For example, you may require specific kinds of interactions on complex data-visualization components that are not required in regular interactive elements. |
| 20 | + |
| 21 | +## Syntax |
| 22 | + |
| 23 | +```json |
| 24 | +{ |
| 25 | + "uiCoverage": { |
| 26 | + "allowedInteractionCommands": [ |
| 27 | + { |
| 28 | + "selector": string, |
| 29 | + "commands": [string] |
| 30 | + } |
| 31 | + ] |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### Options |
| 37 | + |
| 38 | +The `allowedInteractionCommands` property accepts an array of objects, where each object defines a rule for limiting interaction commands for elements matching a specific selector. |
| 39 | + |
| 40 | +| Option | Required | Default | Description | |
| 41 | +| ---------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 42 | +| `selector` | Required | | A CSS selector to identify elements. Supports standard CSS selector syntax, including IDs, classes, attributes, and combinators. | |
| 43 | +| `commands` | Required | | An array of command names (strings) that should be tracked as interactions for elements matching the selector. All other interaction commands will be ignored for these elements. | |
| 44 | + |
| 45 | +## Examples |
| 46 | + |
| 47 | +### Limiting button interactions to clicks only |
| 48 | + |
| 49 | +#### Config |
| 50 | + |
| 51 | +```json |
| 52 | +{ |
| 53 | + "uiCoverage": { |
| 54 | + "allowedInteractionCommands": [ |
| 55 | + { |
| 56 | + "selector": "button, [role='button']", |
| 57 | + "commands": ["click", "realClick"] |
| 58 | + } |
| 59 | + ] |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +#### Usage in tests |
| 65 | + |
| 66 | +```javascript |
| 67 | +// Only click and realClick will be tracked for buttons |
| 68 | +cy.get('[data-cy="submit-button"]').click() // ✓ Tracked |
| 69 | +cy.get('[data-cy="submit-button"]').realClick() // ✓ Tracked |
| 70 | +cy.get('[data-cy="submit-button"]').hover() // ✗ Not tracked |
| 71 | +cy.get('[data-cy="submit-button"]').focus() // ✗ Not tracked |
| 72 | +``` |
| 73 | + |
| 74 | +### Different rules for form elements |
| 75 | + |
| 76 | +#### Config |
| 77 | + |
| 78 | +```json |
| 79 | +{ |
| 80 | + "uiCoverage": { |
| 81 | + "allowedInteractionCommands": [ |
| 82 | + { |
| 83 | + "selector": "input[type='text'], textarea", |
| 84 | + "commands": ["type", "clear", "realType"] |
| 85 | + }, |
| 86 | + { |
| 87 | + "selector": "select", |
| 88 | + "commands": ["select", "click"] |
| 89 | + }, |
| 90 | + { |
| 91 | + "selector": "input[type='checkbox'], input[type='radio']", |
| 92 | + "commands": ["check", "uncheck", "click"] |
| 93 | + } |
| 94 | + ] |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +#### Usage in tests |
| 100 | + |
| 101 | +```javascript |
| 102 | +// Text inputs: only type, clear, and realType are tracked |
| 103 | +cy.get('[data-cy="username"]').type('john_doe') // ✓ Tracked |
| 104 | +cy.get('[data-cy="username"]').clear() // ✓ Tracked |
| 105 | +cy.get('[data-cy="username"]').focus() // ✗ Not tracked |
| 106 | + |
| 107 | +// Select elements: only select and click are tracked |
| 108 | +cy.get('[data-cy="country"]').select('US') // ✓ Tracked |
| 109 | +cy.get('[data-cy="country"]').click() // ✓ Tracked |
| 110 | +cy.get('[data-cy="country"]').hover() // ✗ Not tracked |
| 111 | + |
| 112 | +// Checkboxes/Radio buttons: check, uncheck, and click are tracked |
| 113 | +cy.get('[data-cy="agree-terms"]').check() // ✓ Tracked |
| 114 | +cy.get('[data-cy="agree-terms"]').click() // ✓ Tracked |
| 115 | +``` |
| 116 | + |
| 117 | +### Excluding hover interactions for mobile components |
| 118 | + |
| 119 | +#### Config |
| 120 | + |
| 121 | +```json |
| 122 | +{ |
| 123 | + "uiCoverage": { |
| 124 | + "allowedInteractionCommands": [ |
| 125 | + { |
| 126 | + "selector": "[data-mobile='true']", |
| 127 | + "commands": ["click", "tap", "swipe", "type"] |
| 128 | + } |
| 129 | + ] |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +#### Usage in tests |
| 135 | + |
| 136 | +```javascript |
| 137 | +// Mobile components: hover interactions are excluded |
| 138 | +cy.get('[data-mobile="true"][data-cy="menu-item"]').click() // ✓ Tracked |
| 139 | +cy.get('[data-mobile="true"][data-cy="menu-item"]').tap() // ✓ Tracked |
| 140 | +cy.get('[data-mobile="true"][data-cy="menu-item"]').hover() // ✗ Not tracked |
| 141 | +``` |
| 142 | + |
| 143 | +### Allowing assertions as interactions |
| 144 | + |
| 145 | +Note that because `allowedInteractionCommands` replaces the allowed interactions, if you add `assert` as an interaction, remember to also add any other acceptable interactions to the list. |
| 146 | + |
| 147 | +#### Config |
| 148 | + |
| 149 | +```json |
| 150 | +{ |
| 151 | + "uiCoverage": { |
| 152 | + "additionalInteractionCommands": [ |
| 153 | + { |
| 154 | + "selector": "button[data-no-explicit-interaction='true']", |
| 155 | + "commands": ["assert"] |
| 156 | + } |
| 157 | + ] |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +#### Usage in tests |
| 163 | + |
| 164 | +```javascript |
| 165 | +// Any assertions on matching elements are tracked |
| 166 | +cy.get('button[data-no-explicit-interaction="true"]').should('exist) // ✓ Tracked |
| 167 | +cy.get('button[data-no-explicit-interaction="true"]').should('be.visible) // ✓ Tracked |
| 168 | +cy.get('button[data-no-explicit-interaction="true"]').click() // ✗ Not tracked |
| 169 | +``` |
| 170 | + |
| 171 | +## Notes |
| 172 | + |
| 173 | +- Elements that don't match any selector will have all interaction commands tracked (default behavior). |
| 174 | +- If an element matches multiple selectors, commands from all matching rules will be allowed. A high degree of specificity for these selectors is recommended. |
| 175 | +- Command names are case-sensitive and must match exactly as they appear in your test code. |
| 176 | +- This configuration works separately from `additionalInteractionCommands`. Custom commands do **not** need to be globally defined as `additionalInteractionCommands` in order to be declared here. |
0 commit comments