|
| 1 | +// TODO: Use aliased import(@cypress-dir) once #9631 is merged |
| 2 | +import { |
| 3 | + LABEL_CONFIG_KEYS, |
| 4 | + FIELD_CONFIG_KEYS, |
| 5 | + FIELD_TYPES, |
| 6 | + BUTTON_CONFIG_KEYS, |
| 7 | +} from './constants/command_constants.js'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Helper function to validate that config objects only contain valid keys |
| 11 | + * |
| 12 | + * @param {Object} config - The configuration object to validate |
| 13 | + * @param {Object} validKeysObject - The object containing valid keys (e.g., LABEL_CONFIG_KEYS) |
| 14 | + * @param {string} configType - The type of configuration being validated (for error messages) |
| 15 | + */ |
| 16 | +const validateConfigKeys = (config, validKeysObject, configType) => { |
| 17 | + const validKeys = Object.values(validKeysObject); |
| 18 | + |
| 19 | + Object.keys(config).forEach((key) => { |
| 20 | + if (!validKeys.includes(key)) { |
| 21 | + cy.logAndThrowError( |
| 22 | + `Unknown key "${key}" in ${configType} config. Valid keys are: ${validKeys.join( |
| 23 | + ', ' |
| 24 | + )}` |
| 25 | + ); |
| 26 | + } |
| 27 | + }); |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * Validates form field labels based on provided configurations |
| 32 | + * |
| 33 | + * @param {Array} labelConfigs - Array of label configuration objects |
| 34 | + * @param {string} labelConfigs[].forValue - The 'for' attribute value of the label |
| 35 | + * @param {string} [labelConfigs[].expectedText] - The expected text content of the label |
| 36 | + * |
| 37 | + * Example: |
| 38 | + * cy.validateFormLabels([ |
| 39 | + * { [LABEL_CONFIG_KEYS.FOR_VALUE]: 'name', [LABEL_CONFIG_KEYS.EXPECTED_TEXT]: 'Name' }, |
| 40 | + * { [LABEL_CONFIG_KEYS.FOR_VALUE]: 'email', [LABEL_CONFIG_KEYS.EXPECTED_TEXT]: 'Email Address' } |
| 41 | + * ]); |
| 42 | + * |
| 43 | + * Or using regular object keys: |
| 44 | + * cy.validateFormLabels([ |
| 45 | + * { forValue: 'name', expectedText: 'Name' }, |
| 46 | + * { forValue: 'email', expectedText: 'Email Address' } |
| 47 | + * ]); |
| 48 | + * |
| 49 | + * Both approaches work but using config-keys object(LABEL_CONFIG_KEYS) is recommended to avoid typos and unknown keys |
| 50 | + */ |
| 51 | +Cypress.Commands.add('validateFormLabels', (labelConfigs) => { |
| 52 | + if (!Array.isArray(labelConfigs)) { |
| 53 | + cy.logAndThrowError('labelConfigs must be an array'); |
| 54 | + } |
| 55 | + |
| 56 | + if (!labelConfigs.length) { |
| 57 | + cy.logAndThrowError('labelConfigs array cannot be empty'); |
| 58 | + } |
| 59 | + |
| 60 | + labelConfigs.forEach((config) => { |
| 61 | + validateConfigKeys(config, LABEL_CONFIG_KEYS, 'label'); |
| 62 | + |
| 63 | + const forValue = config[LABEL_CONFIG_KEYS.FOR_VALUE]; |
| 64 | + const expectedText = config[LABEL_CONFIG_KEYS.EXPECTED_TEXT]; |
| 65 | + |
| 66 | + if (!forValue) { |
| 67 | + cy.logAndThrowError( |
| 68 | + `${LABEL_CONFIG_KEYS.FOR_VALUE} is required for each label config` |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + const labelCheck = cy |
| 73 | + .getFormLabelByForAttribute({ forValue }) |
| 74 | + .should('be.visible'); |
| 75 | + |
| 76 | + if (expectedText) { |
| 77 | + labelCheck.and('contain.text', expectedText); |
| 78 | + } |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +/** |
| 83 | + * Validates form input fields based on provided configurations |
| 84 | + * |
| 85 | + * @param {Array} fieldConfigs - Array of field configuration objects |
| 86 | + * @param {string} fieldConfigs[].id - The ID of the form field |
| 87 | + * @param {string} [fieldConfigs[].fieldType='input'] - The type of field ('input', 'select', 'textarea') |
| 88 | + * @param {string} [fieldConfigs[].inputFieldType='text'] - The type of input field ('text', 'password', 'number') |
| 89 | + * @param {boolean} [fieldConfigs[].shouldBeDisabled=false] - Whether the field should be disabled |
| 90 | + * @param {string} [fieldConfigs[].expectedValue] - The expected value of the field |
| 91 | + * |
| 92 | + * Example: |
| 93 | + * cy.validateFormFields([ |
| 94 | + * { [FIELD_CONFIG_KEYS.ID]: 'name', [FIELD_CONFIG_KEYS.SHOULD_BE_DISABLED]: true }, |
| 95 | + * { [FIELD_CONFIG_KEYS.ID]: 'email', [FIELD_CONFIG_KEYS.INPUT_FIELD_TYPE]: 'email' }, |
| 96 | + * { |
| 97 | + * [FIELD_CONFIG_KEYS.ID]: 'role', |
| 98 | + * [FIELD_CONFIG_KEYS.FIELD_TYPE]: FIELD_TYPES.SELECT, |
| 99 | + * [FIELD_CONFIG_KEYS.EXPECTED_VALUE]: 'admin' |
| 100 | + * } |
| 101 | + * ]); |
| 102 | + * |
| 103 | + * Or using regular object keys: |
| 104 | + * cy.validateFormFields([ |
| 105 | + * { id: 'name', shouldBeDisabled: true }, |
| 106 | + * { id: 'email' }, |
| 107 | + * { id: 'role', fieldType: 'select', expectedValue: 'admin' } |
| 108 | + * ]); |
| 109 | + * |
| 110 | + * Both approaches work but using config-keys object(FIELD_CONFIG_KEYS) is recommended to avoid typos and unknown keys |
| 111 | + */ |
| 112 | +Cypress.Commands.add('validateFormFields', (fieldConfigs) => { |
| 113 | + if (!Array.isArray(fieldConfigs)) { |
| 114 | + cy.logAndThrowError('fieldConfigs must be an array'); |
| 115 | + } |
| 116 | + |
| 117 | + if (!fieldConfigs.length) { |
| 118 | + cy.logAndThrowError('fieldConfigs array cannot be empty'); |
| 119 | + } |
| 120 | + |
| 121 | + fieldConfigs.forEach((config) => { |
| 122 | + validateConfigKeys(config, FIELD_CONFIG_KEYS, 'field'); |
| 123 | + |
| 124 | + const id = config[FIELD_CONFIG_KEYS.ID]; |
| 125 | + const fieldType = config[FIELD_CONFIG_KEYS.FIELD_TYPE] || FIELD_TYPES.INPUT; |
| 126 | + const inputFieldType = config[FIELD_CONFIG_KEYS.INPUT_FIELD_TYPE] || 'text'; |
| 127 | + const shouldBeDisabled = |
| 128 | + config[FIELD_CONFIG_KEYS.SHOULD_BE_DISABLED] || false; |
| 129 | + const expectedValue = config[FIELD_CONFIG_KEYS.EXPECTED_VALUE]; |
| 130 | + |
| 131 | + if (!id) { |
| 132 | + cy.logAndThrowError( |
| 133 | + `${FIELD_CONFIG_KEYS.ID} is required for each field config` |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + // Check field based on type |
| 138 | + switch (fieldType) { |
| 139 | + case FIELD_TYPES.INPUT: |
| 140 | + cy.getFormInputFieldByIdAndType({ |
| 141 | + inputId: id, |
| 142 | + inputType: inputFieldType, |
| 143 | + }) |
| 144 | + .should('be.visible') |
| 145 | + .then((field) => { |
| 146 | + if (shouldBeDisabled) { |
| 147 | + expect(field).to.be.disabled; |
| 148 | + } else { |
| 149 | + expect(field).to.not.be.disabled; |
| 150 | + } |
| 151 | + |
| 152 | + if (expectedValue) { |
| 153 | + cy.wrap(field).should('have.value', expectedValue); |
| 154 | + } |
| 155 | + }); |
| 156 | + break; |
| 157 | + case FIELD_TYPES.SELECT: |
| 158 | + cy.getFormSelectFieldById({ selectId: id }) |
| 159 | + .should('be.visible') |
| 160 | + .then((field) => { |
| 161 | + if (shouldBeDisabled) { |
| 162 | + expect(field).to.be.disabled; |
| 163 | + } else { |
| 164 | + expect(field).to.not.be.disabled; |
| 165 | + } |
| 166 | + |
| 167 | + if (expectedValue) { |
| 168 | + cy.wrap(field).should('have.value', expectedValue); |
| 169 | + } |
| 170 | + }); |
| 171 | + break; |
| 172 | + case FIELD_TYPES.TEXTAREA: |
| 173 | + cy.getFormTextareaById({ textareaId: id }) |
| 174 | + .should('be.visible') |
| 175 | + .then((field) => { |
| 176 | + if (shouldBeDisabled) { |
| 177 | + expect(field).to.be.disabled; |
| 178 | + } else { |
| 179 | + expect(field).to.not.be.disabled; |
| 180 | + } |
| 181 | + |
| 182 | + if (expectedValue) { |
| 183 | + cy.wrap(field).should('have.value', expectedValue); |
| 184 | + } |
| 185 | + }); |
| 186 | + break; |
| 187 | + |
| 188 | + default: |
| 189 | + cy.logAndThrowError(`Unsupported field type: ${fieldType}`); |
| 190 | + } |
| 191 | + }); |
| 192 | +}); |
| 193 | + |
| 194 | +/** |
| 195 | + * Validates form buttons based on provided configurations |
| 196 | + * |
| 197 | + * @param {Array} buttonConfigs - Array of button configuration objects |
| 198 | + * @param {string} buttonConfigs[].buttonText - The text of the button |
| 199 | + * @param {string} [buttonConfigs[].buttonType='button'] - The type of button (e.g., 'submit', 'reset') |
| 200 | + * @param {boolean} [buttonConfigs[].shouldBeDisabled=false] - Whether the button should be disabled |
| 201 | + * |
| 202 | + * Example: |
| 203 | + * cy.validateFormFooterButtons([ |
| 204 | + * { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Cancel' }, |
| 205 | + * { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Reset', [BUTTON_CONFIG_KEYS.SHOULD_BE_DISABLED]: true }, |
| 206 | + * { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Submit', [BUTTON_CONFIG_KEYS.BUTTON_TYPE]: 'submit' } |
| 207 | + * ]); |
| 208 | + * |
| 209 | + * Or using regular object keys: |
| 210 | + * cy.validateFormFooterButtons([ |
| 211 | + * { buttonText: 'Cancel' }, |
| 212 | + * { buttonText: 'Reset', shouldBeDisabled: true }, |
| 213 | + * { buttonText: 'Submit', buttonType: 'submit' } |
| 214 | + * ]); |
| 215 | + * |
| 216 | + * Both approaches work but using config-keys object(BUTTON_CONFIG_KEYS) is recommended to avoid typos and unknown keys |
| 217 | + */ |
| 218 | +Cypress.Commands.add('validateFormFooterButtons', (buttonConfigs) => { |
| 219 | + if (!Array.isArray(buttonConfigs)) { |
| 220 | + cy.logAndThrowError('buttonConfigs must be an array'); |
| 221 | + } |
| 222 | + |
| 223 | + if (!buttonConfigs.length) { |
| 224 | + cy.logAndThrowError('buttonConfigs array cannot be empty'); |
| 225 | + } |
| 226 | + |
| 227 | + buttonConfigs.forEach((config) => { |
| 228 | + validateConfigKeys(config, BUTTON_CONFIG_KEYS, 'button'); |
| 229 | + |
| 230 | + const buttonText = config[BUTTON_CONFIG_KEYS.BUTTON_TEXT]; |
| 231 | + const buttonType = config[BUTTON_CONFIG_KEYS.BUTTON_TYPE] || 'button'; |
| 232 | + const shouldBeDisabled = |
| 233 | + config[BUTTON_CONFIG_KEYS.SHOULD_BE_DISABLED] || false; |
| 234 | + |
| 235 | + if (!buttonText) { |
| 236 | + cy.logAndThrowError( |
| 237 | + `${BUTTON_CONFIG_KEYS.BUTTON_TEXT} is required for each button config` |
| 238 | + ); |
| 239 | + } |
| 240 | + |
| 241 | + const buttonCheck = cy |
| 242 | + .getFormFooterButtonByTypeWithText({ |
| 243 | + buttonText, |
| 244 | + buttonType, |
| 245 | + }) |
| 246 | + .should('be.visible'); |
| 247 | + |
| 248 | + if (shouldBeDisabled) { |
| 249 | + buttonCheck.and('be.disabled'); |
| 250 | + } else { |
| 251 | + buttonCheck.and('be.enabled'); |
| 252 | + } |
| 253 | + }); |
| 254 | +}); |
0 commit comments