Skip to content

Commit 023f820

Browse files
fix: dont try to disable animation in non-displayed iframes
1 parent 821512d commit 023f820

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

src/browser/existing-browser.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Protocol } from "devtools-protocol";
2323
import { getCalculatedProtocol } from "./commands/saveState";
2424
import { Page } from "puppeteer-core";
2525
import { CDP } from "./cdp";
26+
import type { ElementReference } from "@testplane/wdio-protocols";
2627

2728
const OPTIONAL_SESSION_OPTS = ["transformRequest", "transformResponse"];
2829

@@ -573,19 +574,31 @@ export class ExistingBrowser extends Browser {
573574
);
574575
}
575576

576-
protected async _runInEachIframe(cb: (...args: unknown[]) => unknown): Promise<void> {
577+
protected async _runInEachDisplayedIframe(cb: (...args: unknown[]) => unknown): Promise<void> {
577578
ensure(this._session, BROWSER_SESSION_HINT);
578-
const iframes = await this._session.findElements("css selector", "iframe");
579+
const session = this._session;
580+
const iframes = await session.findElements("css selector", "iframe[src]");
581+
const displayedIframes: ElementReference[] = [];
582+
583+
await Promise.all(
584+
iframes.map(async iframe => {
585+
const isIframeDisplayed = await session.$(iframe).isDisplayed();
586+
587+
if (isIframeDisplayed) {
588+
displayedIframes.push(iframe);
589+
}
590+
}),
591+
);
579592

580593
try {
581-
for (const iframe of iframes) {
582-
await this._session.switchToFrame(iframe);
594+
for (const iframe of displayedIframes) {
595+
await session.switchToFrame(iframe);
583596
await cb();
584597
// switchToParentFrame does not work in ios - https://github.com/appium/appium/issues/14882
585-
await this._session.switchToFrame(null);
598+
await session.switchToFrame(null);
586599
}
587600
} catch (e) {
588-
await this._session.switchToFrame(null);
601+
await session.switchToFrame(null);
589602
throw e;
590603
}
591604
}
@@ -604,7 +617,7 @@ export class ExistingBrowser extends Browser {
604617
}
605618

606619
protected async _disableIframeAnimations(): Promise<void> {
607-
await this._runInEachIframe(() => this._disableFrameAnimations());
620+
await this._runInEachDisplayedIframe(() => this._disableFrameAnimations());
608621
}
609622

610623
protected async _cleanupFrameAnimations(): Promise<void> {
@@ -614,7 +627,7 @@ export class ExistingBrowser extends Browser {
614627
}
615628

616629
protected async _cleanupIframeAnimations(): Promise<void> {
617-
await this._runInEachIframe(() => this._cleanupFrameAnimations());
630+
await this._runInEachDisplayedIframe(() => this._cleanupFrameAnimations());
618631
}
619632

620633
protected async _cleanupPageAnimations(): Promise<void> {

test/src/browser/existing-browser.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -840,13 +840,16 @@ describe("ExistingBrowser", () => {
840840
});
841841

842842
describe("'disableAnimation: true' and 'automationProtocol: webdriver'", () => {
843-
it("should disable animations", async () => {
843+
it("should disable animations on displayed iframes", async () => {
844844
const clientBridge = stubClientBridge_();
845845
const browser = await initBrowser_(mkBrowser_({ automationProtocol: "webdriver" }));
846846
const iframeElement1 = { "element-12345": "67890_element_1" };
847847
const iframeElement2 = { "element-54321": "09876_element_2" };
848+
const elementStub = (browser.publicAPI.$ = sandbox.stub());
849+
elementStub.withArgs(iframeElement1).returns({ isDisplayed: () => Promise.resolve(false) });
850+
elementStub.withArgs(iframeElement2).returns({ isDisplayed: () => Promise.resolve(true) });
848851
browser.publicAPI.findElements
849-
.withArgs("css selector", "iframe")
852+
.withArgs("css selector", "iframe[src]")
850853
.resolves([iframeElement1, iframeElement2]);
851854

852855
await browser.prepareScreenshot(".selector", { disableAnimation: true });
@@ -856,15 +859,11 @@ describe("ExistingBrowser", () => {
856859
".selector",
857860
sinon.match({ disableAnimation: true }),
858861
]),
859-
860-
browser.publicAPI.switchToFrame.withArgs(iframeElement1),
861-
clientBridge.call.withArgs("disableFrameAnimations"),
862-
browser.publicAPI.switchToFrame.withArgs(null),
863-
864862
browser.publicAPI.switchToFrame.withArgs(iframeElement2),
865863
clientBridge.call.withArgs("disableFrameAnimations"),
866864
browser.publicAPI.switchToFrame.withArgs(null),
867865
);
866+
assert.neverCalledWithMatch(browser.publicAPI.switchToFrame, iframeElement1);
868867
});
869868
});
870869

@@ -886,7 +885,11 @@ describe("ExistingBrowser", () => {
886885
const clientBridge = stubClientBridge_();
887886
const browser = await initBrowser_(mkBrowser_({ automationProtocol: "webdriver" }));
888887
const iframeElement = { "element-12345": "67890_element_1" };
889-
browser.publicAPI.findElements.withArgs("css selector", "iframe").resolves([iframeElement]);
888+
browser.publicAPI.findElements.withArgs("css selector", "iframe[src]").resolves([iframeElement]);
889+
browser.publicAPI.$ = sandbox
890+
.stub()
891+
.withArgs(iframeElement)
892+
.returns({ isDisplayed: () => Promise.resolve(true) });
890893

891894
await browser.prepareScreenshot(".selector", { disableAnimation: false });
892895

@@ -932,7 +935,11 @@ describe("ExistingBrowser", () => {
932935
const clientBridge = stubClientBridge_();
933936
const browser = await initBrowser_(mkBrowser_({ automationProtocol: "webdriver" }));
934937
const iframeElement = { "element-12345": "67890_element_1" };
935-
browser.publicAPI.findElements.withArgs("css selector", "iframe").resolves([iframeElement]);
938+
browser.publicAPI.findElements.withArgs("css selector", "iframe[src]").resolves([iframeElement]);
939+
browser.publicAPI.$ = sandbox
940+
.stub()
941+
.withArgs(iframeElement)
942+
.returns({ isDisplayed: () => Promise.resolve(true) });
936943

937944
await browser.cleanupScreenshot({ disableAnimation: true });
938945

@@ -945,7 +952,11 @@ describe("ExistingBrowser", () => {
945952
stubClientBridge_();
946953
const browser = await initBrowser_(mkBrowser_({ automationProtocol: "webdriver" }));
947954
const iframeElement = { "element-12345": "67890_element_1" };
948-
browser.publicAPI.findElements.withArgs("css selector", "iframe").resolves([iframeElement]);
955+
browser.publicAPI.findElements.withArgs("css selector", "iframe[src]").resolves([iframeElement]);
956+
browser.publicAPI.$ = sandbox
957+
.stub()
958+
.withArgs(iframeElement)
959+
.returns({ isDisplayed: () => Promise.resolve(true) });
949960

950961
await browser.cleanupScreenshot({ disableAnimation: true });
951962

@@ -958,7 +969,7 @@ describe("ExistingBrowser", () => {
958969
it("should not switch to any frame if there are no iframes on the page ", async () => {
959970
stubClientBridge_();
960971
const browser = await initBrowser_(mkBrowser_({ automationProtocol: "webdriver" }));
961-
browser.publicAPI.findElements.withArgs("css selector", "iframe").resolves([]);
972+
browser.publicAPI.findElements.withArgs("css selector", "iframe[src]").resolves([]);
962973

963974
await browser.cleanupScreenshot({ disableAnimation: true });
964975

0 commit comments

Comments
 (0)