Skip to content

Commit

Permalink
chore(refactor): grab frameElement using slightly stricter logic (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored Mar 10, 2021
1 parent 129edfe commit 4462edb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
35 changes: 21 additions & 14 deletions src/getCypressElementCoordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ function scrollIntoView(
htmlElement.scrollIntoView({ block });
}

// for cross origin domains .frameElement returns null so query using parentWindow
// but when running using --disable-web-security it will return the frame element
function getFrameElement(currentWindow: Window): HTMLElement {
if (currentWindow.frameElement) {
// accessible for same-origin iframes
// or when running with --disable-web-security
return currentWindow.frameElement as HTMLElement;
}

// fallback to querying using the parent window, mainly to grab the AUT iframe
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return [...currentWindow.parent.document.querySelectorAll("iframe")].find(
(iframe) => iframe.contentWindow === currentWindow
)!;
}

function getIframesPositionShift(element: HTMLElement) {
let currentWindow: Window | null = element.ownerDocument.defaultView;
const noPositionShift = {
Expand All @@ -80,26 +96,17 @@ function getIframesPositionShift(element: HTMLElement) {
}

// eslint-disable-next-line prefer-const
const iframes = []

while (
currentWindow &&
currentWindow !== window.top
) {
iframes.push(
// for cross origin domains .frameElement returns null so query using parentWindow
// but when running using --disable-web-security it will return the frame element
(currentWindow.frameElement as HTMLElement) ??
currentWindow.parent.document.querySelector("iframe")
);
const iframes = [];

while (currentWindow !== window.top) {
iframes.push(getFrameElement(currentWindow));
currentWindow = currentWindow.parent;
}

return iframes.reduceRight(({ frameX, frameY, frameScale }, frame) => {
return iframes.reduceRight(({ frameX, frameY, frameScale }, frame) => {
const { x, y, width } = frame.getBoundingClientRect();

return {
return {
frameX: frameX + x * frameScale,
frameY: frameY + y * frameScale,
frameScale: frameScale * (width / frame.offsetWidth),
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"types": ["cypress"],
"target": "es6",
"module": "commonjs",
"lib": ["DOM","ES2016"],
"lib": ["DOM", "DOM.Iterable", "ES2016"],
"outDir": "dist",
"declaration": true,
"strict": true,
Expand Down

0 comments on commit 4462edb

Please sign in to comment.