-
Notifications
You must be signed in to change notification settings - Fork 21
/
validate-html-markup.js
49 lines (43 loc) · 1.56 KB
/
validate-html-markup.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
42
43
44
45
46
47
48
49
/**
* Validate HTML Markup
*
* Validates the current page's HTML markup contains certain markup
*
* Parameters
*
* element (HTML)
*
* expectedMarkup (JS)
*
* matchType [optional] : Textual match type when validating HTML markup
* Examples: exact, startswith, endswith, contains (default), includes
*
* Base Step Type
*
* Custom Validation
*/
/* eslint-disable no-global-assign */
/* globals document, element, element, expectedMarkup, matchType */
if (typeof element === 'undefined' || element === null) {
element = document.documentElement;
}
if (typeof expectedMarkup === 'undefined' || expectedMarkup === null) {
throw new Error("Error: expectedMarkup is undefined");
}
let matchtype = "contains";
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](element.innerHTML.toString(), expectedMarkup) === false) {
throw new Error("Expected: '" + expectedMarkup + "', Actual: '" + element.innerHTML.toString() +"', MatchType: " + matchtype);
}