Skip to content

Commit

Permalink
feat(rules): add 'valid-by-id' rule
Browse files Browse the repository at this point in the history
  • Loading branch information
eLRuLL committed Jan 19, 2017
1 parent 8dfdc41 commit 189116f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var validLocatorType = require('./lib/rules/valid-locator-type')
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')

module.exports = {
rules: {
Expand Down Expand Up @@ -64,7 +65,8 @@ module.exports = {
'valid-locator-type': validLocatorType,
'no-compound-classes': noCompoundClasses,
'use-count-method': useCountMethod,
'no-browser-driver': noBrowserDriver
'no-browser-driver': noBrowserDriver,
'valid-by-id': validById
},
configs: {
recommended: {
Expand All @@ -76,6 +78,7 @@ module.exports = {
'protractor/no-array-finder-methods': 2,
'protractor/valid-locator-type': 2,
'protractor/no-compound-classes': 2,
'protractor/valid-by-id': 2,
'protractor/missing-wait-message': 1,
'protractor/no-browser-sleep': 1,
'protractor/no-by-xpath': 1,
Expand Down
34 changes: 34 additions & 0 deletions lib/rules/valid-by-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

/**
* @fileoverview asdads
* @author Raul Gallegos
*/

var isHtmlID = /^[A-Za-z]+[\w\-:.]*$/

module.exports = {
meta: {
schema: []
},

create: function (context) {
return {
'CallExpression': function (node) {
var object = node.callee.object
var property = node.callee.property
var insideById = object && property && object.name === 'by' && property.name === 'id'
var argumentExists = node.arguments && node.arguments.length && node.arguments[0].value
if (insideById && argumentExists) {
var idValue = node.arguments[0].value
if (!idValue.match(isHtmlID)) {
context.report({
node: node,
message: 'Invalid ID value: "' + idValue + '"'
})
}
}
}
}
}
}
59 changes: 59 additions & 0 deletions test/rules/valid-by-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict'

var rule = require('../../lib/rules/valid-by-id')
var RuleTester = require('eslint').RuleTester

var eslintTester = new RuleTester()

eslintTester.run('valid-by-id', rule, {
valid: [
'element(by.id("validID"));',
'element(by.css(".myclass"));',
'element.all(by.id("simpleid"));',
'element.all(by.id("foo-bar"));',
'element.all(by.id("foo:bar"));',
'element.all(by.id("foo_bar"));',
'element.all(by.id("foo.bar"));',
'element.all(by.id("a123456789"));',
'element.all(by.id("a1-_:.r2D2"));'
],

invalid: [
{
code: 'element(by.id("#id"));',
errors: [{
message: 'Invalid ID value: "#id"'
}]
},
{
code: 'element(by.id("1startwithnumber"));',
errors: [{
message: 'Invalid ID value: "1startwithnumber"'
}]
},
{
code: 'element(by.id("_"));',
errors: [{
message: 'Invalid ID value: "_"'
}]
},
{
code: 'element(by.id("#"));',
errors: [{
message: 'Invalid ID value: "#"'
}]
},
{
code: 'element(by.id("invalid*id"));',
errors: [{
message: 'Invalid ID value: "invalid*id"'
}]
},
{
code: 'element(by.id("id with spaces"));',
errors: [{
message: 'Invalid ID value: "id with spaces"'
}]
}
]
})

0 comments on commit 189116f

Please sign in to comment.