Skip to content

Commit 6ea6671

Browse files
authored
Merge pull request #9689 from asirvadAbrahamVarghese/enhance-form-element-selectors
Enhance form element selectors
2 parents 94e5eca + 3f7f0d2 commit 6ea6671

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

cypress/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ ManageIQ implements the following cypress extensions:
8282

8383
##### element_selectors
8484

85-
* `cy.getFormFooterButtonByTypeWithText({ buttonType, buttonText })` - retrieves form footer button by its name and type. `buttonText` is the name or text content of the button. `buttonType` is the HTML button type (e.g., 'button', 'submit', 'reset'). Defaults to 'button'. e.g. `cy.getFormFooterButtonByType({buttonType: 'Reset', buttonText: 'reset'});`, `cy.getFormFooterButtonByTypeWithText({buttonText: 'Cancel'});`
85+
* `cy.getFormFooterButtonByTypeWithText({ buttonType, buttonText, buttonWrapperClass })` - retrieves form footer button by its name and type. `buttonText` is the name or text content of the button. `buttonType` is the HTML button type (e.g., 'button', 'submit', 'reset'). Defaults to 'button'. `buttonWrapperClass` is the CSS class of the wrapper element containing the buttons. Defaults to 'bx--btn-set'. e.g. `cy.getFormFooterButtonByTypeWithText({buttonText: 'Cancel'});`, `cy.getFormFooterButtonByTypeWithText({buttonText: 'Submit', buttonType: 'submit'});`, `cy.getFormFooterButtonByTypeWithText({buttonText: 'Save', buttonWrapperClass: 'custom-wrapper'});`
8686
* `cy.getFormInputFieldByIdAndType({ inputId, inputType })` - retrieves a form input field by its ID and type. `inputId` is the ID of the input field. `inputType` is the HTML input type (e.g., 'text', 'email', 'password'). Defaults to 'text'. e.g. `cy.getFormInputFieldByIdAndType({inputId: 'name'});`, `cy.getFormInputFieldByIdAndType({inputId: 'name', inputType: 'text'});`
8787
* `cy.getFormLabelByForAttribute({ forValue })` - retrieves a form label associated with a specific input field by its 'for' attribute. `forValue` is the value of the 'for' attribute that matches the input field's ID. e.g. `cy.getFormLabelByForAttribute({forValue: 'name'});`
8888
* `cy.getFormLegendByText({ legendText })` - retrieves a form legend element by its text content. Legend elements are typically used as captions for fieldset elements in forms. `legendText` is the text content of the legend element. e.g. `cy.getFormLegendByText({legendText: 'Basic Information'});`
@@ -93,7 +93,7 @@ ManageIQ implements the following cypress extensions:
9393

9494
* `cy.validateFormLabels(labelConfigs)` - validates form field labels based on provided configurations. `labelConfigs` is an array of label configuration objects with properties: `forValue` (required) - the 'for' attribute value of the label, `expectedText` (optional) - the expected text content of the label. e.g. `cy.validateFormLabels([{ forValue: 'name', expectedText: 'Name' }, { forValue: 'email', expectedText: 'Email Address' }]);` or using constants: `cy.validateFormLabels([{ [LABEL_CONFIG_KEYS.FOR_VALUE]: 'name', [LABEL_CONFIG_KEYS.EXPECTED_TEXT]: 'Name' }]);`
9595
* `cy.validateFormFields(fieldConfigs)` - validates form input fields based on provided configurations. `fieldConfigs` is an array of field configuration objects with properties: `id` (required) - the ID of the form field, `fieldType` (optional, default: 'input') - the type of field ('input', 'select', 'textarea'), `inputFieldType` (optional, default: 'text') - the type of input field ('text', 'password', 'number'), `shouldBeDisabled` (optional, default: false) - whether the field should be disabled, `expectedValue` (optional) - the expected value of the field. e.g. `cy.validateFormFields([{ id: 'name', shouldBeDisabled: true }, { id: 'role', fieldType: 'select', expectedValue: 'admin' }]);` or using constants: `cy.validateFormFields([{ [FIELD_CONFIG_KEYS.ID]: 'email', [FIELD_CONFIG_KEYS.INPUT_FIELD_TYPE]: 'email' }, { [FIELD_CONFIG_KEYS.ID]: 'name', [FIELD_CONFIG_KEYS.SHOULD_BE_DISABLED]: true }]);`
96-
* `cy.validateFormFooterButtons(buttonConfigs)` - validates form buttons based on provided configurations. `buttonConfigs` is an array of button configuration objects with properties: `buttonText` (required) - the text of the button, `buttonType` (optional, default: 'button') - the type of button (e.g., 'submit', 'reset'), `shouldBeDisabled` (optional, default: false) - whether the button should be disabled. e.g. `cy.validateFormFooterButtons([{ buttonText: 'Cancel' }, { buttonText: 'Submit', buttonType: 'submit', shouldBeDisabled: true }]);` or using constants: `cy.validateFormFooterButtons([{ [BUTTON_CONFIG_KEYS.TEXT]: 'Cancel' }]);`
96+
* `cy.validateFormFooterButtons(buttonConfigs)` - validates form buttons based on provided configurations. `buttonConfigs` is an array of button configuration objects with properties: `buttonText` (required) - the text of the button, `buttonType` (optional, default: 'button') - the type of button (e.g., 'submit', 'reset'), `shouldBeDisabled` (optional, default: false) - whether the button should be disabled, `buttonWrapperClass` (optional, default: 'bx--btn-set') - the CSS class of the wrapper element containing the buttons. e.g. `cy.validateFormFooterButtons([{ buttonText: 'Cancel' }, { buttonText: 'Submit', buttonType: 'submit', shouldBeDisabled: true }]);`, `cy.validateFormFooterButtons([{ buttonText: 'Save', buttonWrapperClass: 'custom-wrapper' }]);` or using constants: `cy.validateFormFooterButtons([{ [BUTTON_CONFIG_KEYS.TEXT]: 'Cancel', [BUTTON_CONFIG_KEYS.BUTTON_WRAPPER_CLASS]: 'custom-button-wrapper' }]);`
9797

9898
#### Assertions
9999

cypress/support/commands/constants/command_constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const BUTTON_CONFIG_KEYS = {
3131
BUTTON_TEXT: 'buttonText',
3232
BUTTON_TYPE: 'buttonType',
3333
SHOULD_BE_DISABLED: 'shouldBeDisabled',
34+
BUTTON_WRAPPER_CLASS: 'buttonWrapperClass',
3435
};
3536

3637
/* ========================================================== */

cypress/support/commands/element_selectors.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
* @param {Object} options - The options object.
55
* @param {string} options.buttonText - The text content of the button (required).
66
* @param {string} [options.buttonType='button'] - The HTML button type (e.g., 'button', 'submit', 'reset'). Defaults to 'button'.
7+
* @param {string} [options.buttonWrapperClass='bx--btn-set'] - The CSS class of the wrapper element containing the buttons. Defaults to 'bx--btn-set'.
78
* @returns {Element} The matched button element.
8-
* @throws {Error} If buttonName is not provided.
9+
* @throws {Error} If buttonText is not provided.
910
*
1011
* Example:
1112
* cy.getFormFooterButtonByTypeWithText({ buttonText: 'Save Changes' });
1213
* cy.getFormFooterButtonByTypeWithText({ buttonText: 'Submit', buttonType: 'submit' });
14+
* cy.getFormFooterButtonByTypeWithText({ buttonText: 'Cancel', buttonWrapperClass: 'custom-wrapper' });
1315
*/
1416
Cypress.Commands.add(
1517
'getFormFooterButtonByTypeWithText',
16-
({ buttonType = 'button', buttonText } = {}) => {
18+
({ buttonType = 'button', buttonText, buttonWrapperClass = 'bx--btn-set' } = {}) => {
1719
if (!buttonText) {
1820
cy.logAndThrowError('buttonText is required');
1921
}
2022
return cy.contains(
21-
`#main-content .bx--btn-set button[type="${buttonType}"]`,
23+
`#main-content .${buttonWrapperClass} button[type="${buttonType}"]`,
2224
buttonText
2325
);
2426
}

cypress/support/commands/form_elements_validation_commands.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,22 @@ Cypress.Commands.add('validateFormFields', (fieldConfigs) => {
208208
* @param {string} buttonConfigs[].buttonText - The text of the button
209209
* @param {string} [buttonConfigs[].buttonType='button'] - The type of button (e.g., 'submit', 'reset')
210210
* @param {boolean} [buttonConfigs[].shouldBeDisabled=false] - Whether the button should be disabled
211+
* @param {string} [buttonConfigs[].buttonWrapperClass='bx--btn-set'] - The CSS class of the wrapper element containing the buttons
211212
*
212213
* Example:
213214
* cy.validateFormFooterButtons([
214215
* { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Cancel' },
215216
* { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Reset', [BUTTON_CONFIG_KEYS.SHOULD_BE_DISABLED]: true },
216-
* { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Submit', [BUTTON_CONFIG_KEYS.BUTTON_TYPE]: 'submit' }
217+
* { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Submit', [BUTTON_CONFIG_KEYS.BUTTON_TYPE]: 'submit' },
218+
* { [BUTTON_CONFIG_KEYS.BUTTON_TEXT]: 'Save', [BUTTON_CONFIG_KEYS.BUTTON_WRAPPER_CLASS]: 'custom-wrapper' }
217219
* ]);
218220
*
219221
* Or using regular object keys:
220222
* cy.validateFormFooterButtons([
221223
* { buttonText: 'Cancel' },
222224
* { buttonText: 'Reset', shouldBeDisabled: true },
223-
* { buttonText: 'Submit', buttonType: 'submit' }
225+
* { buttonText: 'Submit', buttonType: 'submit' },
226+
* { buttonText: 'Save', buttonWrapperClass: 'custom-wrapper' }
224227
* ]);
225228
*
226229
* Both approaches work but using config-keys object(BUTTON_CONFIG_KEYS) is recommended to avoid typos and unknown keys
@@ -241,6 +244,7 @@ Cypress.Commands.add('validateFormFooterButtons', (buttonConfigs) => {
241244
const buttonType = config[BUTTON_CONFIG_KEYS.BUTTON_TYPE] || 'button';
242245
const shouldBeDisabled =
243246
config[BUTTON_CONFIG_KEYS.SHOULD_BE_DISABLED] || false;
247+
const buttonWrapperClass = config[BUTTON_CONFIG_KEYS.BUTTON_WRAPPER_CLASS];
244248

245249
if (!buttonText) {
246250
cy.logAndThrowError(
@@ -252,6 +256,7 @@ Cypress.Commands.add('validateFormFooterButtons', (buttonConfigs) => {
252256
.getFormFooterButtonByTypeWithText({
253257
buttonText,
254258
buttonType,
259+
buttonWrapperClass,
255260
})
256261
.should('be.visible');
257262

0 commit comments

Comments
 (0)