Skip to content

Commit cac3ae6

Browse files
Added command to validate form footer buttons
1 parent 672f387 commit cac3ae6

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

cypress/support/commands/command_constants.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,11 @@ export const FIELD_TYPES = {
2525
TEXTAREA: 'textarea',
2626
};
2727

28+
// Form button configuration keys
29+
export const BUTTON_CONFIG_KEYS = {
30+
BUTTON_TEXT: 'buttonText',
31+
BUTTON_TYPE: 'buttonType',
32+
SHOULD_BE_DISABLED: 'shouldBeDisabled',
33+
};
34+
2835
/* ========================================================== */

cypress/support/commands/form_elements_validation_commands.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
LABEL_CONFIG_KEYS,
44
FIELD_CONFIG_KEYS,
55
FIELD_TYPES,
6+
BUTTON_CONFIG_KEYS
67
} from './command_constants.js';
78

89
/**
@@ -187,3 +188,65 @@ Cypress.Commands.add('validateFormFields', (fieldConfigs) => {
187188
}
188189
});
189190
});
191+
192+
/**
193+
* Validates form buttons based on provided configurations
194+
*
195+
* @param {Array} buttonConfigs - Array of button configuration objects
196+
* @param {string} buttonConfigs[].buttonText - The text of the button
197+
* @param {string} [buttonConfigs[].buttonType='button'] - The type of button (e.g., 'submit', 'reset')
198+
* @param {boolean} [buttonConfigs[].shouldBeDisabled=false] - Whether the button should be disabled
199+
*
200+
* Example:
201+
* cy.validateFormFooterButtons([
202+
* { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Cancel' },
203+
* { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Reset', [BUTTON_CONFIG_KEYS.SHOULD_BE_DISABLED]: true },
204+
* { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Submit', [BUTTON_CONFIG_KEYS.BUTTON_TYPE]: 'submit' }
205+
* ]);
206+
*
207+
* Or using regular object keys:
208+
* cy.validateFormFooterButtons([
209+
* { buttonText: 'Cancel' },
210+
* { buttonText: 'Reset', shouldBeDisabled: true },
211+
* { buttonText: 'Submit', buttonType: 'submit' }
212+
* ]);
213+
*
214+
* Both approaches work but using config-keys object(BUTTON_CONFIG_KEYS) is recommended to avoid typos and unknown keys
215+
*/
216+
Cypress.Commands.add('validateFormFooterButtons', (buttonConfigs) => {
217+
if (!Array.isArray(buttonConfigs)) {
218+
cy.logAndThrowError('buttonConfigs must be an array');
219+
}
220+
221+
if (!buttonConfigs.length) {
222+
cy.logAndThrowError('buttonConfigs array cannot be empty');
223+
}
224+
225+
buttonConfigs.forEach((config) => {
226+
validateConfigKeys(config, BUTTON_CONFIG_KEYS, 'button');
227+
228+
const buttonText = config[BUTTON_CONFIG_KEYS.BUTTON_TEXT];
229+
const buttonType = config[BUTTON_CONFIG_KEYS.BUTTON_TYPE] || 'button';
230+
const shouldBeDisabled =
231+
config[BUTTON_CONFIG_KEYS.SHOULD_BE_DISABLED] || false;
232+
233+
if (!buttonText) {
234+
cy.logAndThrowError(
235+
`${BUTTON_CONFIG_KEYS.BUTTON_TEXT} is required for each button config`
236+
);
237+
}
238+
239+
const buttonCheck = cy
240+
.getFormFooterButtonByTypeWithText({
241+
buttonText,
242+
buttonType,
243+
})
244+
.should('be.visible');
245+
246+
if (shouldBeDisabled) {
247+
buttonCheck.and('be.disabled');
248+
} else {
249+
buttonCheck.and('be.enabled');
250+
}
251+
});
252+
});

0 commit comments

Comments
 (0)