Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
feat: LEAP-439: Add pixel waiting functions in ImageView (#25)
Browse files Browse the repository at this point in the history
* Add pixel waiting functions in ImageView

Two new functions, waitForPixel and waitForPixelRelative, were added. These functions allow the system to wait for a specific pixel color to appear. The waitForPixel waits for an absolute pixel position, while the waitForPixelRelative waits for a relative pixel position based on the element's bounding box.

* Refactor `waitForPixel` method in ImageView

This commit simplifies the `waitForPixel` method in `ImageView`. The changes improve the code readability without changing its functionality. The setTimeout mechanism for pixel color verification is replaced with a more straightforward recursive promise based approach.
  • Loading branch information
Gondragos authored Jan 9, 2024
1 parent fa441d3 commit 9ff949f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions helpers/LSF/ImageView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,27 @@ export const ImageView = {
pixelRelativeShouldBe(x: number, y: number, color: string) {
this.getPixelRelative(x, y).should('equal', color);
},
waitForPixel(x: number, y: number, color: string, { timeout = 1000, attempts = 10, delayBetweenAttempts = 100 } = {}) {
const timeEdge = Date.now() + timeout;
const waitForPixel = (attemptsLeft) => {
return this.getPixel(x, y).then(pixel => {
if (pixel === color || timeEdge < Date.now() || !attemptsLeft) {
return pixel;
} else {
return cy.wait(delayBetweenAttempts).then(() => waitForPixel(attemptsLeft - 1));
}
});
};

waitForPixel(attempts).should('equal', color);
},
waitForPixelRelative(x: number, y: number, color: string, options?: { timeout?: number, times?: number }) {
this.drawingFrame.then(el => {
const bbox: DOMRect = el[0].getBoundingClientRect();

this.waitForPixel(x * bbox.width, y * bbox.height, color, options);
});
},
/**
* Captures a screenshot of an element to compare later
* @param {string} name name of the screenshot
Expand Down

0 comments on commit 9ff949f

Please sign in to comment.