Skip to content
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

Merged
merged 9 commits into from
Sep 26, 2017

Conversation

AlexanderMoskovkin
Copy link
Contributor

const sandbox = this;

const proxyConsoleMeth = meth => {
sandbox.window.console[meth] = function () {
Copy link
Contributor

@miherlosev miherlosev Sep 14, 2017

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);
}

@@ -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
Copy link
Contributor

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

constructor () {
super();

this.CONSOLE_METH_CALLED = 'hammerhead|console|console-meth-called-event';
Copy link
Contributor

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

// Console
this.console = win.console;

this.consoleMeths = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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];

Copy link
Contributor

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) {
Copy link
Contributor

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 = {
Copy link
Contributor

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']);
Copy link
Contributor

@miherlosev miherlosev Sep 14, 2017

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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handledConsoleMethodArgs


const sandbox = this;

const proxyConsoleMeth = meth => {
Copy link
Contributor

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.

@testcafe-build-bot
Copy link
Collaborator

✅ Tests for the commit f87debf have passed. See details:

attach (window) {
super.attach(window);

const sandbox = this;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this variable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch

@AlexanderMoskovkin
Copy link
Contributor Author

FPR

constructor () {
super();

this.CONSOLE_METH_CALLED_EVENT = 'hammerhead|console|console-meth-called';
Copy link
Contributor

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');
}
Copy link
Contributor

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?

Copy link
Contributor Author

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);
Copy link
Contributor

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.

Copy link
Contributor Author

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 () {
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'consoleMethCalled' event...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

@AlexanderMoskovkin
Copy link
Contributor Author

FPR

@testcafe-build-bot
Copy link
Collaborator

❌ Tests for the commit d5afa2a have failed. See details:

@AlexanderMoskovkin
Copy link
Contributor Author

@testcafe-build-bot \retest

@testcafe-build-bot
Copy link
Collaborator

✅ Tests for the commit d5afa2a have passed. See details:

@AlexanderMoskovkin
Copy link
Contributor Author

AlexanderMoskovkin commented Sep 26, 2017

I've rewritten things about iframes, take a look again
/cc @miherlosev @LavrovArtem

@miherlosev miherlosev merged commit 47908e6 into DevExpress:master Sep 26, 2017
AndreyBelym pushed a commit to AndreyBelym/testcafe-hammerhead that referenced this pull request Feb 28, 2019
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants