Skip to content

Commit 46c2d87

Browse files
Added command to validate form footer buttons
1 parent dba9a58 commit 46c2d87

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-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: 61 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
/**
@@ -164,3 +165,63 @@ Cypress.Commands.add('validateFormFields', (fieldConfigs) => {
164165
}
165166
});
166167
});
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

Comments
 (0)