Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions packages/taiko/lib/actions/pageActionChecks.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,18 @@ const checkStable = async (elem) => {
function waitForElementToBeStable() {
let elem = this;
return new Promise((resolve, reject) => {
setTimeout(() => reject(false), 10000);
setTimeout(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add an unit test covering this case ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have add two unit tests, so one for when the element is stable and one for the infinite animation case

() => reject(new Error("Element is not stable: still moving after 10000ms")),
10000,
);
let previousRect;
let currentRect;
function isInSamePosition(previousRect, currentRect) {
const topDiff = Math.abs(previousRect.top - currentRect.top);
const leftDiff = Math.abs(previousRect.left - currentRect.left);
const bottomDiff = Math.abs(previousRect.bottom - currentRect.bottom);
const rightDiff = Math.abs(previousRect.right - currentRect.right);
return topDiff + leftDiff + bottomDiff + rightDiff;
return topDiff + leftDiff + bottomDiff + rightDiff === 0;
}
(function step() {
if (elem.nodeType === Node.TEXT_NODE) {
Expand All @@ -101,14 +104,14 @@ const checkStable = async (elem) => {
if (previousRect === undefined) {
previousRect = currentRect;
window.requestAnimationFrame(step);
return;
}

const positionalDiff = isInSamePosition(previousRect, currentRect);
if (positionalDiff) {
if (isInSamePosition(previousRect, currentRect)) {
resolve(true);
} else {
previousRect = currentRect;
window.requestAnimationFrame(step);
} else {
resolve(true);
}
})();
});
Expand All @@ -123,6 +126,14 @@ const checkStable = async (elem) => {
awaitPromise: true,
},
);
if (res.exceptionDetails) {
const exceptionDetails = res.exceptionDetails;
const message =
exceptionDetails.exception && exceptionDetails.exception.description
? exceptionDetails.exception.description
: exceptionDetails.text || "Element is not stable";
throw new Error(message);
}
return res.result.value;
};

Expand Down
33 changes: 32 additions & 1 deletion tests/unit-tests/actions/pageActionChecks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,35 @@ describe("pageActionChecks", () => {
expect(result.name).to.equal("notActionable1");
});
});
});
describe("checkStable", () => {
afterEach(() => {
pageActionChecks = rewire("taiko/lib/actions/pageActionChecks");
});
it("should return true when element is stable", async () => {
pageActionChecks.__set__("runtimeHandler", {
runtimeCallFunctionOn: () => ({
result: { value: true },
}),
});
const elem = { get: () => 1 };
const result = await pageActionChecks.__get__("checkStable")(elem);
expect(result).to.be.true;
});
it("should throw when element keeps moving and never becomes stable", async () => {
pageActionChecks.__set__("runtimeHandler", {
runtimeCallFunctionOn: () => ({
exceptionDetails: {
exception: {
description:
"Error: Element is not stable: still moving after 10000ms",
},
},
}),
});
const elem = { get: () => 1 };
await expect(
pageActionChecks.__get__("checkStable")(elem),
).to.be.rejectedWith("Element is not stable: still moving after 10000ms");
});
});
});