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

Commit

Permalink
#85 - Refactored getCoordinates method;
Browse files Browse the repository at this point in the history
- Refactored click method;
- Refactored doubleclick mehtod;
- Refactored right-click mehtod;
- Refactored hover mehtod;
- Removed goToUrlAndLoad method from helpers class;
- Removed goToUrlAndLoad example test.
  • Loading branch information
ernestas-zekas committed Feb 9, 2021
1 parent 620be43 commit 65aaf96
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 44 deletions.
6 changes: 3 additions & 3 deletions example/tests/elementActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ describe("Element Actions", () => {
it.each`
action | selectedAttr | pieClickedAttr | description
${async () => { sliceToClick.hover(150); }} | ${null} | ${null} | ${"hover"}
${async () => { sliceToClick.click(150); }} | ${"true"} | ${"true"} | ${"left-click"}
${async () => { sliceToClick.rightClick(null, 100); }} | ${"true"} | ${null} | ${"right-click"}
${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");
Expand Down Expand Up @@ -293,7 +293,7 @@ describe("Element Actions", () => {
await page.goto("https://stackoverflow.com/users/login");

//Act
const coordinates = await rectangleCanvas.getCoordinates();
const coordinates = await rectangleCanvas.getElementClickCoordinates();

//Assert
expect(coordinates.x).toEqual(expectedXCoordinate);
Expand Down
12 changes: 0 additions & 12 deletions example/tests/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,6 @@ describe("Helpers", () => {
await expect(elementToLoad.exists()).resolves.toBeTruthy();
});

it("should wait for navigation to be finished", async () => {
//Arrange
const progressLoader = new Element("html.nprogress-busy");
const loadTimeout = 30000;

//Act
await Helpers.goToUrlAndLoad("https://www.jqueryscript.net/demo/jQuery-Html5-Based-Preloader-Plugin-html5loader/", loadTimeout);

//Assert
await expect(progressLoader.exists()).resolves.toBeFalsy();
});

it("should enter iFrame and get text", async () => {
//Arrange
const iFrameSelector = "#mce_0_ifr";
Expand Down
8 changes: 4 additions & 4 deletions example/tests/interceptor.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Element, Helpers, Interceptor } from "test-juggler";
import { Element, Interceptor } from "test-juggler";

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

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

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

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

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

//Act
var requestAfterAction = await Interceptor.waitForRequestAfterAction(addToCartButton.click());
Expand Down
43 changes: 26 additions & 17 deletions framework/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,50 @@ export default class Element {
}
}

async getCoordinates(xOffset = null, yOffset = null) {
async getElementClickCoordinates(xOffset = null, yOffset = null) {
const elementHandle = await this.wait();
await elementHandle.scrollIntoViewIfNeeded();
const rect = await elementHandle.boundingBox();
const x = xOffset !== null ? xOffset : rect.width / 2;
const y = yOffset !== null ? yOffset : rect.height / 2;
const xCoordinate = rect.x + x;
const yCoordinate = rect.y + y;
console.log(`Action on page at position x: ${xCoordinate}, y: ${yCoordinate}`);
console.log(`Action on element rectangle at position x: ${x}, y: ${y}`);
return { x: xCoordinate, y: yCoordinate };
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} ...`);
const coordinates = await this.getCoordinates(xOffset, yOffset);
await page.mouse.click(coordinates.x, coordinates.y);
if (xOffset != null || yOffset != null) {
const coordinates = await this.getElementClickCoordinates(xOffset, yOffset);
await page.click(this.selector, { position: { x: coordinates.x, y: coordinates.y } });
}
else await page.click(this.selector);
}

async doubleClick(xOffset = null, yOffset = null) {
console.log(`Double clicking ${this.selector} ...`);
const coordinates = await this.getCoordinates(xOffset, yOffset);
await page.mouse.click(coordinates.x, coordinates.y, { clickCount: 2 });
if (xOffset != null || yOffset != null) {
const coordinates = await this.getElementClickCoordinates(xOffset, yOffset);
await page.dblclick(this.selector, { position: { x: coordinates.x, y: coordinates.y } });
}
else await page.dblclick(this.selector);
}

async rightClick(xOffset = null, yOffset = null) {
console.log(`Right clicking ${this.selector} ...`);
const coordinates = await this.getCoordinates(xOffset, yOffset);
await page.mouse.click(coordinates.x, coordinates.y, { button: "right" });
if (xOffset != null || yOffset != null) {
const coordinates = await this.getElementClickCoordinates(xOffset, yOffset);
await page.click(this.selector, { position: { x: coordinates.x, y: coordinates.y }, button: "right" } );
}
else await page.click(this.selector, { button: "right" });
}

async hover(xOffset = null, yOffset = null) {
console.log(`Hovering mouse on to ${this.selector} ...`);
const coordinates = await this.getCoordinates(xOffset, yOffset);
await page.mouse.move(coordinates.x, coordinates.y);
if (xOffset != null || yOffset != null) {
const coordinates = await this.getElementClickCoordinates(xOffset, yOffset);
await page.hover(this.selector, { position: { x: coordinates.x, y: coordinates.y } });
}
else await page.hover(this.selector);
}

async exists() {
Expand Down
8 changes: 0 additions & 8 deletions framework/helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const fs = require("fs");
const retry = require("async-retry");
const config = require(process.cwd() + "/framework.config");
const defaultTimeout = config.defaultTimeout;
const defaultCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

class Helpers {
Expand All @@ -26,12 +24,6 @@ class Helpers {
});
}

async goToUrlAndLoad(url, timeout = defaultTimeout) {
await page.goto(url, {
waitUntil: "networkidle", timeout: timeout,
});
}

async getFrame(selector) {
await page.waitForSelector(selector);
const elementHandle = await page.$(selector);
Expand Down

0 comments on commit 65aaf96

Please sign in to comment.