Skip to content

Commit

Permalink
fix browser hanging when the test switches between windows after swit…
Browse files Browse the repository at this point in the history
…ching to iframe (closes #6085) (#6778)
  • Loading branch information
AlexKamaev committed Dec 27, 2021
1 parent 5268f54 commit 5596a58
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/client/automation/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// NOTE: Initializer should be the first
import './shared-adapter-initializer';

import hammerhead from './deps/hammerhead';
import DispatchEventAutomation from './playback/dispatch-event';
import SetScrollAutomation from './playback/set-scroll';
Expand Down
18 changes: 18 additions & 0 deletions src/client/automation/shared-adapter-initializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import hammerhead from './deps/hammerhead';
import testCafeCore from './deps/testcafe-core';
import { initializeAdapter } from '../../shared/adapter/index';
import { ScrollOptions } from '../../test-run/commands/options';
import { getOffsetOptions } from './utils/offsets';

const { nativeMethods, Promise } = hammerhead;
const { domUtils, ScrollAutomation } = testCafeCore;


initializeAdapter({
PromiseCtor: Promise,
nativeMethods: nativeMethods,
scroll: (el: any, scrollOptions: ScrollOptions) => new ScrollAutomation(el, scrollOptions).run(),

getOffsetOptions: getOffsetOptions,
isDomElement: domUtils.isDomElement,
});
7 changes: 7 additions & 0 deletions src/client/driver/driver-link/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const TYPE = {
restoreChildLink: 'driver|restore-child-link',
childWindowIsLoadedInIFrame: 'driver|child-window-is-loaded-in-iframe',
childWindowIsOpenedInIFrame: 'driver|child-window-is-opened-in-iframe',
stopInternalFromFrame: 'driver|stop-internal-from-iframe',
hasPendingActionFlags: 'driver|has-pending-action-flags',
};

Expand Down Expand Up @@ -150,6 +151,12 @@ export class ChildWindowIsOpenedInFrameMessage extends InterDriverMessage {
}
}

export class StopInternalFromFrameMessage extends InterDriverMessage {
constructor () {
super(TYPE.stopInternalFromFrame);
}
}

export class HasPendingActionFlagsMessage extends InterDriverMessage {
constructor () {
super(TYPE.hasPendingActionFlags);
Expand Down
14 changes: 14 additions & 0 deletions src/client/driver/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,17 @@ export default class Driver extends serviceUtils.EventEmitter {
this._onChildWindowOpened({ window: wnd, windowId: msg.windowId });
}

_handleStopInternalFromFrame (msg, wnd) {
sendConfirmationMessage({
requestMsgId: msg.id,
window: wnd,
});

this.contextStorage.setItem(this.EXECUTING_IN_IFRAME_FLAG, false);

this._stopInternal();
}

_initChildDriverListening () {
messageSandbox.on(messageSandbox.SERVICE_MSG_RECEIVED_EVENT, e => {
const msg = e.message;
Expand All @@ -837,6 +848,9 @@ export default class Driver extends serviceUtils.EventEmitter {
case MESSAGE_TYPE.childWindowIsLoadedInIFrame:
this._handleChildWindowIsLoadedInIFrame(msg, window);
break;
case MESSAGE_TYPE.stopInternalFromFrame:
this._handleStopInternalFromFrame(msg, window);
break;
case MESSAGE_TYPE.setAsMaster:
this._handleSetAsMasterMessage(msg, window);
break;
Expand Down
10 changes: 9 additions & 1 deletion src/client/driver/iframe-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import Driver from './driver';
import ContextStorage from './storage';
import DriverStatus from './status';
import ParentIframeDriverLink from './driver-link/iframe/parent';
import { ChildWindowIsOpenedInFrameMessage, TYPE as MESSAGE_TYPE } from './driver-link/messages';
import IframeNativeDialogTracker from './native-dialog-tracker/iframe';

import {
ChildWindowIsOpenedInFrameMessage,
StopInternalFromFrameMessage,
TYPE as MESSAGE_TYPE,
} from './driver-link/messages';

const messageSandbox = eventSandbox.message;

Expand Down Expand Up @@ -40,6 +44,10 @@ export default class IframeDriver extends Driver {
messageSandbox.sendServiceMsg(new ChildWindowIsOpenedInFrameMessage(), window.top);
}

_stopInternal () {
messageSandbox.sendServiceMsg(new StopInternalFromFrameMessage(), window.top);
}

// Messaging between drivers
_initParentDriverListening () {
eventSandbox.message.on(eventSandbox.message.SERVICE_MSG_RECEIVED_EVENT, e => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
</head>
<body>
<h1>iframe</h1>
</body>
</html>
11 changes: 11 additions & 0 deletions test/functional/fixtures/multiple-windows/pages/i6085/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>gh-6085</title>
</head>
<body>
gh-6085
<iframe src="http://localhost:3000/fixtures/multiple-windows/pages/i6085/iframe.html"></iframe>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
</head>
<body>
<h1>window</h1>
</body>
</html>
4 changes: 4 additions & 0 deletions test/functional/fixtures/multiple-windows/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ describe('Multiple windows', () => {
return runTests('testcafe-fixtures/i6680.js');
});

it('Should not hang if switching to a window from iframe', () => {
return runTests('testcafe-fixtures/i6085.js');
});

describe('API', () => {
it('Open child window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Open child window', { only: 'chrome' });
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const newWindowUrl = 'http://localhost:3000/fixtures/multiple-windows/pages/i6085/window.html';

fixture `Should not hang if switching to a window from iframe`
.page `http://localhost:3000/fixtures/multiple-windows/pages/i6085/index.html`;

test('Should not hang if switching to a window from iframe', async t => {
const newWindow = await t.openWindow(newWindowUrl);

await t.switchToParentWindow();
await t.switchToIframe('iframe');
await t.switchToWindow(newWindow);
await t.click('h1');
});

0 comments on commit 5596a58

Please sign in to comment.