-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): add 'no-absolute-url' rule
- Loading branch information
Showing
7 changed files
with
171 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Recommend against navigating to absolute URLs inside `browser.get()` or `browser.driver.get()` | ||
|
||
The rule would warn if `browser.get()` or `browser.driver.get()` are called with an absolute URL as an argument. | ||
|
||
Instead, it is recommended to have [`baseUrl` configuration option](https://github.com/angular/protractor/blob/master/docs/referenceConf.js) set and navigating to relative URLs in tests. | ||
This helps to easily switch to a different target application URL by simply changing the `baseUrl` setting (e.g. switch from dev to test or staging). | ||
|
||
## Rule details | ||
|
||
Any use of the following patterns are considered warnings: | ||
|
||
```js | ||
browser.get("http://google.com"); | ||
browser.driver.get("https://google.com"); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
browser.get("login"); | ||
browser.driver.get("account/signup"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Utility function to determine if a node is a browser.get() or browser.driver.get() call | ||
* @author Alexander Afanasyev | ||
*/ | ||
module.exports = function (node) { | ||
var object = node.callee.object | ||
var property = node.callee.property | ||
|
||
if (object && property && property.name === 'get') { | ||
var isBrowser = object.name === 'browser' | ||
var isBrowserDriver = object.object && object.object.name === 'browser' && | ||
object.property && object.property.name === 'driver' | ||
if (isBrowser || isBrowserDriver) { | ||
return { | ||
'browserGet': isBrowser, | ||
'browserDriverGet': isBrowserDriver | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict' | ||
|
||
/** | ||
* @fileoverview Recommend against navigating to absolute URLs inside `browser.get()` or `browser.driver.get()` | ||
* @author Alexander Afanasyev | ||
*/ | ||
|
||
var isBrowserGet = require('../is-browser-get') | ||
var isAbsoluteURL = new RegExp('^(?:[a-z]+:)?//', 'i') | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [] | ||
}, | ||
|
||
create: function (context) { | ||
return { | ||
'CallExpression': function (node) { | ||
if (node.arguments && node.arguments[0]) { | ||
var result = isBrowserGet(node) | ||
|
||
if (result) { | ||
var url = node.arguments[0].value | ||
|
||
if (isAbsoluteURL.test(url)) { | ||
context.report({ | ||
node: node, | ||
message: 'Unexpected absolute URL' | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict' | ||
|
||
var rule = require('../../lib/rules/no-absolute-url') | ||
var RuleTester = require('eslint').RuleTester | ||
var eslintTester = new RuleTester() | ||
|
||
eslintTester.run('no-absolute-url', rule, { | ||
valid: [ | ||
'browser.get();', | ||
'browser.driver.get();', | ||
'browser.get("mypage");', | ||
'browser.driver.get("mypage");', | ||
'browser.get("MYPAGE");', | ||
'browser.driver.get("MYPAGE");', | ||
'browser.get("/mypage/mypage");', | ||
'browser.driver.get("/mypage/mypage");' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'browser.get("http://google.com");', | ||
errors: [{ | ||
message: 'Unexpected absolute URL' | ||
}] | ||
}, | ||
{ | ||
code: 'browser.driver.get("https://google.com");', | ||
errors: [{ | ||
message: 'Unexpected absolute URL' | ||
}] | ||
}, | ||
{ | ||
code: 'browser.get("HTTP://google.com");', | ||
errors: [{ | ||
message: 'Unexpected absolute URL' | ||
}] | ||
}, | ||
{ | ||
code: 'browser.driver.get("HTTPS://google.com");', | ||
errors: [{ | ||
message: 'Unexpected absolute URL' | ||
}] | ||
}, | ||
{ | ||
code: 'browser.get("ftp://google.com");', | ||
errors: [{ | ||
message: 'Unexpected absolute URL' | ||
}] | ||
}, | ||
{ | ||
code: 'browser.driver.get("ftp://google.com");', | ||
errors: [{ | ||
message: 'Unexpected absolute URL' | ||
}] | ||
}, | ||
{ | ||
code: 'browser.get("//google.com");', | ||
errors: [{ | ||
message: 'Unexpected absolute URL' | ||
}] | ||
}, | ||
{ | ||
code: 'browser.driver.get("//google.com");', | ||
errors: [{ | ||
message: 'Unexpected absolute URL' | ||
}] | ||
} | ||
] | ||
}) |