-
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 'bare-element-finders' rule
- Loading branch information
Showing
8 changed files
with
260 additions
and
18 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,43 @@ | ||
# Warn if a bare ElementFinder or ElementArrayFinder is declared with no applied action | ||
|
||
This rule is inspired by [this problem](https://stackoverflow.com/questions/44841276/e2e-testing-in-protractor-signin) when there was an `ElementFinder` declared but no action was applied. | ||
|
||
In this case, Protractor would not actually even try to locate the element, because [according to documentation](http://www.protractortest.org/#/locators#actions): | ||
|
||
> The ElementFinder knows how to locate the DOM element using the locator you passed in as a parameter, but it has not actually done so yet. | ||
> It will not contact the browser until an action method has been called. | ||
It is easy to forget to actually call an ElementFinder method and the rule would be handy in these kind of cases. | ||
|
||
## Rule details | ||
|
||
|
||
Any use of the following patterns are considered warnings: | ||
|
||
```js | ||
element(by.id('signin_submit_btn')); | ||
element.all(by.className('myClass')); | ||
element.all(by.css(".class")).get(0); | ||
$(".class"); | ||
$$(".class").first(); | ||
element.all(by.css(".class")).last(); | ||
element(by.css(".class1")).element(by.css(".class2")); | ||
element(by.css(".class1")).$(".class2"); | ||
$$(".class1").first().element(by.css(".class2")); | ||
$(".class1").$(".class2"); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
element(by.id('signin_submit_btn')).click(); | ||
element.all(by.className('myClass')).first().click(); | ||
element.all(by.css(".class")).get(0).sendKeys("test"); | ||
$(".class").sendKeys("test"); | ||
$$(".class").first().sendKeys("test"); | ||
element.all(by.css(".class")).last().click(); | ||
element(by.css(".class1")).element(by.css(".class2")).click(); | ||
element(by.css(".class1")).$(".class2").sendKeys("test"); | ||
$$(".class1").first().element(by.css(".class2")).sendKeys("test"); | ||
$(".class1").$(".class2").sendKeys("test"); | ||
``` |
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,39 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Warn if a bare ElementFinder or ElementArrayFinder is declared with no applied action | ||
* @author Alexander Afanasyev | ||
*/ | ||
|
||
var isElementFinder = require('../is-element-finder') | ||
var isElementArrayFinder = require('../is-element-array-finder') | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [] | ||
}, | ||
|
||
create: function (context) { | ||
function isBareExpression (node) { | ||
return node.parent.type === 'ExpressionStatement' | ||
} | ||
|
||
return { | ||
CallExpression: function (node) { | ||
var isNodeElementFinder = isElementFinder(node) | ||
var isNodeElementArrayFinder = isElementArrayFinder(node) | ||
|
||
if (isNodeElementFinder || isNodeElementArrayFinder) { | ||
if (isBareExpression(node)) { | ||
var target = isNodeElementFinder ? 'ElementFinder' : 'ElementArrayFinder' | ||
|
||
context.report({ | ||
node: node, | ||
message: 'Bare ' + target + ' with no applied action detected.' | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,157 @@ | ||
'use strict' | ||
|
||
var rule = require('../../lib/rules/bare-element-finders') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
var eslintTester = new RuleTester() | ||
|
||
eslintTester.run('bare-element-finders', rule, { | ||
valid: [ | ||
'', | ||
'all()', | ||
'test.all()', | ||
'$$', | ||
'var myElement = element(by.id("signin_submit_btn"));', | ||
'var myElements = element.all(by.className("myClass"));', | ||
'var myElement = $("#signin_submit_btn");', | ||
'var myElements = $$(".myClass");', | ||
'element(by.id("signin_submit_btn")).click();', | ||
'element.all(by.className("myClass")).first().click();', | ||
'element.all(by.css(".class")).get(0).sendKeys("test");', | ||
'$(".class").sendKeys("test");', | ||
'$$(".class").first().sendKeys("test");', | ||
'element.all(by.css(".class")).last().click();', | ||
'element(by.css(".class1")).element(by.css(".class2")).click();', | ||
'element(by.css(".class1")).$(".class2").sendKeys("test");', | ||
'$$(".class1").first().element(by.css(".class2")).sendKeys("test");', | ||
'$(".class1").$(".class2").sendKeys("test");', | ||
'function test() { return q.all([promise1, promise2]); }', | ||
'function test() { return element(by.id("signin_submit_btn")); }', | ||
'function test() { return element.all(by.css("class1")); }', | ||
'function test() { return $$("class1"); }', | ||
'function test() { return $("class1"); }' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'element(by.id("signin_submit_btn"));', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'function test() { element(by.id("signin_submit_btn")); }', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element.all(by.className("myClass"));', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementArrayFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element.all(by.css(".class")).get(0);', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '$(".class");', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '$$(".class").first();', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element.all(by.css(".class")).last();', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element(by.css(".class1")).element(by.css(".class2"));', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element(by.css(".class1")).$(".class2");', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '$$(".class1").first().element(by.css(".class2"));', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '$(".class1").$(".class2");', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '$(".class1").$$(".class2");', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementArrayFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: '$$(".class1").$$(".class2").all(by.className("class3"));', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementArrayFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element.all(by.className("class3")).$$(".class4");', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementArrayFinder with no applied action detected.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'element(by.id("id1")).element(by.id("id2")).$$(".class4");', | ||
errors: [ | ||
{ | ||
message: 'Bare ElementArrayFinder with no applied action detected.' | ||
} | ||
] | ||
} | ||
] | ||
}) |