|
1 | 1 | /* eslint-disable no-undef */ |
2 | 2 |
|
3 | 3 | // toolbarButton: String for the text of the toolbar button to click. |
4 | | -// dropdownButton: String for the text of the dropdown button to click after the toolbar dropdown is opened. |
5 | | -Cypress.Commands.add('toolbar', (toolbarButton, dropdownButton = '') => { |
6 | | - cy.get('#toolbar').get('button').then((toolbarButtons) => { |
7 | | - const nums = [...Array(toolbarButtons.length).keys()]; |
8 | | - nums.forEach((index) => { |
9 | | - const button = toolbarButtons[index].children[0]; |
10 | | - if (button && button.innerText && button.innerText.includes(toolbarButton)) { |
11 | | - button.click(); |
12 | | - return; |
| 4 | +// toolbarOption: String for the text of the dropdown button to click after the toolbar dropdown is opened. |
| 5 | +Cypress.Commands.add('toolbar', (toolbarButton, toolbarOption = '') => { |
| 6 | + const clickToolbarButton = cy |
| 7 | + .get('#toolbar') |
| 8 | + .find('button') |
| 9 | + .then((buttons) => { |
| 10 | + const targetToolbarButton = [...buttons].find( |
| 11 | + (btn) => btn.innerText.trim() === toolbarButton |
| 12 | + ); |
| 13 | + |
| 14 | + if (!targetToolbarButton) { |
| 15 | + cy.logAndThrowError(`Toolbar button: "${toolbarButton}" was not found`); |
13 | 16 | } |
| 17 | + return cy.wrap(targetToolbarButton).click(); |
14 | 18 | }); |
15 | | - }); |
16 | 19 |
|
17 | | - if (dropdownButton) { |
18 | | - return cy.get('.bx--overflow-menu-options').then((dropdownButtons) => { |
19 | | - const buttons = dropdownButtons.children(); |
20 | | - for (let index = 0; index < buttons.length; index++) { |
21 | | - const button = buttons[index]; |
22 | | - if ( |
23 | | - button && |
24 | | - button.innerText && |
25 | | - button.innerText.includes(dropdownButton) |
26 | | - ) { |
27 | | - return cy.wrap(button.children[0]).click(); |
| 20 | + // If toolbarOption is provided, wait for toolbar to open, |
| 21 | + // then look for the given toolbar option |
| 22 | + if (toolbarOption) { |
| 23 | + return clickToolbarButton.then(() => { |
| 24 | + return cy.get('.bx--overflow-menu-options li').then((toolbarOptions) => { |
| 25 | + const targetToolbarOption = [...toolbarOptions].find( |
| 26 | + (option) => option.innerText.trim() === toolbarOption |
| 27 | + ); |
| 28 | + |
| 29 | + if (!targetToolbarOption) { |
| 30 | + cy.logAndThrowError(`"${toolbarOption}" option was not found in the "${toolbarButton}" toolbar`); |
28 | 31 | } |
29 | | - } |
30 | | - return cy.wrap(null); |
| 32 | + // returning the cypress chainable to the top of the command scope |
| 33 | + return cy.wrap(targetToolbarOption).click(); |
| 34 | + }); |
31 | 35 | }); |
32 | 36 | } |
33 | | - return cy.wrap(null); |
| 37 | + |
| 38 | + // else, just return the toolbar button click chain |
| 39 | + return clickToolbarButton; |
34 | 40 | }); |
35 | 41 |
|
36 | 42 | // toolbarButton: String for the text of the toolbar button to click. |
|
0 commit comments