Skip to content

Commit 0ad518e

Browse files
committed
feat(rules): add 'no-get-inner-outer-html' rule
1 parent 123a673 commit 0ad518e

File tree

5 files changed

+132
-1
lines changed

5 files changed

+132
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Rule | Default Error Level | Auto-fixable | Options
5656
[no-promise-in-if][] | 1 | |
5757
[no-execute-script][] | 1 | | requires plugin "settings"
5858
[no-repetitive-locators][] | 1 | |
59+
[no-get-inner-outer-html][] | 1 | |
5960
[by-css-shortcut][] | 0 | |
6061

6162
For example, the `missing-perform` rule is enabled by default and will cause
@@ -97,6 +98,7 @@ See [configuring rules][] for more information.
9798
[no-execute-script]: docs/rules/no-execute-script.md
9899
[correct-chaining]: docs/rules/correct-chaining.md
99100
[no-repetitive-locators]: docs/rules/no-repetitive-locators.md
101+
[no-get-inner-outer-html]: docs/rules/no-get-inner-outer-html.md
100102
[configuring rules]: http://eslint.org/docs/user-guide/configuring#configuring-rules
101103

102104
## Recommended configuration
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Warn about using `getInnerHtml()` and `getOuterHtml()` methods
2+
3+
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).
4+
And, hence, Protractor itself _does not have these methods documented_ as a part of [public API](http://www.protractortest.org/#/api) anymore.
5+
6+
## Rule details
7+
8+
Any use of the following patterns are considered warnings:
9+
10+
```js
11+
expect(element(by.id("myid")).getInnerHtml()).toEqual("test");
12+
expect(element(by.id("myid")).getOuterHtml()).toEqual("test");
13+
element.all(by.css(".class")).first().getOuterHtml();
14+
element(by.id("id")).all(by.css(".class")).last().getInnerHtml();
15+
$$(".class").first().getOuterHtml();
16+
$(".class").getInnerHtml().then(function (html) { console.log(html) });
17+
```
18+
19+
The following patterns are not warnings:
20+
21+
```js
22+
expect(element(by.id("myid")).getText()).toEqual("test");
23+
getInnerHtml();
24+
var html = getOuterHtml();
25+
elm.getInnerHTML();
26+
elm.getOuterHTML();
27+
```

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var noPromiseInIf = require('./lib/rules/no-promise-in-if')
2121
var noExecuteScript = require('./lib/rules/no-execute-script')
2222
var correctChaining = require('./lib/rules/correct-chaining')
2323
var noRepetitiveLocators = require('./lib/rules/no-repetitive-locators')
24+
var noGetInnerOuterHtml = require('./lib/rules/no-get-inner-outer-html')
2425

2526
module.exports = {
2627
rules: {
@@ -44,7 +45,8 @@ module.exports = {
4445
'no-promise-in-if': noPromiseInIf,
4546
'no-execute-script': noExecuteScript,
4647
'no-repetitive-locators': noRepetitiveLocators,
47-
'correct-chaining': correctChaining
48+
'correct-chaining': correctChaining,
49+
'no-get-inner-outer-html': noGetInnerOuterHtml
4850
},
4951
configs: {
5052
recommended: {
@@ -69,6 +71,7 @@ module.exports = {
6971
'protractor/no-promise-in-if': 1,
7072
'protractor/no-execute-script': 1,
7173
'protractor/no-repetitive-locators': 1,
74+
'protractor/no-get-inner-outer-html': 1,
7275
'protractor/by-css-shortcut': 0
7376
},
7477
globals: {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
/**
4+
* @fileoverview Warn about using `getInnerHtml()` and `getOuterHtml()` methods
5+
* @author Alexander Afanasyev
6+
*/
7+
8+
module.exports = {
9+
meta: {
10+
schema: []
11+
},
12+
13+
create: function (context) {
14+
return {
15+
'CallExpression': function (node) {
16+
var object = node.callee.object
17+
var property = node.callee.property
18+
19+
if (object && property) {
20+
var isInnerHtml = property.name === 'getInnerHtml'
21+
var isOuterHtml = property.name === 'getOuterHtml'
22+
if (isInnerHtml || isOuterHtml) {
23+
context.report({
24+
node: node,
25+
message: 'Unexpected "' + property.name + '()"'
26+
})
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict'
2+
3+
var rule = require('../../lib/rules/no-get-inner-outer-html')
4+
var RuleTester = require('eslint').RuleTester
5+
6+
var eslintTester = new RuleTester()
7+
8+
eslintTester.run('no-get-inner-outer-html', rule, {
9+
valid: [
10+
'expect(element(by.id("myid")).getText()).toEqual("test");',
11+
'getInnerHtml();',
12+
'var html = getOuterHtml();',
13+
'elm.getInnerHTML();',
14+
'elm.getOuterHTML();'
15+
],
16+
17+
invalid: [
18+
{
19+
code: 'expect(element(by.id("myid")).getInnerHtml()).toEqual("test");',
20+
errors: [
21+
{
22+
message: 'Unexpected "getInnerHtml()"'
23+
}
24+
]
25+
},
26+
{
27+
code: 'expect(element(by.id("myid")).getOuterHtml()).toEqual("test");',
28+
errors: [
29+
{
30+
message: 'Unexpected "getOuterHtml()"'
31+
}
32+
]
33+
},
34+
{
35+
code: 'element.all(by.css(".class")).first().getOuterHtml();',
36+
errors: [
37+
{
38+
message: 'Unexpected "getOuterHtml()"'
39+
}
40+
]
41+
},
42+
{
43+
code: 'element(by.id("id")).all(by.css(".class")).last().getInnerHtml();',
44+
errors: [
45+
{
46+
message: 'Unexpected "getInnerHtml()"'
47+
}
48+
]
49+
},
50+
{
51+
code: '$$(".class").first().getOuterHtml();',
52+
errors: [
53+
{
54+
message: 'Unexpected "getOuterHtml()"'
55+
}
56+
]
57+
},
58+
{
59+
code: '$(".class").getInnerHtml().then(function (html) { console.log(html) });',
60+
errors: [
61+
{
62+
message: 'Unexpected "getInnerHtml()"'
63+
}
64+
]
65+
}
66+
]
67+
})

0 commit comments

Comments
 (0)