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 pathelementActions.test.js
More file actions
367 lines (289 loc) · 13.1 KB
/
elementActions.test.js
File metadata and controls
367 lines (289 loc) · 13.1 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import { Element } from "test-juggler";
const fs = require("fs").promises;
const fsExtra = require("fs-extra");
describe("Element Actions", () => {
const sliceToClick = new Element("[seriesName='seriesx2'] path");
const localPath = process.cwd().replace(/\\/g, "/");
beforeEach(async () => {
console.log("Running test: " + jasmine["currentTest"].fullName);
});
afterAll(async () => {
const tempFileDir = process.cwd() + "/example/testFiles/temp";
await fsExtra.emptyDir(tempFileDir);
});
it("should get element selector", async () => {
//Arrange
const element = new Element("some.selector");
//Act and Assert
expect(element.selector).toMatch("some.selector");
});
it("should get child element by XPath selector", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/");
//Act
const element = new Element("//div[@id='content']");
const childElement = element.newChildElement("//h1");
//Assert
await expect(childElement.isVisible()).resolves.toBeTruthy();
});
it("should wait for an element", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/dynamic_loading/2");
const startButton = new Element("div#start>button");
const elementToLoad = new Element("div#finish");
//Act
await startButton.click();
await elementToLoad.wait();
//Assert
await expect(elementToLoad.exists()).resolves.toBeTruthy();
});
it("should wait for element to be visible", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/dynamic_loading/1");
const startButton = new Element("div#start>button");
const elementToLoad = new Element("div#finish");
//Act
await startButton.click();
await elementToLoad.waitUntilVisible();
//Assert
await expect(elementToLoad.isVisible()).resolves.toBeTruthy();
});
it("should wait for element to be invisible", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/dynamic_loading/1");
const startButton = new Element("div#start>button");
const loader = new Element("div#loading");
const elementToLoad = new Element("div#finish");
//Act
await startButton.click();
await loader.waitUntilInvisible();
//Assert
await expect(loader.isVisible()).resolves.toBeFalsy();
await expect(elementToLoad.isVisible()).resolves.toBeTruthy();
});
it("should click an element", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/add_remove_elements/");
const addElementButton = new Element("button[onclick='addElement()']");
const deleteButton = new Element("button[onclick='deleteElement()']");
//Act
await addElementButton.click();
//Assert
await expect(deleteButton.exists()).resolves.toBeTruthy();
});
it("should double click an element", async () => {
//Arrange
await page.goto("https://demoqa.com/buttons");
const doubleClickButton = new Element("#doubleClickBtn");
const doubleClickMessage = new Element("#doubleClickMessage");
//Act
await doubleClickButton.doubleClick();
//Assert
await expect(doubleClickMessage.exists()).resolves.toBeTruthy();
expect(await doubleClickMessage.text()).toEqual("You have done a double click");
});
it("should right click an element", async () => {
//Arrange
await page.goto("https://demoqa.com/buttons");
const rightClickButton = new Element("#rightClickBtn");
const rightClickMessage = new Element("#rightClickMessage");
//Act
await rightClickButton.rightClick();
//Assert
await expect(rightClickMessage.exists()).resolves.toBeTruthy();
expect(await rightClickMessage.text()).toEqual("You have done a right click");
});
it("should check if element exist", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/");
const headerText = new Element(".heading");
//Act
const elementExists = await headerText.exists();
//Assert
expect(elementExists).toBeTruthy();
});
it("should check if element is visible", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/");
const headerText = new Element(".heading");
//Act
const elementIsVisible = await headerText.isVisible();
//Assert
expect(elementIsVisible).toBeTruthy();
});
//TODO: Unxit when issue is resolved in Puppeteer: https://github.com/puppeteer/puppeteer/issues/4562
xit("should Drag and Drop one element on to another", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/drag_and_drop");
const firstBox = new Element("#column-a");
const secondBox = new Element("#column-b");
const firstBoxInitialHeader = await firstBox.text();
console.log("First box initial header is: " + firstBoxInitialHeader);
const secondBoxInitialHeader = await secondBox.text();
console.log("Second box initial header is: " + secondBoxInitialHeader);
//Act
await firstBox.dragAndDrop(secondBox);
//Assert
const firstBoxResultHeader = await firstBox.text();
console.log("First box header after drag and drop is: " + firstBoxInitialHeader);
const secondBoxResultHeader = await secondBox.text();
console.log("Second box header after drag and drop is: " + secondBoxInitialHeader);
expect(firstBoxResultHeader).toMatch(secondBoxInitialHeader);
expect(secondBoxResultHeader).toMatch(firstBoxInitialHeader);
});
it("should get inner text of an element", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/");
const headerText = new Element(".heading");
//Act
const actualText = await headerText.text();
//Assert
expect(actualText).toMatch("Welcome to the-internet");
});
it("should get value of style attribute text-align of an element", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/inputs");
const element = new Element("#page-footer > div > div");
//Act
const actualTextAlignValue = await element.getStyleAttributeValue("text-align");
//Assert
expect(actualTextAlignValue).toMatch("center");
});
it("should hover on an element", async () => {
//Arrange
await page.goto("https://demoqa.com/tool-tips" );
const button = new Element("#toolTipButton");
const tooltip = new Element("#buttonToolTip");
//Act
await button.hover();
//Assert
await expect(tooltip.isVisible()).resolves.toBeTruthy();
});
it("should get DOM element", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/");
const headerText = new Element(".heading");
//Act
const headerTextDomElement = await headerText.wait();
//Assert
await expect(headerTextDomElement.evaluate(domElement => domElement.textContent)).resolves.toMatch("Welcome to the-internet");
});
it("should get element's value", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/inputs");
const inputElement = new Element("input[type=number]");
const inputNumber = "123";
//Act
await page.type(inputElement.selector, inputNumber);
//Assert
expect(await inputElement.value()).toEqual(inputNumber);
});
it("should get element's attribute value", async () => {
//Arrange, Act
await page.goto("http://the-internet.herokuapp.com/login");
const formElement = new Element("#login");
const attributeName = "action";
//Assert
expect(await formElement.getAttributeValue(attributeName)).toEqual("/authenticate");
});
it.each`
action | selectedAttr | pieClickedAttr | description
${async () => { sliceToClick.hover(150); }} | ${null} | ${null} | ${"hover"}
${async () => { sliceToClick.click(null, 85); }} | ${"true"} | ${"true"} | ${"left-click"}
${async () => { sliceToClick.rightClick(100, 90); }} | ${"true"} | ${null} | ${"right-click"}
`("should $description element with offset", async ({ action, selectedAttr, pieClickedAttr }) => {
//Arrange
const toolTip = new Element(".apexcharts-tooltip.apexcharts-active");
await page.goto("https://apexcharts.com/samples/react/pie/simple-donut.html");
await page.waitForSelector(`${sliceToClick.selector}[stroke-width='2']`);
//Act
await action();
//Assert
expect(await toolTip.isVisible()).toBe(true);
expect(await toolTip.text()).toContain("series-2: 55");
expect(await sliceToClick.getAttributeValue("selected")).toEqual(selectedAttr);
expect(await sliceToClick.getAttributeValue("data:pieClicked")).toEqual(pieClickedAttr);
});
it("should type element's text value", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/inputs");
const inputElement = new Element("input[type=number]");
const inputNumber = "456";
//Act
await inputElement.enterText(inputNumber);
//Assert
expect(await inputElement.value()).toEqual(inputNumber);
});
it("should clear element's text value", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/inputs");
const inputElement = new Element("input[type=number]");
const inputNumber = "456";
await inputElement.enterText(inputNumber);
//Act
await inputElement.clearText();
//Assert
expect(await inputElement.value()).toEqual("");
});
xit("should cover element", async () => {
//TODO: Test should be added and unxit`ed when #23 is implemented.
});
it("should get coordinates of element", async () => {
//Arrange
const expectedXCoordinate = 640; //width: default viewport 1280px / 2
const expectedYCoordinate = 25; //height: top bar 50px / 2
const rectangleCanvas = new Element(".top-bar__network");
await page.goto("https://stackoverflow.com/users/login");
//Act
const coordinates = await rectangleCanvas.getCoordinates();
//Assert
expect(coordinates.x).toEqual(expectedXCoordinate);
expect(coordinates.y).toEqual(expectedYCoordinate);
});
it("should upload a file when an absolute path is provided", async () => {
//Arrange
const filePath = process.cwd() + "/example/testFiles/Dummy.txt";
const remotePath = "C:\\fakepath\\Dummy.txt";
const uploadElement = new Element("#uploadFile");
const resultElement = new Element("#uploadedFilePath");
await page.goto("https://demoqa.com/upload-download");
//Act
await uploadElement.uploadFile(filePath, true);
//Assert
expect(await resultElement.text()).toEqual(remotePath);
});
it("should upload a file when a relative path is provided", async () => {
//Arrange
const filePath = "/example/testFiles/Dummy.txt";
const remotePath = "C:\\fakepath\\Dummy.txt";
const uploadElement = new Element("#uploadFile");
const resultElement = new Element("#uploadedFilePath");
await page.goto("https://demoqa.com/upload-download");
//Act
await uploadElement.uploadFile(filePath, false);
//Assert
expect(await resultElement.text()).toEqual(remotePath);
});
it("should download a file when an absolute path is provided", async () => {
//Arrange
const filePath = localPath + "/example/examplePages/files/example.zip";
const resultFilePath = localPath + "/example/testFiles/temp/example.zip";
const downloadElement = new Element("#downloadLink");
await page.goto(`file:///${localPath}/example/examplePages/download.html`);
//Act
await downloadElement.downloadFile(resultFilePath);
//Assert
expect(await fs.readFile(filePath)).toEqual(await fs.readFile(resultFilePath));
});
it("should download a file when an relative path is provided", async () => {
//Arrange
const filePath = localPath + "/example/examplePages/files/example.zip";
const resultFilePath = "/example/testFiles/temp/example.zip";
const downloadElement = new Element("#downloadLink");
await page.goto(`file:///${localPath}/example/examplePages/download.html`);
//Act
await downloadElement.downloadFile(resultFilePath);
//Assert
expect(await fs.readFile(filePath)).toEqual(await fs.readFile(localPath + resultFilePath));
});
});