-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): add 'valid-locator-type' rule
- Loading branch information
Showing
7 changed files
with
349 additions
and
13 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
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,42 @@ | ||
# Ensure correct locator argument type for `element()`, `element.all()`, `$()` and `$$()` | ||
|
||
This rule would warn if there is a incorrect "by" locator type passed to `element()` and `element.all()`. | ||
Additionally, this rule would warn if there is a "by"-type locator passed to `$()` and `$$()` shortcuts. | ||
|
||
## Rule details | ||
|
||
Any use of the following patterns are considered errors: | ||
|
||
```js | ||
element(".class"); | ||
element.all(".class"); | ||
$(by.css(".class")); | ||
$$(by.css(".class")); | ||
element(by.css(".class1")).element(".class2"); | ||
element(by.css(".class1")).all(".class2"); | ||
element.all(by.css(".class1")).all(".class2"); | ||
$(".class1").all(".class2"); | ||
$(".class1").element(".class2"); | ||
$$(".class1").all(".class2"); | ||
$(".class1").$(by.css(".class2")); | ||
$(".class1").$$(by.css(".class2")); | ||
$$(".class1").$$(by.css(".class2")); | ||
``` | ||
|
||
The following patterns are not errors: | ||
|
||
```js | ||
element(by.css(".class")); | ||
element.all(by.css(".class")); | ||
$(".class"); | ||
$$(".class"); | ||
element(by.css(".class1")).element(by.css(".class2")); | ||
element(by.css(".class1")).all(by.css(".class2")); | ||
element.all(by.css(".class1")).all(by.css(".class2")); | ||
$(".class1").all(by.css(".class2")); | ||
$(".class1").element(by.css(".class2")); | ||
$$(".class1").all(by.css(".class2")); | ||
$(".class1").$(".class2"); | ||
$(".class1").$$(".class2"); | ||
$$(".class1").$$(".class2"); | ||
``` |
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
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,128 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Ensure correct locator argument type for `element()`, `element.all()`, `$()` and `$$()` | ||
* @author Alexander Afanasyev | ||
*/ | ||
|
||
var isElementFinder = require('../is-element-finder') | ||
var isElementArrayFinder = require('../is-element-array-finder') | ||
|
||
// The first four "is" functions below are simple helper functions that help to distinguish between `$` vs `element`, and `$$` vs `element.all` | ||
|
||
/** | ||
* Checks if a given node is an ElementFinder represented by element() callee - both nested and not | ||
* | ||
* @param {ASTNode} node - A node to check. | ||
* @returns {boolean} | ||
*/ | ||
function isElement (node) { | ||
return node.callee.name === 'element' || (node.callee.property && node.callee.property.name === 'element') | ||
} | ||
|
||
/** | ||
* Checks if a given node is an ElementArrayFinder represented by all() callee - both nested and not | ||
* | ||
* @param {ASTNode} node - A node to check. | ||
* @returns {boolean} | ||
*/ | ||
function isElementAll (node) { | ||
return node.callee.property && node.callee.property.name === 'all' | ||
} | ||
|
||
/** | ||
* Checks if a given node is an ElementFinder represented by $() callee - both nested and not | ||
* | ||
* @param {ASTNode} node - A node to check. | ||
* @returns {boolean} | ||
*/ | ||
function is$ (node) { | ||
return node.callee.name === '$' || (node.callee.property && node.callee.property.name === '$') | ||
} | ||
|
||
/** | ||
* Checks if a given node is an ElementArrayFinder represented by $$() callee - both nested and not | ||
* | ||
* @param {ASTNode} node - A node to check. | ||
* @returns {boolean} | ||
*/ | ||
function is$$ (node) { | ||
return node.callee.name === '$$' || (node.callee.property && node.callee.property.name === '$$') | ||
} | ||
|
||
/** | ||
* Checks if a given CallExpression node has the first literal argument | ||
* | ||
* @param {ASTNode} node - A node to check. | ||
* @returns {boolean} | ||
*/ | ||
function isArgumentLiteral (node) { | ||
return node.arguments && node.arguments[0].type === 'Literal' | ||
} | ||
|
||
/** | ||
* Checks if a given CallExpression node has the argument the "by" expression | ||
* | ||
* @param {ASTNode} node - A node to check. | ||
* @returns {boolean} | ||
*/ | ||
function isArgumentByLocator (node) { | ||
if (node.arguments && node.arguments[0].type === 'CallExpression') { | ||
var argument = node.arguments[0] | ||
if (argument.callee && argument.callee.object && argument.callee.object.name === 'by') { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [] | ||
}, | ||
|
||
// TODO: need to make isElementFinder and isElementArrayFinder universal - handling both Member and Call expressions | ||
create: function (context) { | ||
return { | ||
CallExpression: function (node) { | ||
// element finders | ||
if (isElementFinder(node)) { | ||
// handle element | ||
if (isElement(node) && isArgumentLiteral(node)) { | ||
context.report({ | ||
node: node.arguments[0], | ||
message: 'Invalid locator type.' | ||
}) | ||
} | ||
|
||
// handle $ | ||
if (is$(node) && isArgumentByLocator(node)) { | ||
context.report({ | ||
node: node.arguments[0], | ||
message: 'Invalid locator type.' | ||
}) | ||
} | ||
} | ||
|
||
// element array finders | ||
if (isElementArrayFinder(node)) { | ||
// handle element.all | ||
if (isElementAll(node) && isArgumentLiteral(node)) { | ||
context.report({ | ||
node: node.arguments[0], | ||
message: 'Invalid locator type.' | ||
}) | ||
} | ||
|
||
// handle $$ | ||
if (is$$(node) && isArgumentByLocator(node)) { | ||
context.report({ | ||
node: node.arguments[0], | ||
message: 'Invalid locator type.' | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.