Get Browser Console Logs support
Because lot of information is shown in console log, it would be great if there is any chance how to capture this logs.
Solution Maybe it should be feasible to have something like 'recordHar' when opening context to save logs to file, or even to expose GLOBAL variable with console logs?
Alternatives I tried to use custom js keyword, promises etc but nothing was successful. Maybe there is some way but I did not found it yet.
I also tried to fire promise or some kind of 'background' task (using google, because I'm not Js developer) but nothing was able to capture logs.
@allcontributors please add @tomaspekarovic for ideas
ok I found out, that
async function openNewPage(url, page, logger) {
page.on('console', msg => logger("console text: " + msg.text()));
await page.goto(url);
await page.waitForTimeout(15000);
}
Can be used to handle logs, but the problem is, that page.on event does not start to listening on every console event. It just works only in context of the keyword.
So keyword is listening for 15 s. Maybe the solution is to somehow enable to send on events to page when the page is created?
New Page https://google.sk on_event=(console, msg => logger('console text: ' + msg.text()))
function msg => logger('console text: ' + msg.text())
New Page https://google.sk on_event=(console, function)
Maybe even to set more 'on_event' hooks to the page. This would definitely helps not only for console logs, but also for automatic handling of events that can occur on the page.
I know that I can create JS keywords but the on event will be only in the scope of keyword. If it will be in the general scope, I can handle popups, console logs, and everything that playwright offers.
Also maybe there is any keyword/possibility to 'return' that page object and apply it in general ? maybe just to apply that event?
page.on event should work in background, example we already set close event and it works nicely. Also console are already collected. So in theory, one should write a keyword which collects the console messages from consoleMessages list.
Thanks Tatu, at the end I found out that there is way how to get console logs.
Keyword Close Page will return console logs and error logs right after the page is closed. I will try to get inspired and if nobody will be faster than me I'll try to create PR (hopefully)
As this is already possible with Close Page I'll take the high prio away.
I would like to expand on this request:
Close Page indeed returns console logs, but we're relying on automatic page closing. So my next idea was to use an initialization file and add the Close Page keyword in the Test Teardown. Unfortunately, this is a default teardown and is not executed when there is a teardown specified in the test case. Seems there is no way to run a teardown on every test in robot framework at this moment. I found a ticket that requested this feature, but it was rejected in favor of using Listeners.
I also tried a jsextension with page.on('console', ...), but didn't get it to work.
The only solution I see at this moment is to add Close Page in every teardown, but that's tedious ofcourse. I also tried Listeners, but I couldn't figure out how to connect to the running browser instance..
I'd like getters for the errors and console lists of the current active page instead of having to close the page explicitly. TBH, it doesn't matter in practice in my use case; I just think it looks kludgey; especially the list wrapping the dictionary. Maybe I'm missing some obvious way of shortening this?
@{msgs} Close Page
&{dict} Set Variable ${msgs}[0]
Log Many @{dict.errors}
Log Many @{dict.console}
@jin-harmoney I've been missing a way to call the default teardown in the test case teardown in cases when I just need to do a little extra cleanup, but it sounds like you'd need a way to add "teardown triggers" to all teardowns in a suite. Sounds useful.
Will be implemented by #2627