Skip to content

Commit 672f387

Browse files
Added command to validate form user-input fields
1 parent 20b9b6d commit 672f387

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

cypress/support/commands/command_constants.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,20 @@ export const LABEL_CONFIG_KEYS = {
99
EXPECTED_TEXT: 'expectedText',
1010
};
1111

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+
1228
/* ========================================================== */

cypress/support/commands/form_elements_validation_commands.js

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// TODO: Use aliased import(@cypress-dir) once #9631 is merged
2-
import { LABEL_CONFIG_KEYS } from './command_constants.js';
2+
import {
3+
LABEL_CONFIG_KEYS,
4+
FIELD_CONFIG_KEYS,
5+
FIELD_TYPES,
6+
} from './command_constants.js';
37

48
/**
59
* Helper function to validate that config objects only contain valid keys
@@ -71,3 +75,115 @@ Cypress.Commands.add('validateFormLabels', (labelConfigs) => {
7175
}
7276
});
7377
});
78+
79+
/**
80+
* Validates form input fields based on provided configurations
81+
*
82+
* @param {Array} fieldConfigs - Array of field configuration objects
83+
* @param {string} fieldConfigs[].id - The ID of the form field
84+
* @param {string} [fieldConfigs[].fieldType='input'] - The type of field ('input', 'select', 'textarea')
85+
* @param {string} [fieldConfigs[].inputFieldType='text'] - The type of input field ('text', 'password', 'number')
86+
* @param {boolean} [fieldConfigs[].shouldBeDisabled=false] - Whether the field should be disabled
87+
* @param {string} [fieldConfigs[].expectedValue] - The expected value of the field
88+
*
89+
* Example:
90+
* cy.validateFormFields([
91+
* { [FIELD_CONFIG_KEYS.ID]: 'name', [FIELD_CONFIG_KEYS.SHOULD_BE_DISABLED]: true },
92+
* { [FIELD_CONFIG_KEYS.ID]: 'email', [FIELD_CONFIG_KEYS.INPUT_FIELD_TYPE]: 'email' },
93+
* {
94+
* [FIELD_CONFIG_KEYS.ID]: 'role',
95+
* [FIELD_CONFIG_KEYS.FIELD_TYPE]: FIELD_TYPES.SELECT,
96+
* [FIELD_CONFIG_KEYS.EXPECTED_VALUE]: 'admin'
97+
* }
98+
* ]);
99+
*
100+
* Or using regular object keys:
101+
* cy.validateFormFields([
102+
* { id: 'name', shouldBeDisabled: true },
103+
* { id: 'email' },
104+
* { id: 'role', fieldType: 'select', expectedValue: 'admin' }
105+
* ]);
106+
*
107+
* Both approaches work but using config-keys object(FIELD_CONFIG_KEYS) is recommended to avoid typos and unknown keys
108+
*/
109+
Cypress.Commands.add('validateFormFields', (fieldConfigs) => {
110+
if (!Array.isArray(fieldConfigs)) {
111+
cy.logAndThrowError('fieldConfigs must be an array');
112+
}
113+
114+
if (!fieldConfigs.length) {
115+
cy.logAndThrowError('fieldConfigs array cannot be empty');
116+
}
117+
118+
fieldConfigs.forEach((config) => {
119+
validateConfigKeys(config, FIELD_CONFIG_KEYS, 'field');
120+
121+
const id = config[FIELD_CONFIG_KEYS.ID];
122+
const fieldType = config[FIELD_CONFIG_KEYS.FIELD_TYPE] || FIELD_TYPES.INPUT;
123+
const inputFieldType = config[FIELD_CONFIG_KEYS.INPUT_FIELD_TYPE] || 'text';
124+
const shouldBeDisabled =
125+
config[FIELD_CONFIG_KEYS.SHOULD_BE_DISABLED] || false;
126+
const expectedValue = config[FIELD_CONFIG_KEYS.EXPECTED_VALUE];
127+
128+
if (!id) {
129+
cy.logAndThrowError(
130+
`${FIELD_CONFIG_KEYS.ID} is required for each field config`
131+
);
132+
}
133+
134+
// Check field based on type
135+
switch (fieldType) {
136+
case FIELD_TYPES.INPUT:
137+
cy.getFormInputFieldByIdAndType({
138+
inputId: id,
139+
inputType: inputFieldType,
140+
})
141+
.should('be.visible')
142+
.then((field) => {
143+
if (shouldBeDisabled) {
144+
expect(field).to.be.disabled;
145+
} else {
146+
expect(field).to.not.be.disabled;
147+
}
148+
149+
if (expectedValue) {
150+
cy.wrap(field).should('have.value', expectedValue);
151+
}
152+
});
153+
break;
154+
case FIELD_TYPES.SELECT:
155+
cy.getFormSelectFieldById({ selectId: id })
156+
.should('be.visible')
157+
.then((field) => {
158+
if (shouldBeDisabled) {
159+
expect(field).to.be.disabled;
160+
} else {
161+
expect(field).to.not.be.disabled;
162+
}
163+
164+
if (expectedValue) {
165+
cy.wrap(field).should('have.value', expectedValue);
166+
}
167+
});
168+
break;
169+
case FIELD_TYPES.TEXTAREA:
170+
cy.getFormTextareaById({ textareaId: id })
171+
.should('be.visible')
172+
.then((field) => {
173+
if (shouldBeDisabled) {
174+
expect(field).to.be.disabled;
175+
} else {
176+
expect(field).to.not.be.disabled;
177+
}
178+
179+
if (expectedValue) {
180+
cy.wrap(field).should('have.value', expectedValue);
181+
}
182+
});
183+
break;
184+
185+
default:
186+
cy.logAndThrowError(`Unsupported field type: ${fieldType}`);
187+
}
188+
});
189+
});

0 commit comments

Comments
 (0)