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 18dcec5 commit d5c3b34
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/rules/no-get-raw-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Warn about using deprecated `getInnerHtml()` and `getOuterHtml()` methods

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.

## Rule details

Any use of the following patterns are considered warnings:

```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) });
```

The following patterns are not warnings:

```js
expect(element(by.id("myid")).getText()).toEqual("test");
getInnerHtml();
var html = getOuterHtml();
elm.getInnerHTML();
elm.getOuterHTML();
```
32 changes: 32 additions & 0 deletions lib/rules/no-get-raw-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

/**
* @fileoverview Warn about using `getInnerHtml()` and `getOuterHtml()` methods
* @author Alexander Afanasyev
*/

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

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

if (object && property) {
var isInnerHtml = property.name === 'getInnerHtml'
var isOuterHtml = property.name === 'getOuterHtml'
if (isInnerHtml || isOuterHtml) {
context.report({
node: node,
message: 'Unexpected "' + property.name + '()"'
})
}
}
}
}
}
}
67 changes: 67 additions & 0 deletions test/rules/no-get-raw-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

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

var eslintTester = new RuleTester()

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

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

0 comments on commit d5c3b34

Please sign in to comment.