Skip to content

Commit 95ff009

Browse files
Added command to validate form labels
1 parent 6f934f8 commit 95ff009

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Form elements validation constants
3+
* ==========================================================
4+
*/
5+
6+
// Form label configuration keys
7+
export const LABEL_CONFIG_KEYS = {
8+
FOR_VALUE: 'forValue',
9+
EXPECTED_TEXT: 'expectedText',
10+
};
11+
12+
/* ========================================================== */
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// TODO: Use aliased import(@cypress-dir) once #9631 is merged
2+
import { LABEL_CONFIG_KEYS } from './command_constants.js';
3+
4+
/**
5+
* Validates form field labels based on provided configurations
6+
*
7+
* @param {Array} labelConfigs - Array of label configuration objects
8+
* @param {string} labelConfigs[].forValue - The 'for' attribute value of the label
9+
* @param {string} [labelConfigs[].expectedText] - The expected text content of the label
10+
*
11+
* Example:
12+
* cy.validateFormLabels([
13+
* { [LABEL_CONFIG_KEYS.FOR_VALUE]: 'name', [LABEL_CONFIG_KEYS.EXPECTED_TEXT]: 'Name' },
14+
* { [LABEL_CONFIG_KEYS.FOR_VALUE]: 'email', [LABEL_CONFIG_KEYS.EXPECTED_TEXT]: 'Email Address' }
15+
* ]);
16+
*
17+
* Or using regular object keys:
18+
* cy.validateFormLabels([
19+
* { forValue: 'name', expectedText: 'Name' },
20+
* { forValue: 'email', expectedText: 'Email Address' }
21+
* ]);
22+
*
23+
* Both approaches work but using config-keys object(LABEL_CONFIG_KEYS) is recommended to avoid typos and unknown keys
24+
*/
25+
Cypress.Commands.add('validateFormLabels', (labelConfigs) => {
26+
if (!Array.isArray(labelConfigs)) {
27+
cy.logAndThrowError('labelConfigs must be an array');
28+
}
29+
30+
if (!labelConfigs.length) {
31+
cy.logAndThrowError('labelConfigs array cannot be empty');
32+
}
33+
34+
labelConfigs.forEach((config) => {
35+
const forValue = config[LABEL_CONFIG_KEYS.FOR_VALUE];
36+
const expectedText = config[LABEL_CONFIG_KEYS.EXPECTED_TEXT];
37+
38+
if (!forValue) {
39+
cy.logAndThrowError(
40+
`${LABEL_CONFIG_KEYS.FOR_VALUE} is required for each label config`
41+
);
42+
}
43+
44+
const labelCheck = cy
45+
.getFormLabelByForAttribute({ forValue })
46+
.should('be.visible');
47+
48+
if (expectedText) {
49+
labelCheck.and('contain.text', expectedText);
50+
}
51+
});
52+
});

cypress/support/e2e.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import './commands/api_commands.js';
4444
import './commands/custom_logging_commands.js';
4545
import './commands/element_selectors.js';
4646
import './commands/explorer.js';
47+
import './commands/form_elements_validation_commands.js';
4748
import './commands/gtl.js';
4849
import './commands/login.js';
4950
import './commands/menu.js';

0 commit comments

Comments
 (0)