-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement ConsoleSandbox
. Add the consoleMethCalled
event.
#1312
Conversation
src/client/sandbox/console.js
Outdated
const sandbox = this; | ||
|
||
const proxyConsoleMeth = meth => { | ||
sandbox.window.console[meth] = function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a new code we use the spread
operator for an argument cloning
see
https://github.com/DevExpress/testcafe-hammerhead/blob/master/src/client/sandbox/node/document/index.js#L107
https://github.com/DevExpress/testcafe-hammerhead/blob/master/src/client/sandbox/node/window.js#L300
https://github.com/DevExpress/testcafe-hammerhead/blob/master/src/client/sandbox/event/index.js#L98
So, rewrite as
sandbox.window.console[meth] = (...args) => {
sandbox.emit(sandbox.CONSOLE_METH_CALLED, { meth, args: args});
sandbox.nativeMethods.consoleMeths[meth].apply(sandbox.nativeMethods.console, args);
}
src/client/index.js
Outdated
@@ -54,7 +54,8 @@ class Hammerhead { | |||
beforeXhrSend: this.sandbox.xhr.BEFORE_XHR_SEND_EVENT, | |||
fetchSent: this.sandbox.fetch.FETCH_REQUEST_SENT_EVENT, | |||
pageNavigationTriggered: this.pageNavigationWatch.PAGE_NAVIGATION_TRIGGERED_EVENT, | |||
scriptElementAdded: this.sandbox.node.element.SCRIPT_ELEMENT_ADDED_EVENT | |||
scriptElementAdded: this.sandbox.node.element.SCRIPT_ELEMENT_ADDED_EVENT, | |||
consoleMethCalled: this.sandbox.console.CONSOLE_METH_CALLED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.sandbox.console.CONSOLE_METH_CALLED
-> this.sandbox.console.CONSOLE_METH_CALLED_EVENT
src/client/sandbox/console.js
Outdated
constructor () { | ||
super(); | ||
|
||
this.CONSOLE_METH_CALLED = 'hammerhead|console|console-meth-called-event'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.CONSOLE_METH_CALLED_EVENT = 'hammerhead|event|console-meth-called
src/client/sandbox/native-methods.js
Outdated
// Console | ||
this.console = win.console; | ||
|
||
this.consoleMeths = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't use an additional hierarchy level for similar cases.
See
https://github.com/DevExpress/testcafe-hammerhead/blob/master/src/client/sandbox/native-methods.js#L121
https://github.com/DevExpress/testcafe-hammerhead/blob/master/src/client/sandbox/native-methods.js#L176
Rewrite as
this.consoleLog = ...
this.consoleWarn = ...
etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? Is there a significant reason for this? I believe it's better to leave it as is to use
sandbox.nativeMethods.consoleMeths[meth]
instead of
const nativeMeth = console + meth[0].toUpperCase() + meth.slice(1);
sandbox.nativeMethods[nativeMeth];
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. If you use a console method as key then let you leave it as is.
@@ -0,0 +1,66 @@ | |||
var consoleSandbox = hammerhead.sandbox.console; | |||
|
|||
function argsToArray (args) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array.prototype.slice.call
?
originMethods[meth].apply(console, args); | ||
} | ||
|
||
window.console = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To capture console trace you override methods that already overriden in sandbox.
Usually, we use an another way - we override the appropriate native methods.
See tests in https://github.com/DevExpress/testcafe-hammerhead/blob/master/test/client/fixtures/sandbox/node/methods-test.js
Use this way too.
window.console.info('7', '8'); | ||
/* eslint-enable no-console */ | ||
|
||
deepEqual(handledEvents, ['log', 'warn', 'error', 'info']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handledConsoleMethodNames
|
||
deepEqual(handledEvents, ['log', 'warn', 'error', 'info']); | ||
deepEqual(log, '12345678'); | ||
deepEqual(logFromEvents, '12345678'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handledConsoleMethodArgs
src/client/sandbox/console.js
Outdated
|
||
const sandbox = this; | ||
|
||
const proxyConsoleMeth = meth => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this function as private member of ConsoleSandbox, I think it is more redable.
✅ Tests for the commit f87debf have passed. See details: |
src/client/sandbox/console.js
Outdated
attach (window) { | ||
super.attach(window); | ||
|
||
const sandbox = this; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch
FPR |
src/client/sandbox/console.js
Outdated
constructor () { | ||
super(); | ||
|
||
this.CONSOLE_METH_CALLED_EVENT = 'hammerhead|console|console-meth-called'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to 'hammerhead|event|console-meth-called'
this._proxyConsoleMeth('info'); | ||
this._proxyConsoleMeth('error'); | ||
this._proxyConsoleMeth('warn'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we override the console.dir
method too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess not.
https://developer.mozilla.org/en-US/docs/Web/API/Console/dir
Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user.
@@ -198,6 +200,7 @@ export default class Sandbox extends SandboxBase { | |||
this.node.attach(window); | |||
this.upload.attach(window); | |||
this.cookie.attach(window); | |||
this.console.attach(window); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we reattach ConsoleSandbox
after document.write
is called?
See reattach
method and DOCUMENT_CLEANED_EVENT
event.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems, we should
@@ -47,4 +47,16 @@ if (window.console && typeof window.console.log !== 'undefined') { | |||
info: originMethods.info | |||
}; | |||
}); | |||
|
|||
test('consoleMethCalled event after document.write', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'consoleMethod' event should be raised after document.write
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'consoleMethCalled' event...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep
FPR |
❌ Tests for the commit d5afa2a have failed. See details: |
@testcafe-build-bot \retest |
✅ Tests for the commit d5afa2a have passed. See details: |
I've rewritten things about iframes, take a look again |
…press#1312) * Implement ConsoleSandbox. Add the consoleMethCalled event. * Address remarks * Address Artem's remark * Address Miher's remark * Update test title * Reattach sandbox on the document cleaned event * Send the consoleMethCalled method from iframes to the top window. * Fix the test for IE * Comment the test for IE
It's for DevExpress/testcafe#1738