cypress_test/.eslintrc | 4 + cypress_test/eslint_plugin/index.js | 31 ++++++++++ cypress_test/eslint_plugin/package.json | 5 + cypress_test/integration_tests/mobile/calc/insertion_wizard_spec.js | 2 cypress_test/package.json | 1 5 files changed, 42 insertions(+), 1 deletion(-)
New commits: commit af76b48460c84d73dafd4f756fb923032104e7a1 Author: Tamás Zolnai <[email protected]> AuthorDate: Tue Apr 28 16:00:15 2020 +0200 Commit: Tamás Zolnai <[email protected]> CommitDate: Tue Apr 28 18:42:56 2020 +0200 cypress: add a custom ESLint rule against cy.get().contains() usage. Since cypress retries only the last command, it's better to use the compact cy.contains(selector, content) version, instead of cy.get(selector).contains(content) call. Change-Id: Ie1f4c17bbf736058ecf6bd996b46384fdff19446 Reviewed-on: https://gerrit.libreoffice.org/c/online/+/93081 Tested-by: Jenkins CollaboraOffice <[email protected]> Reviewed-by: Tamás Zolnai <[email protected]> diff --git a/cypress_test/.eslintrc b/cypress_test/.eslintrc index bd0576c25..19001679f 100644 --- a/cypress_test/.eslintrc +++ b/cypress_test/.eslintrc @@ -1,5 +1,9 @@ { "extends": "../loleaflet/.eslintrc", + "plugins": ["cypress-rules"], + "rules": { + "cypress-rules/no-get-contains-chain": 2 + }, "parserOptions": { "ecmaVersion": 6, "sourceType": "module", diff --git a/cypress_test/eslint_plugin/index.js b/cypress_test/eslint_plugin/index.js new file mode 100644 index 000000000..4c2d71c16 --- /dev/null +++ b/cypress_test/eslint_plugin/index.js @@ -0,0 +1,31 @@ +module.exports = { + rules: { + 'no-get-contains-chain': { + /** + * Catches cy.get(selector).contains(content) calls in the code. + * + * Issue: In cypress test framework only the last command is retried. + * In this case it's the contains method and so the get() method is + * not retried. Sometimes, it behaves unexpectedly, because the DOM + * has the correct item matching with both the selector and the content, + * but the test still fails on this command chain. + * + * Fix 1: When we use the content as a selector. In this case, we can use + * cy.contains(selector, content) instead. This is compact command which + * will retry to match both the selector and the content. + * + * Fix 2: When we need an assertion about the content. In this case, we can + * use cy.contains(selector, content) or cy.get(selector).should('have.text', content) + * is also a good replacement. + * + **/ + create: function(context) { + return { + 'CallExpression[callee.property.name=\'contains\'][callee.object.callee.property.name=\'get\']': function(node) { + context.report(node, 'Do not chain get() and contains(). Use cy.contains(selector, content) instead for better retriability!'); + } + }; + } + } + } +}; diff --git a/cypress_test/eslint_plugin/package.json b/cypress_test/eslint_plugin/package.json new file mode 100644 index 000000000..fdf878c7f --- /dev/null +++ b/cypress_test/eslint_plugin/package.json @@ -0,0 +1,5 @@ +{ + "name": "eslint-plugin-cypress-rules", + "version": "1.0.0", + "main": "index.js" +} diff --git a/cypress_test/integration_tests/mobile/calc/insertion_wizard_spec.js b/cypress_test/integration_tests/mobile/calc/insertion_wizard_spec.js index d19e92ca6..59c2c41e0 100644 --- a/cypress_test/integration_tests/mobile/calc/insertion_wizard_spec.js +++ b/cypress_test/integration_tests/mobile/calc/insertion_wizard_spec.js @@ -64,7 +64,7 @@ describe('Calc insertion wizard.', function() { calcHelper.selectAllMobile(); cy.get('#copy-paste-container table td a') - .contains('some text'); + .should('have.text', 'some text'); cy.get('#copy-paste-container table td a') .should('have.attr', 'href', 'http://www.something.com'); diff --git a/cypress_test/package.json b/cypress_test/package.json index cfdad5618..c14652ccd 100644 --- a/cypress_test/package.json +++ b/cypress_test/package.json @@ -7,6 +7,7 @@ "cypress": "4.3.0", "cypress-failed-log": "2.6.2", "eslint": "6.8.0", + "eslint-plugin-cypress-rules": "file:eslint_plugin", "get-port-cli": "2.0.0", "wait-on": "4.0.0" }, _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
