Skip to content

Commit

Permalink
open a new window in Native Automation to throw an error (#2945)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKamaev authored Aug 9, 2023
1 parent 5c3261b commit 5b521ef
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "testcafe-hammerhead",
"description": "A powerful web-proxy used as a core for the TestCafe testing framework (https://github.com/DevExpress/testcafe).",
"version": "31.4.12",
"version": "31.4.13",
"homepage": "https://github.com/DevExpress/testcafe-hammerhead",
"bugs": {
"url": "https://github.com/DevExpress/testcafe-hammerhead/issues"
Expand Down
6 changes: 3 additions & 3 deletions src/client/sandbox/child-window/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default class ChildWindowSandbox extends SandboxBase {
}

handleClickOnLinkOrArea (el: HTMLLinkElement | HTMLAreaElement): void {
if (!settings.get().allowMultipleWindows)
if (!settings.canOpenNewWindow)
return;

this._listeners.initElementListening(el, ['click']);
Expand Down Expand Up @@ -136,7 +136,7 @@ export default class ChildWindowSandbox extends SandboxBase {
handleWindowOpen (window: Window, args: [string?, string?, string?, boolean?]): Window {
const [url, target, parameters] = args;

if (settings.get().allowMultipleWindows && ChildWindowSandbox._shouldOpenInNewWindow(target, DefaultTarget.windowOpen)) {
if (settings.canOpenNewWindow && ChildWindowSandbox._shouldOpenInNewWindow(target, DefaultTarget.windowOpen)) {
const openedWindowInfo = this._openUrlInNewWindow(url, target, parameters, window);

return openedWindowInfo?.wnd;
Expand All @@ -151,7 +151,7 @@ export default class ChildWindowSandbox extends SandboxBase {
}

_handleFormSubmitting (window: Window): void {
if (!settings.get().allowMultipleWindows)
if (!settings.canOpenNewWindow)
return;

this._listeners.initElementListening(window, ['submit']);
Expand Down
2 changes: 1 addition & 1 deletion src/client/sandbox/node/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export default class ElementSandbox extends SandboxBase {
}

getCorrectedTarget (target = ''): string {
if (settings.get().allowMultipleWindows)
if (settings.canOpenNewWindow)
return target;

if (target && !isKeywordTarget(target) && !windowsStorage.findByName(target) ||
Expand Down
5 changes: 2 additions & 3 deletions src/client/sandbox/node/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ export default class WindowSandbox extends SandboxBase {
this.overrideNextSiblingInMutationRecord();
this.overridePreviousSiblingInMutationRecord();
this.overrideValueInHTMLInputElement();
this.overrideOpenInWindow();

if (settings.nativeAutomation)
return;
Expand All @@ -296,8 +297,6 @@ export default class WindowSandbox extends SandboxBase {
if (nativeMethods.objectAssign)
this.overrideAssignInObject();

this.overrideOpenInWindow();

if (window.FontFace)
this.overrideFontFaceInWindow();

Expand Down Expand Up @@ -587,7 +586,7 @@ export default class WindowSandbox extends SandboxBase {
if (originTarget)
return this.nodeSandbox.element.getCorrectedTarget(String(originTarget));

return settings.get().allowMultipleWindows ? DefaultTarget.windowOpen : '_self';
return settings.canOpenNewWindow ? DefaultTarget.windowOpen : '_self';
}

private overrideFontFaceInWindow () {
Expand Down
4 changes: 4 additions & 0 deletions src/client/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class Settings {
get nativeAutomation () {
return this._settings.nativeAutomation;
}

get canOpenNewWindow () {
return this._settings.nativeAutomation || this._settings.allowMultipleWindows;
}
}

const settings = new Settings();
Expand Down
50 changes: 50 additions & 0 deletions test/client/fixtures/sandbox/child-window-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,56 @@ test('window.open', function () {
settings.get().allowMultipleWindows = false;
});

test('window.open. Native Automation. Multiple windows are disabled', function () {
settings.get().nativeAutomation = true;

var storedOpenUrlInNewWindow = windowSandbox._childWindowSandbox._openUrlInNewWindow;
var handled = false;

windowSandbox._childWindowSandbox._openUrlInNewWindow = function (url, target) {
handled = true;

strictEqual(target, '_blank');
};

window.open('/test');

windowSandbox._childWindowSandbox._openUrlInNewWindow = storedOpenUrlInNewWindow;

settings.get().nativeAutomation = false;

strictEqual(handled, true);
});

test('Should open a new window in Native Automation by clicking a link', function () {
settings.get().nativeAutomation = true;

var storedOpenUrlInNewWindow = windowSandbox._childWindowSandbox._openUrlInNewWindow;
var handled = false;

hammerhead.sandbox.childWindow._openUrlInNewWindow = function () {
handled = true;
};

var link = document.createElement('a');

link.innerText = 'link';
link.href = 'http://example.com';
link.target = '_blank';

document.body.appendChild(link);

nativeMethods.click.call(link);

windowSandbox._childWindowSandbox._openUrlInNewWindow = storedOpenUrlInNewWindow;

settings.get().nativeAutomation = false;

document.body.removeChild(link);

strictEqual(handled, true);
});

test('open child window considering base element', function () {
settings.get().allowMultipleWindows = true;

Expand Down

0 comments on commit 5b521ef

Please sign in to comment.