1

I have some automated tests written in TruClient scripts and I want to verify what was printed in the application JS console.

Is there any way to access that from the javascript?

Like console.getText() type of thing?

I want to verify that some information appeared in the console.

For instance, how can I do this?

console.log("Hi");
//Now I want to check if it was printed correctly in the browser
if(console.getText() == "Hi")
{
   //then test passed
}

I am needing this because we are architecting new browsers.

Loop
  • 233
  • 1
  • 3
  • 13
  • It's in the `console`. Did you put it there? Because, if you did, you should know when the `console.log()`s or whatever happened. – StackSlave Jun 10 '19 at 07:17
  • Yes, it is there, and I can check it manually. But I want to check it programatically, automatically, via an API through a script. Preferably through Javascript. – Loop Jun 10 '19 at 07:23
  • I don't know of a way to check what has been consoled. Maybe you want to make your own API, but it's a waste of time. The console is where you look to see if there is a warning or an error. If there was some sort of problem, it should be dealt with. You use the console to test for the problem, why test for the testing of the problem? Or should I make an API to test for that? – StackSlave Jun 10 '19 at 07:34
  • Basically, that's what I want to test, but it's probably overkill. We are hooking console functionality in a new browser, and we want to check it works. – Loop Jun 10 '19 at 07:49

1 Answers1

3

You might be able to intercept calls to console by redefining them:

// Save original console methods
var originalConsole = {
  log: console.log,
  warn: console.warn,
  error: console.error
}

var consoleHistory = [];

console.log = function() {
    // Save inputs given to console.log()
    consoleHistory.push(arguments);

    // Make call to original console
    originalConsole.log.apply(window.console, arguments);
}

// Repeat for warn and error, if needed.

Eloquent JavaScript uses this technique to display errors when evaluating JS in its code sandbox.

AuxTaco
  • 4,883
  • 1
  • 12
  • 27
Leftium
  • 16,497
  • 6
  • 64
  • 99