Skip to content

Commit 4f98999

Browse files
Added command to validate form labels
1 parent b0cf6fb commit 4f98999

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
// Form user input field configuration keys
13+
export const FIELD_CONFIG_KEYS = {
14+
ID: 'id',
15+
FIELD_TYPE: 'fieldType',
16+
INPUT_FIELD_TYPE: 'inputFieldType',
17+
SHOULD_BE_DISABLED: 'shouldBeDisabled',
18+
EXPECTED_VALUE: 'expectedValue',
19+
};
20+
21+
// Form field types
22+
export const FIELD_TYPES = {
23+
INPUT: 'input',
24+
SELECT: 'select',
25+
TEXTAREA: 'textarea',
26+
};
27+
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+
35+
/* ========================================================== */
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// TODO: Use aliased import(@cypress-dir) once #9631 is merged
2+
import { LABEL_CONFIG_KEYS } from './constants/command_constants.js';
3+
4+
/**
5+
* Helper function to validate that config objects only contain valid keys
6+
*
7+
* @param {Object} config - The configuration object to validate
8+
* @param {Object} validKeysObject - The object containing valid keys (e.g., LABEL_CONFIG_KEYS)
9+
* @param {string} configType - The type of configuration being validated (for error messages)
10+
*/
11+
const validateConfigKeys = (config, validKeysObject, configType) => {
12+
const validKeys = Object.values(validKeysObject);
13+
14+
Object.keys(config).forEach(key => {
15+
if (!validKeys.includes(key)) {
16+
cy.logAndThrowError(
17+
`Unknown key "${key}" in ${configType} config. Valid keys are: ${validKeys.join(', ')}`
18+
);
19+
}
20+
});
21+
};
22+
23+
/**
24+
* Validates form field labels based on provided configurations
25+
*
26+
* @param {Array} labelConfigs - Array of label configuration objects
27+
* @param {string} labelConfigs[].forValue - The 'for' attribute value of the label
28+
* @param {string} [labelConfigs[].expectedText] - The expected text content of the label
29+
*
30+
* Example:
31+
* cy.validateFormLabels([
32+
* { [LABEL_CONFIG_KEYS.FOR_VALUE]: 'name', [LABEL_CONFIG_KEYS.EXPECTED_TEXT]: 'Name' },
33+
* { [LABEL_CONFIG_KEYS.FOR_VALUE]: 'email', [LABEL_CONFIG_KEYS.EXPECTED_TEXT]: 'Email Address' }
34+
* ]);
35+
*
36+
* Or using regular object keys:
37+
* cy.validateFormLabels([
38+
* { forValue: 'name', expectedText: 'Name' },
39+
* { forValue: 'email', expectedText: 'Email Address' }
40+
* ]);
41+
*
42+
* Both approaches work but using config-keys object(LABEL_CONFIG_KEYS) is recommended to avoid typos and unknown keys
43+
*/
44+
Cypress.Commands.add('validateFormLabels', (labelConfigs) => {
45+
if (!Array.isArray(labelConfigs)) {
46+
cy.logAndThrowError('labelConfigs must be an array');
47+
}
48+
49+
if (!labelConfigs.length) {
50+
cy.logAndThrowError('labelConfigs array cannot be empty');
51+
}
52+
53+
labelConfigs.forEach((config) => {
54+
validateConfigKeys(config, LABEL_CONFIG_KEYS, 'label');
55+
56+
const forValue = config[LABEL_CONFIG_KEYS.FOR_VALUE];
57+
const expectedText = config[LABEL_CONFIG_KEYS.EXPECTED_TEXT];
58+
59+
if (!forValue) {
60+
cy.logAndThrowError(
61+
`${LABEL_CONFIG_KEYS.FOR_VALUE} is required for each label config`
62+
);
63+
}
64+
65+
const labelCheck = cy
66+
.getFormLabelByForAttribute({ forValue })
67+
.should('be.visible');
68+
69+
if (expectedText) {
70+
labelCheck.and('contain.text', expectedText);
71+
}
72+
});
73+
});

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)