forked from applitools/Eyes.Selenium.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameChain.js
More file actions
146 lines (125 loc) · 3.87 KB
/
FrameChain.js
File metadata and controls
146 lines (125 loc) · 3.87 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
(function() {
'use strict';
var EyesSDK = require('eyes.sdk'),
EyesUtils = require('eyes.utils'),
Frame = require('./Frame');
var ArgumentGuard = EyesUtils.ArgumentGuard,
GeometryUtils = EyesUtils.GeometryUtils;
/**
* Creates a new frame chain.
* @param {Object} logger A Logger instance.
* @param {FrameChain} other A frame chain from which the current frame chain will be created.
*/
function FrameChain(logger, other) {
ArgumentGuard.notNull(logger, "logger");
this._logger = logger;
if (other && other instanceof FrameChain) {
this._logger.verbose("Frame chain copy constructor (size " + other.size() + ")");
this._frames = [];
for (var i = 0, l = other.size(); i < l; i++) {
this._frames.push(new Frame(logger,
other.getFrames()[i].getReference(),
other.getFrames()[i].getId(),
other.getFrames()[i].getLocation(),
other.getFrames()[i].getSize(),
other.getFrames()[i].getParentScrollPosition())
);
}
this._logger.verbose("Done!");
} else {
this._frames = [];
}
}
/**
* Compares two frame chains.
* @param {FrameChain} c1 Frame chain to be compared against c2.
* @param {FrameChain} c2 Frame chain to be compared against c1.
* @return {boolean} True if both frame chains represent the same frame, false otherwise.
*/
FrameChain.isSameFrameChain = function (c1, c2) {
var lc1 = c1.size();
var lc2 = c2.size();
// different chains size means different frames
if (lc1 != lc2) {
return false;
}
for(var i = 0; i < lc1; ++i) {
if (c1.getFrames()[i].getId() != c1.getFrames()[i].getId()) {
return false;
}
}
return true;
};
/**
* @return {Array.<Frame>} frames stored in chain
*/
FrameChain.prototype.getFrames = function () {
return this._frames;
};
/**
* @param {int} index Index of needed frame
* @return {Frame} frame by index in array
*/
FrameChain.prototype.getFrame = function (index) {
if (this._frames.length > index) {
return this._frames[index];
}
throw new Error("No frames for given index");
};
/**
*
* @return {int} The number of frames in the chain.
*/
FrameChain.prototype.size = function () {
return this._frames.length;
};
/**
* Removes all current frames in the frame chain.
*/
FrameChain.prototype.clear = function () {
return this._frames = [];
};
/**
* Removes the last inserted frame element. Practically means we switched
* back to the parent of the current frame
*/
FrameChain.prototype.pop = function () {
return this._frames.pop();
};
/**
* Appends a frame to the frame chain.
* @param {Frame} frame The frame to be added.
*/
FrameChain.prototype.push = function (frame) {
return this._frames.push(frame);
};
/**
* @return {{x: number, y: number}} The location of the current frame in the page.
*/
FrameChain.prototype.getCurrentFrameOffset = function () {
var result = {x: 0, y: 0};
for (var i = 0, l = this._frames.length; i < l; i++) {
result = GeometryUtils.locationOffset(result, this._frames[i].getLocation());
}
return result;
};
/**
* @return {{x: number, y: number}} The outermost frame's location, or NoFramesException.
*/
FrameChain.prototype.getDefaultContentScrollPosition = function () {
if (this._frames.length == 0) {
throw new Error("No frames in frame chain");
}
return this._frames[0].getParentScrollPosition();
};
/**
* @return {{width: number, height: number}} The size of the current frame.
*/
FrameChain.prototype.getCurrentFrameSize = function () {
this._logger.verbose("getCurrentFrameSize()");
var result = this._frames[this._frames.length - 1].getSize();
this._logger.verbose("Done!");
return result;
};
module.exports = FrameChain;
}());