Skip to content

Commit a953648

Browse files
authored
Merge pull request #9537 from asirvadAbrahamVarghese/add-form-element-selectors
Add custom commands for selecting form elements
2 parents e70aeed + ad8b665 commit a953648

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

cypress/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ ManageIQ implements the following cypress extensions:
6161

6262
* `cy.logAndThrowError(messageToLog, messageToThrow)` - Logs a custom error message to Cypress log and then throws an error. `messageToLog` is the message to display in the Cypress command log. `messageToThrow` is the optional error message to throw, defaults to `messageToLog`. e.g. `cy.logAndThrowError('This is the logged message', 'This is the thrown error message');`, `cy.logAndThrowError('This is the message that gets logged and thrown');`
6363

64+
##### element_selectors
65+
66+
* `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');`
67+
* `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');`
68+
* `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');`
69+
* `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');`
70+
6471
#### Assertions
6572

6673
* `cy.expect_explorer_title(title)` - check that the title on an explorer screen matches the provided title. `title`: String for the title.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* eslint-disable no-undef */
2+
3+
/**
4+
* Retrieves a form footer button by its name and type.
5+
*
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'.
8+
* @returns {Element} The matched button element.
9+
*
10+
* Example:
11+
* cy.getFormFooterButtonByType('Save Changes');
12+
* cy.getFormFooterButtonByType('Reset', 'reset');
13+
*/
14+
Cypress.Commands.add('getFormFooterButtonByType', (name, type = 'button') =>
15+
cy.contains(`#main-content .bx--btn-set button[type="${type}"]`, name)
16+
);
17+
18+
/**
19+
* Retrieves a form input field by its ID and type.
20+
*
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'.
23+
* @returns {Element} The matched input field element.
24+
*
25+
* Example:
26+
* cy.getFormInputFieldById('name');
27+
* cy.getFormInputFieldById('name', 'text');
28+
*/
29+
Cypress.Commands.add('getFormInputFieldById', (inputId, type = 'text') =>
30+
cy.get(`#main-content .bx--form input#${inputId}[type="${type}"]`)
31+
);
32+
33+
/**
34+
* Retrieves a form label associated with a specific input field by its ID.
35+
*
36+
* @param {string} inputId - The ID of the input field.
37+
* @returns {Element} The matched label element.
38+
*
39+
* Example:
40+
* cy.getFormLabelByInputId('name');
41+
*/
42+
Cypress.Commands.add('getFormLabelByInputId', (inputId) =>
43+
cy.get(`#main-content .bx--form label[for="${inputId}"]`)
44+
);
45+
46+
/**
47+
* Retrieves a form select field by its ID.
48+
*
49+
* @param {string} selectId - The ID of the select field.
50+
* @returns {Element} The matched select field element.
51+
*
52+
* Example:
53+
* cy.getFormSelectFieldById('select-scan-limit');
54+
*/
55+
Cypress.Commands.add('getFormSelectFieldById', (selectId) =>
56+
cy.get(`#main-content .bx--form select#${selectId}`)
57+
);

cypress/support/e2e.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
// Commands
4343
import './commands/custom_logging_commands.js';
44+
import './commands/element_selectors.js';
4445
import './commands/explorer.js';
4546
import './commands/gtl.js';
4647
import './commands/login.js';

0 commit comments

Comments
 (0)