Skip to content

Commit

Permalink
feat(rules): add 'no-get-raw-id' rule
Browse files Browse the repository at this point in the history
  • Loading branch information
alecxe committed Jun 21, 2017
1 parent d5c3b34 commit 3ede9fa
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 45 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ There are various types of rules implemented in the plugin. Here is a rough cate
* [no-array-finder-methods][]: Disallow using `ElementArrayFinder` methods on `ElementFinder`
* [array-callback-return][]: Enforce `return` statements in callbacks of `ElementArrayFinder` methods
* [no-get-inner-outer-html][]: Warn about using deprecated `getInnerHtml()` and `getOuterHtml()` methods
* [no-get-raw-id][]: Warn about using removed `getRawId()` method
* [no-promise-in-if][]: Warn if promise is checked for truthiness inside an `if` condition
#### Locating Elements
Expand Down Expand Up @@ -105,6 +106,7 @@ Rule | Default Error Level | Auto-fixable | Options
[no-array-finder-methods][] | 2 | |
[valid-locator-type][] | 2 | |
[no-compound-classes][] | 2 | |
[no-get-raw-id][] | 2 | |
[missing-wait-message][] | 1 (Warning) | |
[no-browser-sleep][] | 1 | |
[no-by-xpath][] | 1 | |
Expand Down Expand Up @@ -182,6 +184,7 @@ See [configuring rules][] for more information.
[no-browser-driver]: docs/rules/no-browser-driver.md
[valid-by-id]: docs/rules/valid-by-id.md
[valid-by-tagname]: docs/rules/valid-by-tagname.md
[no-get-raw-id]: docs/rules/no-get-raw-id.md
[configuring rules]: http://eslint.org/docs/user-guide/configuring#configuring-rules
## Recommended configuration
Expand Down
28 changes: 13 additions & 15 deletions docs/rules/no-get-raw-id.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
# Warn about using deprecated `getInnerHtml()` and `getOuterHtml()` methods
# Warn about using removed `getRawId()` method

Selenium [has deprecated `getInnerHtml()` and `getOuterHtml()` methods in version 2.53](https://github.com/SeleniumHQ/selenium/blob/96ed95a97405fa267eea09c4008cda9e7703e84d/javascript/node/selenium-webdriver/CHANGES.md#change-summary).
And, hence, Protractor itself _does not have these methods documented_ as a part of [public API](http://www.protractortest.org/#/api) anymore.
`getRawId()` method has been removed in [`Protractor` 5.0.0](https://github.com/angular/protractor/blob/ea72d5588aef983aa84705abd1ad1afa36065be7/CHANGELOG.md#500) and in [`Selenium` 3.0.0](https://github.com/SeleniumHQ/selenium/blob/427307d6e24000d7db68e8c36362fab05c477cce/javascript/node/selenium-webdriver/CHANGES.md#v300-beta-1).
Use `getId()` method instead.

## Rule details

Any use of the following patterns are considered warnings:
Any use of the following patterns are considered errors:

```js
expect(element(by.id("myid")).getInnerHtml()).toEqual("test");
expect(element(by.id("myid")).getOuterHtml()).toEqual("test");
element.all(by.css(".class")).first().getOuterHtml();
element(by.id("id")).all(by.css(".class")).last().getInnerHtml();
$$(".class").first().getOuterHtml();
$(".class").getInnerHtml().then(function (html) { console.log(html) });
expect(element(by.id("myid")).getRawId()).toEqual("id");
element.all(by.css(".class")).first().getRawId();
element(by.id("id")).all(by.css(".class")).last().getRawId();
$$(".class").first().getRawId();
$(".class").getRawId().then(function (id) { console.log(id) });
```

The following patterns are not warnings:

```js
expect(element(by.id("myid")).getText()).toEqual("test");
getInnerHtml();
var html = getOuterHtml();
elm.getInnerHTML();
elm.getOuterHTML();
expect(element(by.id("myid")).getId()).toEqual("id");
getRawId();
var html = getRawId();
elm.getRawId();
```
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ 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')
var noGetRawId = require('./lib/rules/no-get-raw-id')

module.exports = {
rules: {
Expand Down Expand Up @@ -68,7 +69,8 @@ module.exports = {
'use-count-method': useCountMethod,
'no-browser-driver': noBrowserDriver,
'valid-by-id': validById,
'valid-by-tagname': validByTagName
'valid-by-tagname': validByTagName,
'no-get-raw-id': noGetRawId
},
configs: {
recommended: {
Expand All @@ -78,6 +80,7 @@ module.exports = {
'protractor/correct-chaining': 2,
'protractor/no-invalid-selectors': 2,
'protractor/no-array-finder-methods': 2,
'protractor/no-get-raw-id': 2,
'protractor/valid-locator-type': 2,
'protractor/no-compound-classes': 2,
'protractor/missing-wait-message': 1,
Expand Down
15 changes: 14 additions & 1 deletion lib/is-element-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
* @fileoverview Utility function to determine if a node is an ElementFinder
* @author Alexander Afanasyev
*/

var isElementArrayFinder = require('./is-element-array-finder')
var elementArrayFinderGetMethods = [
'get',
'last',
'first'
]

module.exports = function (node) {
// handling $ shortcut
var callee = node.callee
if (callee) {
// handling raw $ and element
if (callee.name === '$' || callee.name === 'element') {
return true
}
Expand All @@ -19,6 +27,11 @@ module.exports = function (node) {
if (callee.property.name === '$' || callee.property.name === 'element') {
return true
}

// got element finder from element array finder
if (elementArrayFinderGetMethods.indexOf(callee.property.name) > -1 && callee.object && isElementArrayFinder(callee.object)) {
return true
}
}
}

Expand Down
18 changes: 8 additions & 10 deletions lib/rules/no-get-raw-id.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

/**
* @fileoverview Warn about using `getInnerHtml()` and `getOuterHtml()` methods
* @fileoverview Warn about using `getRawId()` method
* @author Alexander Afanasyev
*/
var isElementFinder = require('../is-element-finder')

module.exports = {
meta: {
Expand All @@ -12,17 +13,14 @@ module.exports = {

create: function (context) {
return {
'CallExpression': function (node) {
var object = node.callee.object
var property = node.callee.property
MemberExpression: function (node) {
var property = node.property

if (object && property) {
var isInnerHtml = property.name === 'getInnerHtml'
var isOuterHtml = property.name === 'getOuterHtml'
if (isInnerHtml || isOuterHtml) {
if (property && property.name === 'getRawId') {
if (node.object && isElementFinder(node.object)) {
context.report({
node: node,
message: 'Unexpected "' + property.name + '()"'
node: property,
message: 'Unexpected "' + property.name + '()". Use "getId()" instead'
})
}
}
Expand Down
44 changes: 26 additions & 18 deletions test/rules/no-get-raw-id.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,73 @@
'use strict'

var rule = require('../../lib/rules/no-get-inner-outer-html')
var rule = require('../../lib/rules/no-get-raw-id')
var RuleTester = require('eslint').RuleTester

var eslintTester = new RuleTester()

eslintTester.run('no-get-inner-outer-html', rule, {
eslintTester.run('no-get-raw-id', rule, {
valid: [
'expect(element(by.id("myid")).getText()).toEqual("test");',
'getInnerHtml();',
'var html = getOuterHtml();',
'elm.getInnerHTML();',
'expect(element(by.id("myid")).getId()).toEqual("id");',
'getRawId();',
'var html = getRawId();',
'elm.getRawId();',
'elm.getOuterHTML();'
],

invalid: [
{
code: 'expect(element(by.id("myid")).getInnerHtml()).toEqual("test");',
code: 'expect(element(by.id("myid")).getRawId()).toEqual("id");',
errors: [
{
message: 'Unexpected "getInnerHtml()"'
message: 'Unexpected "getRawId()". Use "getId()" instead'
}
]
},
{
code: 'expect(element(by.id("myid")).getOuterHtml()).toEqual("test");',
code: 'element.all(by.css(".class")).get(1).getRawId();',
errors: [
{
message: 'Unexpected "getOuterHtml()"'
message: 'Unexpected "getRawId()". Use "getId()" instead'
}
]
},
{
code: 'element.all(by.css(".class")).first().getOuterHtml();',
code: 'element.all(by.css(".class")).first().getRawId();',
errors: [
{
message: 'Unexpected "getOuterHtml()"'
message: 'Unexpected "getRawId()". Use "getId()" instead'
}
]
},
{
code: 'element(by.id("id")).all(by.css(".class")).last().getInnerHtml();',
code: 'element(by.id("id")).all(by.css(".class")).last().getRawId();',
errors: [
{
message: 'Unexpected "getInnerHtml()"'
message: 'Unexpected "getRawId()". Use "getId()" instead'
}
]
},
{
code: '$$(".class").first().getOuterHtml();',
code: 'element.all(by.css(".class")).first().$$(".myclass").last().getRawId();',
errors: [
{
message: 'Unexpected "getOuterHtml()"'
message: 'Unexpected "getRawId()". Use "getId()" instead'
}
]
},
{
code: '$(".class").getInnerHtml().then(function (html) { console.log(html) });',
code: '$$(".class").first().getRawId();',
errors: [
{
message: 'Unexpected "getInnerHtml()"'
message: 'Unexpected "getRawId()". Use "getId()" instead'
}
]
},
{
code: '$(".class").getRawId().then(function (id) { console.log(id) });',
errors: [
{
message: 'Unexpected "getRawId()". Use "getId()" instead'
}
]
}
Expand Down

0 comments on commit 3ede9fa

Please sign in to comment.