-
Notifications
You must be signed in to change notification settings - Fork 21
/
validate-current-url.js
41 lines (36 loc) · 1.29 KB
/
validate-current-url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Validate Current URL
*
* Validates the current page's URL
*
* Parameters
*
* expectedURL (JS)
*
* matchType [optional] : Textual match type when validating URL
* Examples: exact, startswith (default), endswith, includes
*
* Step Type
*
* Custom Validation
*/
/* globals window, expectedURL,matchType */
if (typeof expectedURL === 'undefined' || expectedURL === null) {
throw new Error("Error: expectedURL is undefined");
}
let matchtype = "startswith";
if (typeof matchType !== 'undefined' && matchType !== null)
matchtype = matchType.toLowerCase();
/* Convenience functions used for matching
*/
const stringMatch = {};
stringMatch['exact'] = function (str1, str2) { return (str1 === str2); };
stringMatch['startswith'] = function (str1, str2) { return str1.startsWith(str2); };
stringMatch['endswith'] = function (str1, str2) { return str1.endsWith(str2); };
stringMatch['includes'] = function (str1, str2) { return str1.includes(str2); };
stringMatch['contains'] = function (str1, str2) { return str1.includes(str2); };
/* Validate
*/
if (stringMatch[matchtype](window.location.href.toString(), expectedURL) === false) {
throw new Error("Expected: '" + expectedURL + "', Actual: '" + window.location.href +"', MatchType: " + matchtype);
}