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