diff --git a/docs/rules/no-get-raw-id.md b/docs/rules/no-get-raw-id.md new file mode 100644 index 0000000..b576967 --- /dev/null +++ b/docs/rules/no-get-raw-id.md @@ -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(); +``` diff --git a/lib/rules/no-get-raw-id.js b/lib/rules/no-get-raw-id.js new file mode 100644 index 0000000..55fbfbc --- /dev/null +++ b/lib/rules/no-get-raw-id.js @@ -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 + '()"' + }) + } + } + } + } + } +} diff --git a/test/rules/no-get-raw-id.js b/test/rules/no-get-raw-id.js new file mode 100644 index 0000000..af0ece7 --- /dev/null +++ b/test/rules/no-get-raw-id.js @@ -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()"' + } + ] + } + ] +})