Skip to content

Commit 105e909

Browse files
authored
Merge pull request #9598 from asirvadAbrahamVarghese/enhance-form-element-selectors
Enhance form element selectors
2 parents afa3d77 + 84dcf23 commit 105e909

File tree

4 files changed

+137
-55
lines changed

4 files changed

+137
-55
lines changed

cypress/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ ManageIQ implements the following cypress extensions:
7474

7575
##### element_selectors
7676

77-
* `cy.getFormFooterButtonByType(name, type)` - retrieves form footer button by its name and type. `name` is the name or text content of the button. `type` is the HTML button type (e.g., 'button', 'submit', 'reset'). Defaults to 'button'. e.g. `cy.getFormFooterButtonByType('Save');`, `cy.getFormFooterButtonByType('Reset', 'reset');`
78-
* `cy.getFormInputFieldById(inputId, type)` - retrieves a form input field by its ID and type. `inputId` is the ID of the input field. `type` is the HTML input type (e.g., 'text', 'email', 'password'). Defaults to 'text'. e.g. `cy.getFormInputFieldById('name');`, `cy.getFormInputFieldById('name', 'text');`
79-
* `getFormLabelByInputId(inputId)` - retrieves a form label associated with a specific input field by its ID. `inputId` is the ID of the input field. e.g. `cy.getFormLabelByInputId('name');`
80-
* `cy.getFormSelectFieldById(selectId)` - retrieves a form select field by its ID. `selectId` is the ID of the select field. e.g. `cy.getFormSelectFieldById('select-scan-limit');`
77+
* `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'});`
78+
* `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'});`
79+
* `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'});`
80+
* `cy.getFormSelectFieldById({ selectId })` - retrieves a form select field by its ID. `selectId` is the ID of the select field. e.g. `cy.getFormSelectFieldById({selectId: 'select-scan-limit'});`
81+
* `cy.getFormTextareaById({ textareaId })` - retrieves a form textarea field by its ID. `textareaId` is the ID of the textarea field. e.g. `cy.getFormTextareaById({textareaId: 'default.auth_key'});`
8182

8283
#### Assertions
8384

cypress/e2e/ui/Settings/Application-Settings/edit_collect_logs.cy.js

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,21 @@ function resetProtocolDropdown({ selectServerListItem = true } = {}) {
6666
selectToolbarEditButton();
6767

6868
// Resetting Protocol dropdown value
69-
cy.getFormSelectFieldById('log_protocol').then((selectField) => {
70-
const currentValue = selectField.val();
71-
// If the value is not default one(BLANK_VALUE), then setting it to blank
72-
if (currentValue !== DROPDOWN_BLANK_VALUE) {
73-
cy.wrap(selectField).select(DROPDOWN_BLANK_VALUE);
74-
cy.getFormFooterButtonByType(SAVE_BUTTON_TEXT, 'submit').click();
75-
// Validating confirmation flash message
76-
cy.expect_flash(flashClassMap.success, FLASH_MESSAGE_SETTINGS_SAVED);
69+
cy.getFormSelectFieldById({ selectId: 'log_protocol' }).then(
70+
(selectField) => {
71+
const currentValue = selectField.val();
72+
// If the value is not default one(BLANK_VALUE), then setting it to blank
73+
if (currentValue !== DROPDOWN_BLANK_VALUE) {
74+
cy.wrap(selectField).select(DROPDOWN_BLANK_VALUE);
75+
cy.getFormFooterButtonByTypeWithText({
76+
buttonText: SAVE_BUTTON_TEXT,
77+
buttonType: 'submit',
78+
}).click();
79+
// Validating confirmation flash message
80+
cy.expect_flash(flashClassMap.success, FLASH_MESSAGE_SETTINGS_SAVED);
81+
}
7782
}
78-
});
83+
);
7984
}
8085

8186
function goToCollectLogsTabAndOpenEditForm() {
@@ -93,49 +98,71 @@ function validateFormElements() {
9398
'be.visible'
9499
);
95100
// Assert protocol field label is visible
96-
cy.getFormLabelByInputId('log_protocol').should('be.visible');
101+
cy.getFormLabelByForAttribute({ forValue: 'log_protocol' }).should(
102+
'be.visible'
103+
);
97104
// Assert protocol field is visible and enabled
98-
cy.getFormSelectFieldById('log_protocol')
105+
cy.getFormSelectFieldById({ selectId: 'log_protocol' })
99106
.should('be.visible')
100107
.and('be.enabled');
101108
// Assert cancel button is visible and enabled
102-
cy.getFormFooterButtonByType(CANCEL_BUTTON_TEXT)
109+
cy.getFormFooterButtonByTypeWithText({
110+
buttonText: CANCEL_BUTTON_TEXT,
111+
})
103112
.should('be.visible')
104113
.and('be.enabled');
105114
// Assert save button is visible and disabled
106-
cy.getFormFooterButtonByType(SAVE_BUTTON_TEXT, 'submit')
115+
cy.getFormFooterButtonByTypeWithText({
116+
buttonText: SAVE_BUTTON_TEXT,
117+
buttonType: 'submit',
118+
})
107119
.should('be.visible')
108120
.and('be.disabled');
109121
// Assert reset button is visible and disabled
110-
cy.getFormFooterButtonByType(RESET_BUTTON_TEXT)
122+
cy.getFormFooterButtonByTypeWithText({
123+
buttonText: RESET_BUTTON_TEXT,
124+
})
111125
.should('be.visible')
112126
.and('be.disabled');
113127
}
114128

115129
function cancelButtonValidation() {
116130
// Click cancel button in the form
117-
cy.getFormFooterButtonByType(CANCEL_BUTTON_TEXT).click();
131+
cy.getFormFooterButtonByTypeWithText({
132+
buttonText: CANCEL_BUTTON_TEXT,
133+
}).click();
118134
// Validating confirmation flash message
119135
cy.expect_flash(flashClassMap.success, FLASH_MESSAGE_OPERATION_CANCELLED);
120136
}
121137

122138
function resetButtonValidation() {
123139
// Selecting Samba option from dropdown
124-
cy.getFormSelectFieldById('log_protocol').select(SAMBA_DROPDOWN_VALUE);
140+
cy.getFormSelectFieldById({ selectId: 'log_protocol' }).select(
141+
SAMBA_DROPDOWN_VALUE
142+
);
125143
// Confirm Reset button is enabled once dropdown value is changed and then click on Reset
126-
cy.getFormFooterButtonByType(RESET_BUTTON_TEXT).should('be.enabled').click();
144+
cy.getFormFooterButtonByTypeWithText({
145+
buttonText: RESET_BUTTON_TEXT,
146+
})
147+
.should('be.enabled')
148+
.click();
127149
// Confirm dropdown has the old value
128-
cy.getFormSelectFieldById('log_protocol').should(
150+
cy.getFormSelectFieldById({ selectId: 'log_protocol' }).should(
129151
'have.value',
130152
DROPDOWN_BLANK_VALUE
131153
);
132154
}
133155

134156
function saveButtonValidation() {
135157
// Selecting Samba option from dropdown
136-
cy.getFormSelectFieldById('log_protocol').select(SAMBA_DROPDOWN_VALUE);
158+
cy.getFormSelectFieldById({ selectId: 'log_protocol' }).select(
159+
SAMBA_DROPDOWN_VALUE
160+
);
137161
// Confirm Save button is enabled once dropdown value is changed and then click on Save
138-
cy.getFormFooterButtonByType(SAVE_BUTTON_TEXT, 'submit')
162+
cy.getFormFooterButtonByTypeWithText({
163+
buttonText: SAVE_BUTTON_TEXT,
164+
buttonType: 'submit',
165+
})
139166
.should('be.enabled')
140167
.click();
141168
// Validating confirmation flash message

cypress/e2e/ui/Settings/Application-Settings/settings_access_control.cy.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@ describe('Settings > Application Settings > Access Control', () => {
3131
]);
3232

3333
cy.toolbar(TOOLBAR_MENU, 'Add child Tenant to this Tenant');
34-
cy.getFormInputFieldById('name').type(INITIAL_TENANT_NAME);
35-
cy.getFormInputFieldById('description').type(INITIAL_TENANT_DESCRIPTION);
36-
cy.getFormFooterButtonByType('Add', 'submit').click();
34+
cy.getFormInputFieldByIdAndType({ inputId: 'name' }).type(
35+
INITIAL_TENANT_NAME
36+
);
37+
cy.getFormInputFieldByIdAndType({ inputId: 'description' }).type(
38+
INITIAL_TENANT_DESCRIPTION
39+
);
40+
cy.getFormFooterButtonByTypeWithText({
41+
buttonText: 'Add',
42+
buttonType: 'submit',
43+
}).click();
3744
cy.expect_flash(flashClassMap.success, FLASH_MESSAGE_OPERATION_ADDED);
3845
cy.selectAccordionItem([
3946
/^ManageIQ Region/,
Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,104 @@
1-
/* eslint-disable no-undef */
2-
31
/**
4-
* Retrieves a form footer button by its name and type.
2+
* Retrieves a form footer button by its text content and type using an object parameter.
53
*
6-
* @param {string} name - The name or text content of the button.
7-
* @param {string} [type='button'] - The HTML button type (e.g., 'button', 'submit', 'reset'). Defaults to 'button'.
4+
* @param {Object} options - The options object.
5+
* @param {string} options.buttonText - The text content of the button (required).
6+
* @param {string} [options.buttonType='button'] - The HTML button type (e.g., 'button', 'submit', 'reset'). Defaults to 'button'.
87
* @returns {Element} The matched button element.
8+
* @throws {Error} If buttonName is not provided.
99
*
1010
* Example:
11-
* cy.getFormFooterButtonByType('Save Changes');
12-
* cy.getFormFooterButtonByType('Reset', 'reset');
11+
* cy.getFormFooterButtonByTypeWithText({ buttonText: 'Save Changes' });
12+
* cy.getFormFooterButtonByTypeWithText({ buttonText: 'Submit', buttonType: 'submit' });
1313
*/
14-
Cypress.Commands.add('getFormFooterButtonByType', (name, type = 'button') =>
15-
cy.contains(`#main-content .bx--btn-set button[type="${type}"]`, name)
14+
Cypress.Commands.add(
15+
'getFormFooterButtonByTypeWithText',
16+
({ buttonType = 'button', buttonText } = {}) => {
17+
if (!buttonText) {
18+
cy.logAndThrowError('buttonName is required');
19+
}
20+
return cy.contains(
21+
`#main-content .bx--btn-set button[type="${buttonType}"]`,
22+
buttonText
23+
);
24+
}
1625
);
1726

1827
/**
19-
* Retrieves a form input field by its ID and type.
28+
* Retrieves a form input field by its ID and type using an object parameter.
2029
*
21-
* @param {string} inputId - The ID of the input field.
22-
* @param {string} [type='text'] - The HTML input type (e.g., 'text', 'email', 'password'). Defaults to 'text'.
30+
* @param {Object} options - The options object.
31+
* @param {string} options.inputId - The ID of the input field (required).
32+
* @param {string} [options.inputType='text'] - The HTML input inputType (e.g., 'text', 'email', 'password'). Defaults to 'text'.
2333
* @returns {Element} The matched input field element.
34+
* @throws {Error} If inputId is not provided.
2435
*
2536
* Example:
26-
* cy.getFormInputFieldById('name');
27-
* cy.getFormInputFieldById('name', 'text');
37+
* cy.getFormInputFieldByIdAndType({ inputId: 'name' });
38+
* cy.getFormInputFieldByIdAndType({ inputId: 'password', inputType: 'password' });
2839
*/
29-
Cypress.Commands.add('getFormInputFieldById', (inputId, type = 'text') =>
30-
cy.get(`#main-content .bx--form input#${inputId}[type="${type}"]`)
40+
Cypress.Commands.add(
41+
'getFormInputFieldByIdAndType',
42+
({ inputId, inputType = 'text' }) => {
43+
if (!inputId) {
44+
cy.logAndThrowError('inputId is required');
45+
}
46+
return cy.get(
47+
`#main-content .bx--form input[id="${inputId}"][type="${inputType}"]`
48+
);
49+
}
3150
);
3251

3352
/**
34-
* Retrieves a form label associated with a specific input field by its ID.
53+
* Retrieves a form label associated with a specific input field by its 'for' attribute.
3554
*
36-
* @param {string} inputId - The ID of the input field.
55+
* @param {Object} options - The options object.
56+
* @param {string} options.forValue - The value of the 'for' attribute that matches the input field's ID (required).
3757
* @returns {Element} The matched label element.
58+
* @throws {Error} If forValue is not provided.
3859
*
3960
* Example:
40-
* cy.getFormLabelByInputId('name');
61+
* cy.getFormLabelByForAttribute({ forValue: 'name' });
4162
*/
42-
Cypress.Commands.add('getFormLabelByInputId', (inputId) =>
43-
cy.get(`#main-content .bx--form label[for="${inputId}"]`)
44-
);
63+
Cypress.Commands.add('getFormLabelByForAttribute', ({ forValue }) => {
64+
if (!forValue) {
65+
cy.logAndThrowError('forValue is required');
66+
}
67+
return cy.get(`#main-content .bx--form label[for="${forValue}"]`);
68+
});
4569

4670
/**
47-
* Retrieves a form select field by its ID.
71+
* Retrieves a form select field by its ID using an object parameter.
4872
*
49-
* @param {string} selectId - The ID of the select field.
73+
* @param {Object} options - The options object.
74+
* @param {string} options.selectId - The ID of the select field (required).
5075
* @returns {Element} The matched select field element.
76+
* @throws {Error} If selectId is not provided.
5177
*
5278
* Example:
53-
* cy.getFormSelectFieldById('select-scan-limit');
79+
* cy.getFormSelectFieldById({ selectId: 'select-scan-limit' });
5480
*/
55-
Cypress.Commands.add('getFormSelectFieldById', (selectId) =>
56-
cy.get(`#main-content .bx--form select#${selectId}`)
57-
);
81+
Cypress.Commands.add('getFormSelectFieldById', ({ selectId }) => {
82+
if (!selectId) {
83+
cy.logAndThrowError('selectId is required');
84+
}
85+
return cy.get(`#main-content .bx--form select[id="${selectId}"]`);
86+
});
87+
88+
/**
89+
* Retrieves a form textarea field by its ID using an object parameter.
90+
*
91+
* @param {Object} options - The options object.
92+
* @param {string} options.textareaId - The ID of the textarea field (required).
93+
* @returns {Element} The matched textarea field element.
94+
* @throws {Error} If textareaId is not provided.
95+
*
96+
* Example:
97+
* cy.getFormTextareaById({ textareaId: 'default.auth_key' });
98+
*/
99+
Cypress.Commands.add('getFormTextareaById', ({ textareaId }) => {
100+
if (!textareaId) {
101+
cy.logAndThrowError('textareaId is required');
102+
}
103+
return cy.get(`#main-content .bx--form textarea[id="${textareaId}"]`);
104+
});

0 commit comments

Comments
 (0)