-
Notifications
You must be signed in to change notification settings - Fork 21
/
is-visible.js
61 lines (56 loc) · 1.92 KB
/
is-visible.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
50
51
52
53
54
55
56
57
58
59
60
61
/**
* isVisible
*
* Vaildate that an element is visible (or not visible)
*
* Parameters
*
* element (HTML)
* isVisible (JS) - Element visible/not visible flag true/false. Default (true).
*
* Base Step
* Custom Validation
*
* Disclaimer
* This Custom Action is provided "AS IS". It is for instructional purposes only and is not officially supported by Testim
*
**/
/* Validate required parameters
*/
if (typeof (element) === 'undefined')
throw new Error("Target element has not been specified.");
function _isVisible(element) {
while (element) {
if (element === document) {
return true;
}
var $style = window.getComputedStyle(element, null);
if (!element) {
return false;
} else if (!$style) {
return false;
} else if ($style.display === 'none') {
return false;
} else if ($style.visibility === 'hidden') {
return false;
} else if (+$style.opacity === 0) {
return false;
} else if (($style.display === 'block' || $style.display === 'inline-block') &&
$style.height === '0px' && $style.overflow === 'hidden') {
return false;
} else if (element.offsetWidth === 0 && element.offsetHeight === 0) {
return false;
}
else {
return $style.position === 'fixed' || _isVisible(element.parentNode);
}
}
}
let expected_value = (typeof (isVisible) !== 'undefined' && isVisible !== null) ? isVisible : true;
let actual_value = (element === null) ? false : _isVisible(element);
console.log("Element visible is " + actual_value + " when it should be " + expected_value);
if (actual_value === expected_value)
return true;
else {
throw new Error("Element is " + ((actual_value) ? "VISIBLE" : "NOTVISIBLE") + " when it should be " + ((expected_value) ? "VISIBLE" : "NOTVISIBLE"));
}