-
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.
Merge pull request #69 from eLRuLL/valid-by-tagname
feat(rules): add 'valid-by-tagname' rule
- Loading branch information
Showing
5 changed files
with
141 additions
and
1 deletion.
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,34 @@ | ||
# Prohibit use of invalid Tag Name value when using `by.tagName()` locator | ||
|
||
Ensure a valid Tag Name is being used with `by.tagName()` locator. [A valid Tag Name](https://www.w3.org/TR/html/syntax.html#tag-name) should only | ||
contain alphanumeric and cannot start with a number. | ||
|
||
This rule is very useful for notifying when an invalid Tag Name is being used. | ||
It will also prevent unintentionally putting different types of locators instead of the actual Tag name. | ||
|
||
## Rule details | ||
|
||
Any use of the following patterns are considered errors when using `by.tagName`: | ||
|
||
```js | ||
element(by.tagName("_customTagName")); | ||
element(by.tagName("div.classname")); | ||
element(by.tagName("blockquote:")); | ||
element(by.tagName("multiple tagnames")); | ||
element(by.tagName('option[value="Test"]')); | ||
element(by.tagName(" div ")); | ||
element(by.tagName("12345")); | ||
``` | ||
|
||
The following patterns are not errors: | ||
|
||
```js | ||
element(by.tagName("a")); | ||
element(by.tagName("b")); | ||
element(by.tagName("i")); | ||
element(by.tagName("A")); | ||
element(by.tagName("Area")); | ||
element(by.tagName("BlockQuote")); | ||
element(by.tagName("h1")); | ||
element(by.tagName("H1")); | ||
``` |
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,36 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Ensure tag name is valid when using `by.tagName()` locator. | ||
* @author Raul Gallegos | ||
*/ | ||
|
||
var isTagName = /^[A-Za-z][A-Za-z0-9]*$/ | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [] | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
'CallExpression': function (node) { | ||
var object = node.callee.object | ||
var property = node.callee.property | ||
|
||
var insideByTagName = object && property && object.name === 'by' && property.name === 'tagName' | ||
var argumentExists = node.arguments && node.arguments.length && node.arguments[0].value | ||
|
||
if (insideByTagName && argumentExists) { | ||
var tagName = node.arguments[0].value | ||
if (!tagName.match(isTagName)) { | ||
context.report({ | ||
node: node, | ||
message: 'Invalid TagName value: "' + tagName + '"' | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,64 @@ | ||
'use strict' | ||
|
||
var rule = require('../../lib/rules/valid-by-tagname') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
var eslintTester = new RuleTester() | ||
|
||
eslintTester.run('valid-by-tagname', rule, { | ||
valid: [ | ||
'element(by.tagName("a"));', | ||
'element(by.tagName("b"));', | ||
'element(by.tagName("i"));', | ||
'element(by.tagName("A"));', | ||
'element(by.tagName("Area"));', | ||
'element(by.tagName("BlockQuote"));', | ||
'element(by.tagName("h1"));', | ||
'element(by.tagName("H1"));' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'element(by.tagName("_customTagName"));', | ||
errors: [{ | ||
message: 'Invalid TagName value: "_customTagName"' | ||
}] | ||
}, | ||
{ | ||
code: 'element(by.tagName("div.classname"));', | ||
errors: [{ | ||
message: 'Invalid TagName value: "div.classname"' | ||
}] | ||
}, | ||
{ | ||
code: 'element(by.tagName("blockquote:"));', | ||
errors: [{ | ||
message: 'Invalid TagName value: "blockquote:"' | ||
}] | ||
}, | ||
{ | ||
code: 'element(by.tagName("multiple tagnames"));', | ||
errors: [{ | ||
message: 'Invalid TagName value: "multiple tagnames"' | ||
}] | ||
}, | ||
{ | ||
code: 'element(by.tagName(\'option[value="Test"]\'));', | ||
errors: [{ | ||
message: 'Invalid TagName value: "option[value="Test"]"' | ||
}] | ||
}, | ||
{ | ||
code: 'element(by.tagName(" div "));', | ||
errors: [{ | ||
message: 'Invalid TagName value: " div "' | ||
}] | ||
}, | ||
{ | ||
code: 'element(by.tagName("12345"));', | ||
errors: [{ | ||
message: 'Invalid TagName value: "12345"' | ||
}] | ||
} | ||
] | ||
}) |