Skip to content

Commit 0f1de19

Browse files
authored
Merge pull request #9530 from asirvadAbrahamVarghese/fix-toolbar-cypress-command
Fixed toolbar to throw error on wrong option
2 parents ef8fb4c + 0ba2929 commit 0f1de19

File tree

4 files changed

+66
-36
lines changed

4 files changed

+66
-36
lines changed

cypress/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ ManageIQ implements the following cypress extensions:
5454
##### toolbar
5555

5656
* `cy.toolbarItems(toolbarButton)` - returns an array of objects {text: String, disabled: Boolean} for the toolbar dropdown buttons for when a toolbar button is clicked. `toolbarButton` is the string for the text of the toolbar button that you want to click on.
57-
* `cy.toolbar(toolbarButton, dropdownButton)` - click on the toolbar button specified by the user. Can also then click on a specified dropdown button as well. `toolbarButton` is the string for the text of the toolbar button that you want to click on. `dropdownButton` is the string for the text of the toolbar dropdown button that you want to click on. s
57+
* `cy.toolbar(toolbarButton, toolbarOption)` - click on the toolbar button specified by the user. Can also then click on a specified dropdown button as well. `toolbarButton` is the string for the text of the toolbar button that you want to click on. `toolbarOption` is the string for the text of the toolbar dropdown option that you want to click on.
58+
59+
##### custom_logging_commands
60+
61+
* `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');`
5862

5963
#### Assertions
6064

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* eslint-disable no-undef */
2+
3+
/**
4+
* Logs a custom error message to Cypress log and then throws an error.
5+
*
6+
* @param {string} messageToLog - The message to display in the Cypress command log.
7+
* @param {string} [messageToThrow] - Optional error message to throw, defaults to messageToLog
8+
*
9+
* Usage:
10+
* cy.logAndThrowError('This is the logged message', 'This is the thrown error message');
11+
*/
12+
Cypress.Commands.add('logAndThrowError', (messageToLog, messageToThrow) => {
13+
Cypress.log({
14+
name: 'error',
15+
displayName: '❗ CypressError:',
16+
message: messageToLog,
17+
});
18+
throw new Error(messageToThrow || messageToLog);
19+
});

cypress/support/commands/toolbar.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,42 @@
11
/* eslint-disable no-undef */
22

33
// 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`);
1316
}
17+
return cy.wrap(targetToolbarButton).click();
1418
});
15-
});
1619

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`);
2831
}
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+
});
3135
});
3236
}
33-
return cy.wrap(null);
37+
38+
// else, just return the toolbar button click chain
39+
return clickToolbarButton;
3440
});
3541

3642
// toolbarButton: String for the text of the toolbar button to click.

cypress/support/e2e.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,21 @@
4040
// ***********************************************************
4141

4242
// Commands
43-
import './commands/explorer.js'
44-
import './commands/gtl.js'
45-
import './commands/login.js'
46-
import './commands/menu.js'
47-
import './commands/stub_notifications.js'
48-
import './commands/toolbar.js'
49-
import './commands/throttle_response.js'
43+
import './commands/custom_logging_commands.js';
44+
import './commands/explorer.js';
45+
import './commands/gtl.js';
46+
import './commands/login.js';
47+
import './commands/menu.js';
48+
import './commands/stub_notifications.js';
49+
import './commands/throttle_response.js';
50+
import './commands/toolbar.js';
5051

5152
// Assertions
52-
import './assertions/expect_rates_table.js';
53-
import './assertions/expect_search_box.js'
54-
import './assertions/expect_title.js'
55-
import './assertions/expect_text.js'
5653
import './assertions/expect_alerts.js';
54+
import './assertions/expect_rates_table.js';
55+
import './assertions/expect_search_box.js';
56+
import './assertions/expect_text.js';
57+
import './assertions/expect_title.js';
5758

5859
// This is needed to prevent Cypress tests from failing due to uncaught errors:
5960
// Undefined errors are occuring on every initial page load of Manage IQ

0 commit comments

Comments
 (0)