Skip to content

Commit afcc20e

Browse files
authored
Merge pull request #9654 from asirvadAbrahamVarghese/add-dual-list-commands
Add dual list commands
2 parents 696d6cd + af2e608 commit afcc20e

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

cypress/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ ManageIQ implements the following cypress extensions:
7676

7777
* `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');`
7878

79+
##### dual_list_commands
80+
81+
* `cy.dualListAction({ actionType, optionsToSelect })` - performs actions on a dual-list component (components with two lists where items can be moved between them). `actionType` is the type of action to perform, use values from DUAL_LIST_ACTION_TYPE: 'add' (move selected items from left to right), 'remove' (move selected items from right to left), 'add-all' (move all items from left to right), or 'remove-all' (move all items from right to left). `optionsToSelect` is an array of option texts to select (required for 'add' and 'remove' actions, not needed for 'add-all' and 'remove-all'). e.g. `cy.dualListAction({ actionType: DUAL_LIST_ACTION_TYPE.ADD, optionsToSelect: ['Option 1', 'Option 2'] });`, `cy.dualListAction({ actionType: DUAL_LIST_ACTION_TYPE.REMOVE, optionsToSelect: ['Option 3'] });`, `cy.dualListAction({ actionType: DUAL_LIST_ACTION_TYPE.ADD_ALL });`, `cy.dualListAction({ actionType: DUAL_LIST_ACTION_TYPE.REMOVE_ALL });`
82+
7983
##### element_selectors
8084

8185
* `cy.getFormFooterButtonByTypeWithText({ buttonType, buttonText })` - retrieves form footer button by its name and type. `buttonText` is the name or text content of the button. `buttonType` is the HTML button type (e.g., 'button', 'submit', 'reset'). Defaults to 'button'. e.g. `cy.getFormFooterButtonByType({buttonType: 'Reset', buttonText: 'reset'});`, `cy.getFormFooterButtonByTypeWithText({buttonText: 'Cancel'});`

cypress/support/commands/constants/command_constants.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,13 @@ export const BUTTON_CONFIG_KEYS = {
3333
};
3434

3535
/* ========================================================== */
36+
37+
/**
38+
* Constant for dual-list component action types
39+
*/
40+
export const DUAL_LIST_ACTION_TYPE = {
41+
ADD: 'add',
42+
REMOVE: 'remove',
43+
ADD_ALL: 'add-all',
44+
REMOVE_ALL: 'remove-all',
45+
};
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* eslint-disable no-undef */
2+
/**
3+
* @fileoverview
4+
* This file contains Cypress commands for interacting with dual-list components.
5+
* A dual-list component consists of two lists where items can be moved between them
6+
* using add/remove buttons.
7+
*/
8+
import { DUAL_LIST_ACTION_TYPE } from './constants/command_constants';
9+
10+
// CSS selectors for dual-list component elements
11+
const DUAL_LIST_BODY_SELECTOR =
12+
'fieldset.bx--fieldset .bx--structured-list-tbody';
13+
const DUAL_LIST_OPTION_ROW_SELECTOR = '.bx--structured-list-row';
14+
15+
/**
16+
* Command to perform actions on a dual list component.
17+
* This command allows you to:
18+
* - Add selected items from the left list to the right list
19+
* - Remove selected items from the right list to the left list
20+
* - Add all items from the left list to the right list
21+
* - Remove all items from the right list to the left list
22+
*
23+
* @param {Object} options - Parameters for the action
24+
* @param {string} options.actionType - Type of action to perform. Use values from DUAL_LIST_ACTION_TYPE:
25+
* - DUAL_LIST_ACTION_TYPE.ADD: Move selected items from left to right
26+
* - DUAL_LIST_ACTION_TYPE.REMOVE: Move selected items from right to left
27+
* - DUAL_LIST_ACTION_TYPE.ADD_ALL: Move all items from left to right
28+
* - DUAL_LIST_ACTION_TYPE.REMOVE_ALL: Move all items from right to left
29+
* @param {Array<string>} [options.optionsToSelect] - Array of option texts to select
30+
* (required for 'add' and 'remove' actions, not needed for 'add-all' and 'remove-all')
31+
*
32+
* @example
33+
* // Add specific items from left to right
34+
* cy.dualListAction({
35+
* actionType: DUAL_LIST_ACTION_TYPE.ADD,
36+
* optionsToSelect: ['Option 1', 'Option 2']
37+
* });
38+
*
39+
* @example
40+
* // Remove specific items from right to left
41+
* cy.dualListAction({
42+
* actionType: DUAL_LIST_ACTION_TYPE.REMOVE,
43+
* optionsToSelect: ['Option 3']
44+
* });
45+
*
46+
* @example
47+
* // Add all items from left to right
48+
* cy.dualListAction({
49+
* actionType: DUAL_LIST_ACTION_TYPE.ADD_ALL
50+
* });
51+
*
52+
* @example
53+
* // Remove all items from right to left
54+
* cy.dualListAction({
55+
* actionType: DUAL_LIST_ACTION_TYPE.REMOVE_ALL
56+
* });
57+
*/
58+
Cypress.Commands.add('dualListAction', ({ actionType, optionsToSelect }) => {
59+
if (!actionType) {
60+
cy.logAndThrowError('actionType is required');
61+
}
62+
63+
const validActionTypes = Object.values(DUAL_LIST_ACTION_TYPE);
64+
if (!validActionTypes.includes(actionType)) {
65+
cy.logAndThrowError(
66+
`Action type "${actionType}" not supported. Valid action types are: ${validActionTypes.join(
67+
', '
68+
)}`
69+
);
70+
}
71+
72+
const isActionTypeAddAll = actionType === DUAL_LIST_ACTION_TYPE.ADD_ALL;
73+
if (isActionTypeAddAll || actionType === DUAL_LIST_ACTION_TYPE.REMOVE_ALL) {
74+
const targetButtonId = isActionTypeAddAll
75+
? '#move-all-right'
76+
: '#move-all-left';
77+
78+
return cy.get(targetButtonId).click();
79+
}
80+
81+
const isActionTypeAdd = actionType === DUAL_LIST_ACTION_TYPE.ADD;
82+
if (isActionTypeAdd || actionType === DUAL_LIST_ACTION_TYPE.REMOVE) {
83+
if (!optionsToSelect || !optionsToSelect.length) {
84+
cy.logAndThrowError(
85+
'optionsToSelect array is required for add and remove actions'
86+
);
87+
}
88+
89+
const targetListIndex = isActionTypeAdd ? 'first' : 'last';
90+
const targetButtonId = isActionTypeAdd ? '#move-right' : '#move-left';
91+
optionsToSelect.forEach((option) => {
92+
cy.get(DUAL_LIST_BODY_SELECTOR)
93+
[targetListIndex]()
94+
.contains(DUAL_LIST_OPTION_ROW_SELECTOR, option)
95+
.click();
96+
});
97+
cy.get(targetButtonId).click();
98+
}
99+
});

cypress/support/e2e.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
// Commands
4343
import './commands/api_commands.js';
4444
import './commands/custom_logging_commands.js';
45+
import './commands/dual_list_commands.js';
4546
import './commands/element_selectors.js';
4647
import './commands/explorer.js';
4748
import './commands/form_elements_validation_commands.js';

0 commit comments

Comments
 (0)