Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Commit 65aaf96

Browse files
#85 - Refactored getCoordinates method;
- Refactored click method; - Refactored doubleclick mehtod; - Refactored right-click mehtod; - Refactored hover mehtod; - Removed goToUrlAndLoad method from helpers class; - Removed goToUrlAndLoad example test.
1 parent 620be43 commit 65aaf96

5 files changed

Lines changed: 33 additions & 44 deletions

File tree

example/tests/elementActions.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ describe("Element Actions", () => {
235235
it.each`
236236
action | selectedAttr | pieClickedAttr | description
237237
${async () => { sliceToClick.hover(150); }} | ${null} | ${null} | ${"hover"}
238-
${async () => { sliceToClick.click(150); }} | ${"true"} | ${"true"} | ${"left-click"}
239-
${async () => { sliceToClick.rightClick(null, 100); }} | ${"true"} | ${null} | ${"right-click"}
238+
${async () => { sliceToClick.click(null, 85); }} | ${"true"} | ${"true"} | ${"left-click"}
239+
${async () => { sliceToClick.rightClick(100, 90); }} | ${"true"} | ${null} | ${"right-click"}
240240
`("should $description element with offset", async ({ action, selectedAttr, pieClickedAttr }) => {
241241
//Arrange
242242
const toolTip = new Element(".apexcharts-tooltip.apexcharts-active");
@@ -293,7 +293,7 @@ describe("Element Actions", () => {
293293
await page.goto("https://stackoverflow.com/users/login");
294294

295295
//Act
296-
const coordinates = await rectangleCanvas.getCoordinates();
296+
const coordinates = await rectangleCanvas.getElementClickCoordinates();
297297

298298
//Assert
299299
expect(coordinates.x).toEqual(expectedXCoordinate);

example/tests/helpers.test.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,6 @@ describe("Helpers", () => {
4747
await expect(elementToLoad.exists()).resolves.toBeTruthy();
4848
});
4949

50-
it("should wait for navigation to be finished", async () => {
51-
//Arrange
52-
const progressLoader = new Element("html.nprogress-busy");
53-
const loadTimeout = 30000;
54-
55-
//Act
56-
await Helpers.goToUrlAndLoad("https://www.jqueryscript.net/demo/jQuery-Html5-Based-Preloader-Plugin-html5loader/", loadTimeout);
57-
58-
//Assert
59-
await expect(progressLoader.exists()).resolves.toBeFalsy();
60-
});
61-
6250
it("should enter iFrame and get text", async () => {
6351
//Arrange
6452
const iFrameSelector = "#mce_0_ifr";

example/tests/interceptor.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Element, Helpers, Interceptor } from "test-juggler";
1+
import { Element, Interceptor } from "test-juggler";
22

33
const DemoQaSite = "https://demoqa.com/books";
44
const DemoOpenCartSite = "https://demo.opencart.com/";
@@ -95,7 +95,7 @@ describe("Interceptor", () => {
9595

9696
it("should count all requests", async () => {
9797
//Act
98-
var totalRequests = await Interceptor.getAllRequestsData(Helpers.goToUrlAndLoad(DemoOpenCartSite));
98+
var totalRequests = await Interceptor.getAllRequestsData(page.goto(DemoOpenCartSite));
9999

100100
//Assert
101101
expect(totalRequests.length > 0).toBeTruthy();
@@ -105,7 +105,7 @@ describe("Interceptor", () => {
105105
it("should detect specific response after action", async () => {
106106
//Arrange
107107
const responseUrlFragment = "cart/info";
108-
await Helpers.goToUrlAndLoad(DemoOpenCartSite);
108+
await page.goto(DemoOpenCartSite);
109109

110110
//Act
111111
var responseAfterAction = await Interceptor.waitForResponseAfterAction(addToCartButton.click(), responseUrlFragment);
@@ -119,7 +119,7 @@ describe("Interceptor", () => {
119119

120120
it("should detect any request after action", async () => {
121121
//Arrange
122-
await Helpers.goToUrlAndLoad(DemoOpenCartSite);
122+
await page.goto(DemoOpenCartSite);
123123

124124
//Act
125125
var requestAfterAction = await Interceptor.waitForRequestAfterAction(addToCartButton.click());

framework/Element.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,50 @@ export default class Element {
4343
}
4444
}
4545

46-
async getCoordinates(xOffset = null, yOffset = null) {
46+
async getElementClickCoordinates(xOffset = null, yOffset = null) {
4747
const elementHandle = await this.wait();
48-
await elementHandle.scrollIntoViewIfNeeded();
4948
const rect = await elementHandle.boundingBox();
50-
const x = xOffset !== null ? xOffset : rect.width / 2;
51-
const y = yOffset !== null ? yOffset : rect.height / 2;
52-
const xCoordinate = rect.x + x;
53-
const yCoordinate = rect.y + y;
54-
console.log(`Action on page at position x: ${xCoordinate}, y: ${yCoordinate}`);
55-
console.log(`Action on element rectangle at position x: ${x}, y: ${y}`);
56-
return { x: xCoordinate, y: yCoordinate };
49+
const xCoordinate = xOffset !== null ? xOffset : rect.width / 2;
50+
const yCoordinate = yOffset !== null ? yOffset : rect.height / 2;
51+
console.log(`Element width: ${rect.width}, height: ${rect.height}`);
52+
console.log(`Action on element rectangle at position x: ${xCoordinate}, y: ${yCoordinate}`);
53+
return { x: xCoordinate, y: yCoordinate };
5754
}
5855

5956
async click(xOffset = null, yOffset = null) {
6057
console.log(`Clicking ${this.selector} ...`);
61-
const coordinates = await this.getCoordinates(xOffset, yOffset);
62-
await page.mouse.click(coordinates.x, coordinates.y);
58+
if (xOffset != null || yOffset != null) {
59+
const coordinates = await this.getElementClickCoordinates(xOffset, yOffset);
60+
await page.click(this.selector, { position: { x: coordinates.x, y: coordinates.y } });
61+
}
62+
else await page.click(this.selector);
6363
}
6464

6565
async doubleClick(xOffset = null, yOffset = null) {
6666
console.log(`Double clicking ${this.selector} ...`);
67-
const coordinates = await this.getCoordinates(xOffset, yOffset);
68-
await page.mouse.click(coordinates.x, coordinates.y, { clickCount: 2 });
67+
if (xOffset != null || yOffset != null) {
68+
const coordinates = await this.getElementClickCoordinates(xOffset, yOffset);
69+
await page.dblclick(this.selector, { position: { x: coordinates.x, y: coordinates.y } });
70+
}
71+
else await page.dblclick(this.selector);
6972
}
7073

7174
async rightClick(xOffset = null, yOffset = null) {
7275
console.log(`Right clicking ${this.selector} ...`);
73-
const coordinates = await this.getCoordinates(xOffset, yOffset);
74-
await page.mouse.click(coordinates.x, coordinates.y, { button: "right" });
76+
if (xOffset != null || yOffset != null) {
77+
const coordinates = await this.getElementClickCoordinates(xOffset, yOffset);
78+
await page.click(this.selector, { position: { x: coordinates.x, y: coordinates.y }, button: "right" } );
79+
}
80+
else await page.click(this.selector, { button: "right" });
7581
}
7682

7783
async hover(xOffset = null, yOffset = null) {
7884
console.log(`Hovering mouse on to ${this.selector} ...`);
79-
const coordinates = await this.getCoordinates(xOffset, yOffset);
80-
await page.mouse.move(coordinates.x, coordinates.y);
85+
if (xOffset != null || yOffset != null) {
86+
const coordinates = await this.getElementClickCoordinates(xOffset, yOffset);
87+
await page.hover(this.selector, { position: { x: coordinates.x, y: coordinates.y } });
88+
}
89+
else await page.hover(this.selector);
8190
}
8291

8392
async exists() {

framework/helpers.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const fs = require("fs");
22
const retry = require("async-retry");
3-
const config = require(process.cwd() + "/framework.config");
4-
const defaultTimeout = config.defaultTimeout;
53
const defaultCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
64

75
class Helpers {
@@ -26,12 +24,6 @@ class Helpers {
2624
});
2725
}
2826

29-
async goToUrlAndLoad(url, timeout = defaultTimeout) {
30-
await page.goto(url, {
31-
waitUntil: "networkidle", timeout: timeout,
32-
});
33-
}
34-
3527
async getFrame(selector) {
3628
await page.waitForSelector(selector);
3729
const elementHandle = await page.$(selector);

0 commit comments

Comments
 (0)