This repository was archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathElement.js
More file actions
225 lines (201 loc) · 8.85 KB
/
Element.js
File metadata and controls
225 lines (201 loc) · 8.85 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import Helpers from "./helpers";
const path = require("path");
const config = require(process.cwd() + "/test-juggler.config");
const defaultTimeout = config.defaultTimeout;
const shortTimeout = config.shortTimeout;
export default class Element {
constructor(selector, elementPage = page) {
this.selector = selector,
this.page = elementPage
}
newChildElement(childSelector) {
var isXPathSlector = (selector) => selector.startsWith("//");
if (isXPathSlector(this.selector) != isXPathSlector(childSelector)) {
throw "Cannot combine different selectors types!";
}
return new Element(`${this.selector} ${childSelector}`);
}
async wait(timeout = defaultTimeout) {
console.log(`Waiting for ${this.selector} ...`);
const elementHandle = await this.page.waitForSelector(this.selector, { timeout: timeout });
if (config.captureScreenshots) {
await Helpers.takeScreenshot();
}
return elementHandle;
}
async waitUntilVisible(timeout = defaultTimeout) {
console.log(`Waiting for ${this.selector} to be visible...`);
const elementHandle = await this.page.waitForSelector(this.selector, { state: "visible", timeout: timeout });
if (config.captureScreenshots) {
await Helpers.takeScreenshot();
}
return elementHandle;
}
async waitUntilInvisible(timeout = defaultTimeout) {
console.log(`Waiting for ${this.selector} to be invisible...`);
await this.page.waitForSelector(this.selector, { state: "hidden", timeout: timeout });
if (config.captureScreenshots) {
await Helpers.takeScreenshot();
}
}
async getCoordinates(xOffset = null, yOffset = null) {
const elementHandle = await this.wait();
const rect = await elementHandle.boundingBox();
const xCoordinate = xOffset !== null ? xOffset : rect.width / 2;
const yCoordinate = yOffset !== null ? yOffset : rect.height / 2;
console.log(`Element width: ${rect.width}, height: ${rect.height}`);
console.log(`Action on element rectangle at position x: ${xCoordinate}, y: ${yCoordinate}`);
return { x: xCoordinate, y: yCoordinate };
}
async click(xOffset = null, yOffset = null) {
console.log(`Clicking ${this.selector} ...`);
if (xOffset != null || yOffset != null) {
const coordinates = await this.getCoordinates(xOffset, yOffset);
await this.page.click(this.selector, { position: { x: coordinates.x, y: coordinates.y } });
}
else await this.page.click(this.selector);
}
async doubleClick(xOffset = null, yOffset = null) {
console.log(`Double clicking ${this.selector} ...`);
if (xOffset != null || yOffset != null) {
const coordinates = await this.getCoordinates(xOffset, yOffset);
await this.page.dblclick(this.selector, { position: { x: coordinates.x, y: coordinates.y } });
}
else await this.page.dblclick(this.selector);
}
async rightClick(xOffset = null, yOffset = null) {
console.log(`Right clicking ${this.selector} ...`);
if (xOffset != null || yOffset != null) {
const coordinates = await this.getCoordinates(xOffset, yOffset);
await this.page.click(this.selector, { position: { x: coordinates.x, y: coordinates.y }, button: "right" } );
}
else await this.page.click(this.selector, { button: "right" });
}
async hover(xOffset = null, yOffset = null) {
console.log(`Hovering mouse on to ${this.selector} ...`);
if (xOffset != null || yOffset != null) {
const coordinates = await this.getCoordinates(xOffset, yOffset);
await this.page.hover(this.selector, { position: { x: coordinates.x, y: coordinates.y } });
}
else await this.page.hover(this.selector);
}
async exists() {
let exist;
try {
await this.wait(shortTimeout);
exist = true;
console.log(`Element ${this.selector} exists`);
}
catch (error) {
exist = false;
console.log(`Element ${this.selector} does not exist`);
}
return exist;
}
async isVisible() {
let visible;
try {
await this.waitUntilVisible(shortTimeout);
visible = true;
console.log(`Element ${this.selector} is visible`);
}
catch (error) {
visible = false;
console.log(`Element ${this.selector} is not visible`);
}
return visible;
}
async dragAndDrop(destination) {
const sourceElement = await this.page.waitForSelector(this.selector);
const destinationElement = await this.page.waitForSelector(destination.selector);
const sourceBox = await sourceElement.boundingBox();
const destinationBox = await destinationElement.boundingBox();
console.log(`Drag and dropping ${this.selector} to ${destination.selector} ...`);
await this.page.mouse.move(sourceBox.x + sourceBox.width / 2, sourceBox.y + sourceBox.height / 2);
await this.page.mouse.down();
await this.page.mouse.move(destinationBox.x + destinationBox.width / 2, destinationBox.y + destinationBox.height / 2);
await this.page.mouse.up();
}
async text() {
console.log(`Getting inner text of ${this.selector} ...`);
const elementHandle = await this.wait();
const text = await elementHandle.evaluate(element => element.innerText);
return text;
}
async value() {
console.log(`Getting value of ${this.selector} ...`);
const elementHandle = await this.wait();
const value = await elementHandle.evaluate(element => element.value);
return value;
}
async enterText(text) {
console.log(`Entering the text value for ${this.selector} ...`);
const elementHandle = await this.wait();
await elementHandle.type(text);
}
async clearText() {
console.log(`Clearing the text value for ${this.selector} ...`);
await this.click();
await this.page.keyboard.down("Control");
await this.page.keyboard.press("A");
await this.page.keyboard.up("Control");
await this.page.keyboard.press("Backspace");
}
async takeScreenshot() {
console.log(`Taking the screenshot for ${this.selector} ...`);
const elementHandle = await this.wait();
return await elementHandle.screenshot();
}
async getAttributeValue(attributeName) {
const elementHandle = await this.wait();
console.log(`Getting '${attributeName}' attribute value of element ${this.selector} ...`);
const atributeValue = await elementHandle.evaluate((element, attributeName) => element.getAttribute(`${attributeName}`), attributeName);
return atributeValue;
}
async getStyleAttributeValue(name) {
const elementHandle = await this.wait();
console.log(`Getting value of style attribute '${name}' of ${this.selector} ...`);
const attributeValue = await elementHandle.evaluate((element, name) => element.style[name], name);
return attributeValue;
}
async cover() {
const elementHandle = await this.wait();
await elementHandle.evaluate((element) => {
//Get bounding area of element we want to cover
const rect = element.getBoundingClientRect();
//Create a canvas element
var canvas = document.createElement("canvas");
//Set canvas drawing area width/height
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
//Position canvas
canvas.style.position = "absolute";
canvas.style.left = 0;
canvas.style.top = 0;
canvas.style.zIndex = 100000;
//Make sure you can click 'through' the canvas
canvas.style.pointerEvents = "none";
//Append canvas to body element
document.body.appendChild(canvas);
var context = canvas.getContext("2d");
//Draw rectangle
context.rect(rect.x, rect.y, rect.width, rect.height);
context.fillStyle = "yellow";
context.fill();
});
}
async uploadFile(filePath, isAbsolutePath) {
if (!isAbsolutePath) filePath = process.cwd() + filePath;
console.log(`Uploading a file with path ${filePath}`);
const elementHandle = await this.wait();
await elementHandle.setInputFiles(filePath);
}
async downloadFile(filePath) {
if (!path.isAbsolute(filePath)) filePath = process.cwd().replace(/\\/g, "/") + filePath;
const [download] = await Promise.all([
this.page.waitForEvent("download"),
this.click(),
]);
await download.saveAs(filePath);
}
}