-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rule disallow usage of cypress-xpath
- Loading branch information
Showing
5 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Disallow usage of cypress xpath (`no-xpath`) | ||
|
||
This rule disallow the usage of cypress-xpath for selecting elements. | ||
|
||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
|
||
cy.xpath('//div[@class=\"container\"]').click() | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
|
||
```js | ||
|
||
cy.get('[data-cy="container"]').click(); | ||
``` | ||
|
||
|
||
## Further Reading | ||
|
||
`cypress-xpath` package has been deprecated since Oct 13, 2022. | ||
|
||
|
||
See [the Cypress Best Practices guide](https://docs.cypress.io/guides/references/best-practices.html#Selecting-Elements). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: "disallow usage of xpath in selector", | ||
recommended: false, | ||
url: "https://github.com/cypress-io/eslint-plugin-cypress/blob/master/docs/rules/no-xpath.md" | ||
}, | ||
fixable: null, // Or `code` or `whitespace` | ||
schema: [], // Add a schema if the rule has options | ||
messages: { | ||
unexpected: 'avoid using cypress xpath', | ||
}, | ||
}, | ||
|
||
create (context) { | ||
return { | ||
CallExpression (node) { | ||
if (isCallingCyXpath(node)) { | ||
context.report({ node, messageId: 'unexpected' }) | ||
} | ||
}, | ||
} | ||
}, | ||
}; | ||
|
||
function isCallingCyXpath (node) { | ||
return node.callee.type === 'MemberExpression' && | ||
node.callee.object.type === 'Identifier' && | ||
node.callee.object.name === 'cy' && | ||
node.callee.property.type === 'Identifier' && | ||
node.callee.property.name === 'xpath' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/no-xpath"), | ||
RuleTester = require("eslint").RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester(); | ||
ruleTester.run("no-xpath", rule, { | ||
valid: [ | ||
{ code: 'cy.wait("@someRequest")' }, | ||
{ code: 'cy.get("button").click({force: true})' }, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: "cy.xpath('//div[@class=\"container\"]/p[1]').click()", | ||
errors: [{ messageId: "unexpected" }], | ||
}, | ||
{ | ||
code: "cy.xpath('//p[1]').should('exist')", | ||
errors: [{ messageId: "unexpected" }] | ||
} | ||
], | ||
}); |