|
3 | 3 | LABEL_CONFIG_KEYS, |
4 | 4 | FIELD_CONFIG_KEYS, |
5 | 5 | FIELD_TYPES, |
| 6 | + BUTTON_CONFIG_KEYS |
6 | 7 | } from './command_constants.js'; |
7 | 8 |
|
8 | 9 | /** |
@@ -164,3 +165,63 @@ Cypress.Commands.add('validateFormFields', (fieldConfigs) => { |
164 | 165 | } |
165 | 166 | }); |
166 | 167 | }); |
| 168 | + |
| 169 | +/** |
| 170 | + * Validates form buttons based on provided configurations |
| 171 | + * |
| 172 | + * @param {Array} buttonConfigs - Array of button configuration objects |
| 173 | + * @param {string} buttonConfigs[].buttonText - The text of the button |
| 174 | + * @param {string} [buttonConfigs[].buttonType='button'] - The type of button (e.g., 'submit', 'reset') |
| 175 | + * @param {boolean} [buttonConfigs[].shouldBeDisabled=false] - Whether the button should be disabled |
| 176 | + * |
| 177 | + * Example: |
| 178 | + * cy.validateFormFooterButtons([ |
| 179 | + * { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Cancel' }, |
| 180 | + * { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Reset', [BUTTON_CONFIG_KEYS.SHOULD_BE_DISABLED]: true }, |
| 181 | + * { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Submit', [BUTTON_CONFIG_KEYS.BUTTON_TYPE]: 'submit' } |
| 182 | + * ]); |
| 183 | + * |
| 184 | + * Or using regular object keys: |
| 185 | + * cy.validateFormFooterButtons([ |
| 186 | + * { buttonText: 'Cancel' }, |
| 187 | + * { buttonText: 'Reset', shouldBeDisabled: true }, |
| 188 | + * { buttonText: 'Submit', buttonType: 'submit' } |
| 189 | + * ]); |
| 190 | + * |
| 191 | + * Both approaches work but using config-keys object(BUTTON_CONFIG_KEYS) is recommended to avoid typos and unknown keys |
| 192 | + */ |
| 193 | +Cypress.Commands.add('validateFormFooterButtons', (buttonConfigs) => { |
| 194 | + if (!Array.isArray(buttonConfigs)) { |
| 195 | + cy.logAndThrowError('buttonConfigs must be an array'); |
| 196 | + } |
| 197 | + |
| 198 | + if (!buttonConfigs.length) { |
| 199 | + cy.logAndThrowError('buttonConfigs array cannot be empty'); |
| 200 | + } |
| 201 | + |
| 202 | + buttonConfigs.forEach((config) => { |
| 203 | + const buttonText = config[BUTTON_CONFIG_KEYS.BUTTON_TEXT]; |
| 204 | + const buttonType = config[BUTTON_CONFIG_KEYS.BUTTON_TYPE] || 'button'; |
| 205 | + const shouldBeDisabled = |
| 206 | + config[BUTTON_CONFIG_KEYS.SHOULD_BE_DISABLED] || false; |
| 207 | + |
| 208 | + if (!buttonText) { |
| 209 | + cy.logAndThrowError( |
| 210 | + `${BUTTON_CONFIG_KEYS.BUTTON_TEXT} is required for each button config` |
| 211 | + ); |
| 212 | + } |
| 213 | + |
| 214 | + const buttonCheck = cy |
| 215 | + .getFormFooterButtonByTypeWithText({ |
| 216 | + buttonText, |
| 217 | + buttonType, |
| 218 | + }) |
| 219 | + .should('be.visible'); |
| 220 | + |
| 221 | + if (shouldBeDisabled) { |
| 222 | + buttonCheck.and('be.disabled'); |
| 223 | + } else { |
| 224 | + buttonCheck.and('be.enabled'); |
| 225 | + } |
| 226 | + }); |
| 227 | +}); |
0 commit comments