Skip to content

Commit

Permalink
feat(rules): add 'valid-by-tagname' rule
Browse files Browse the repository at this point in the history
  • Loading branch information
eLRuLL committed Jan 21, 2017
1 parent 34368a7 commit 05b9b50
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var noCompoundClasses = require('./lib/rules/no-compound-classes')
var useCountMethod = require('./lib/rules/use-count-method')
var noBrowserDriver = require('./lib/rules/no-browser-driver')
var validById = require('./lib/rules/valid-by-id')
var validByTagName = require('./lib/rules/valid-by-tagname')

module.exports = {
rules: {
Expand Down Expand Up @@ -66,7 +67,8 @@ module.exports = {
'no-compound-classes': noCompoundClasses,
'use-count-method': useCountMethod,
'no-browser-driver': noBrowserDriver,
'valid-by-id': validById
'valid-by-id': validById,
'valid-by-tagname': validByTagName
},
configs: {
recommended: {
Expand Down
159 changes: 159 additions & 0 deletions lib/rules/valid-by-tagname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
'use strict'

/**
* @fileoverview Ensure tag name is html valid when using `by.tagName()` locator.
* @author Raul Gallegos
*/

var validHTMLTagNames = [
'a',
'abbr',
'acronym',
'address',
'applet',
'area',
'article',
'aside',
'audio',
'b',
'base',
'basefont',
'bdi',
'bdo',
'big',
'blockquote',
'body',
'br',
'button',
'canvas',
'caption',
'center',
'cite',
'code',
'col',
'colgroup',
'datalist',
'dd',
'del',
'details',
'dfn',
'dialog',
'dir',
'div',
'dl',
'dt',
'em',
'embed',
'fieldset',
'figcaption',
'figure',
'font',
'footer',
'form',
'frame',
'frameset',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'head',
'header',
'hr',
'html',
'i',
'iframe',
'img',
'input',
'ins',
'kbd',
'keygen',
'label',
'legend',
'li',
'link',
'main',
'map',
'mark',
'menu',
'menuitem',
'meta',
'meter',
'nav',
'noframes',
'noscript',
'object',
'ol',
'optgroup',
'option',
'output',
'p',
'param',
'picture',
'pre',
'progress',
'q',
'rp',
'rt',
'ruby',
's',
'samp',
'script',
'section',
'select',
'small',
'source',
'span',
'strike',
'strong',
'style',
'sub',
'summary',
'sup',
'table',
'tbody',
'td',
'textarea',
'tfoot',
'th',
'thead',
'time',
'title',
'tr',
'track',
'tt',
'u',
'ul',
'var',
'video',
'wbr'
]

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 (validHTMLTagNames.indexOf(tagName.toLowerCase()) === -1) {
context.report({
node: node,
message: 'Invalid TagName value: "' + tagName + '"'
})
}
}
}
}
}
}
58 changes: 58 additions & 0 deletions test/rules/valid-by-tagname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'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("divs"));',
errors: [{
message: 'Invalid TagName value: "divs"'
}]
},
{
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 "'
}]
}
]
})

0 comments on commit 05b9b50

Please sign in to comment.