From df8ccf026e8112ab87f3b1af543bd7d35baaa4a7 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 25 Feb 2021 14:26:03 -0500 Subject: [PATCH] Fixes Cypress flake by adding pipe, click, and should (#92762) (#92777) ## Summary We were seeing flake tests with the error messages: ```ts expected '' to have text 'Install 2 Elastic prebuilt rules ', but the text was 'Install 1 Elastic prebuilt rule ' ``` Running this locally several times I was able to reproduce the flake. I was able to see that the click handler when clicked on the check boxes is not always taking effect for the first check box. A common issue with Cypress is that a lot of page loads and JS activities on pages can add/remove click handlers quickly such as when we are frosting and un-frosting a loading screen. When we try and click on the click handlers for a test and the click handlers are not added yet, we miss one or more and end up with a flake test. Instead we can click as fast as possible using `pipe` and then checking that the check box is clicked before continuing using a `should` Even though the loading screen is done frosting in these tests, the click handler can take one or two milliseconds before they end up being added to the checkboxes. **Analysis** When you have the flake and fail you can scroll in Cypress and pin a point in time and see that indeed we did not get the first checkbox checked even though we had a click. Which makes sense as within a few milliseconds the click handlers are added and we do check the second checkbox: analysis ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios Co-authored-by: Frank Hassanabad --- .../cypress/tasks/alerts_detection_rules.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/security_solution/cypress/tasks/alerts_detection_rules.ts b/x-pack/plugins/security_solution/cypress/tasks/alerts_detection_rules.ts index 529ef4afdfa637..10644e046a68b8 100644 --- a/x-pack/plugins/security_solution/cypress/tasks/alerts_detection_rules.ts +++ b/x-pack/plugins/security_solution/cypress/tasks/alerts_detection_rules.ts @@ -116,9 +116,19 @@ export const reloadDeletedRules = () => { cy.get(RELOAD_PREBUILT_RULES_BTN).click({ force: true }); }; +/** + * Selects the number of rules. Since there can be missing click handlers + * when the page loads at first, we use a pipe and a trigger of click + * on it and then check to ensure that it is checked before continuing + * with the tests. + * @param numberOfRules The number of rules to click/check + */ export const selectNumberOfRules = (numberOfRules: number) => { for (let i = 0; i < numberOfRules; i++) { - cy.get(RULE_CHECKBOX).eq(i).click({ force: true }); + cy.get(RULE_CHECKBOX) + .eq(i) + .pipe(($el) => $el.trigger('click')) + .should('be.checked'); } };