Capture browser console logs with capybara - javascript

I need to capture the console logs (category: info) of a browser using Ruby & Capybara. Until now I have tried using driver.manage.logs.get(:browser) or (:client) but, using this, the result is not what I want. It gives out the interaction results between selenium and browser where I can see my javascript statements sent for execution, but the resulting output fails to get captured.

Whether or not logs are available when using selenium depends on what browser you are using with Selenium. If you were using Firefox you'd be out of luck since it doesn't support the log retrieval API, however since you're using Chrome they are accessible. The issue you're having is that, by default, only WARN or ERROR level logs are captured. You can change this in the driver registration through the loggingPrefs capability
Selenium 3
Capybara.register_driver :logging_selenium_chrome do |app|
caps = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs:{browser: 'ALL'})
browser_options = ::Selenium::WebDriver::Chrome::Options.new()
# browser_options.args << '--some_option' # add whatever browser args and other options you need (--headless, etc)
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options, desired_capabilities: caps)
end
Selenium 4
Capybara.register_driver :logging_selenium_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_option("goog:loggingPrefs", {browser: 'ALL'})
browser_options = ::Selenium::WebDriver::Chrome::Options.new()
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app,
capabilities: options,
browser: :chrome)
end
end
and then specify to use :logging_selenium_chrome as your driver
Capybara.javascript_driver = :logging_selenium_chrome # or however else you're specifying which driver to use
which should then allow you to get the logs in your tests with
page.driver.browser.manage.logs.get(:browser)

Thomas Walpole answer is correct but it seems that nowadays if you are using chrome as your driver you should use
Selenium::WebDriver::Remote::Capabilities.chrome( "goog:loggingPrefs": { browser: 'ALL' } )
Notice goog:loggingPrefs instead of loggingPrefs only with this solution i was able to get console.log printed in the log.
Took me a while and got it from here https://intellipaat.com/community/5478/getting-console-log-output-from-chrome-with-selenium-python-api-bindings after several frustrating attempts.

November 2022 update, use:
page.driver.browser.logs.get(:browser)

Not sure that this is what you want, but take a look at https://github.com/dbalatero/capybara-chromedriver-logger.
It helps me identify the problem with dynamic modules import(''). Works both locally and in Github Actions / Circle CI by displaying failed loads of assets (which i believe outputs as console.error).

Related

shiny-server script resulting in 'Error in Calendar: could not find function "Calendar"'

R version: 4.1.0
Shiny Server v1.5.16.958
Node.js v12.20.0
I have a shiny-server script that I'm having issues with. When I call this script using a web browser, I immediately get: "Disconnected from the server. Reload". Upon inspecting the browser debug console, I received this debug information that I'm not quite sure what to do about. Does anyone have any ideas on how I can resolve this? :
Listening on http://127.0.0.1:46259
gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.
gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.
Attaching package: ‘gdata’
The following object is masked from ‘package:stats’:
nobs
The following object is masked from ‘package:utils’:
object.size
The following object is masked from ‘package:base’:
startsWith
Attaching package: ‘bizdays’
The following object is masked from ‘package:stats’:
offset
Warning: Error in Calendar: could not find function "Calendar"
[No stack trace available]
Error in Calendar(hdays, weekdays = c("saturday", "sunday")) :
could not find function "Calendar"
shiny-server-client.min.js:1 The application unexpectedly exited.
We have figured out the problem. Bizdays has deprecated the function 'Calendar'. In our Server.R file we had to update the code to use 'create.calender' instead.

Monitor console output of websites in Cypress.io

I am using Cypress.io to test our new application (Node, React), but I would like to be able to monitor and check the console.log output of the website for any errors that are not explicitly captured through our standard tests.
I am hoping that this will not only be used to capture stray console.log commands that should not be added to production-ready code, but will also capture deprecations and warnings from any of our dependencies.
Please can you let me know if this is achievable, and if so, how I would do it?
Thanks, Chris.
Check out this plug-in for Cypress that will print all browser logs to stdout:
https://github.com/flotwig/cypress-log-to-output
It only works for Chrome, so just make sure your CI is set up to use Chrome and you're using Chrome locally too.
let spyErrorLog;
before(() => {
Cypress.on("window:before:load", (win) => {
spyErrorLog = cy.spy(win.console, "error"); // can be: log, warn
});
});
after(() => {
expect(spyErrorLog).not.to.be.called;
});
from
see here Check if an error has been written to the console
https://docs.cypress.io/faq/questions/using-cypress-faq#How-do-I-spy-on-console-log
https://docs.cypress.io/examples/examples/recipes#Stubbing-and-spying

Selenium: Trying to resize window from Perl: getting a mysterious JavaScript error

I am using the Perl Selenium package, WWW::Selenium.
Trying to resize the browser window, I am getting a mysterious JavaScript error:
"Threw an exception: missing ; before statement".
Here is the code:
use strict;
use warnings;
use 5.014;
use autodie;
use warnings qw< FATAL utf8 >;
use Carp;
use Carp::Always;
use WWW::Selenium;
my $url = 'http://www.google.com'; #for example
my $sel = WWW::Selenium->new( host => 'localhost',
port => 4444,
browser => '*firefox F:\WIN 7 programs\Web & Internet\Firefox 8 bit\firefox.exe',
browser_url => $url,
);
$sel->open( $url );
$sel->wait_for_page_to_load(10000);
my $res = $sel->window_maximize(); # So far, this works fine
$res = $sel->get_eval( q{ WebDriver driver = ((WebDriverBackedSelenium) selenium).getWrappedDriver();
driver.manage().window().setSize(1040,720);} );
# (Following this: http://stackoverflow.com/questions/1522252/, Eli Colner's post)
The program then crashes here with:
"Threw an exception: missing ; before statement"
If I drop the first JavaScript line and just leave in the 2nd line, namely:
$res = $sel->get_eval( q{driver.manage().window().setSize(1040,720);} );
It bumps with: "driver not defined".
Help will be appreciated - Thanks in advance
Helen
Note: cross posted here: http://www.perlmonks.org/?node_id=1092355
I see invalid javascript in your code, you made a mistaken assumption. Regarding the referenced SO thread that you base your code on:
How to resize/maximize Firefox window during launching Selenium Remote Control?
what makes you think Eli Corner's answer/solution is "javascript"? That is Java, or C# otherwise, because only those language bindings for WebDriver (or Selenium 2) expose a WebDriverBackedSelenium feature. All other language bindings, including Perl have no such option. So even if the code syntax is correct, on execution it will fail because that's not javascript (or shall I say the referenced classes/objects are not javascript).
Your options for a solution the way I see it are:
use real javascript code and Dave Hunt's solution (in that same SO thread) ideally should work, adapted for Perl:
$sel->get_eval("window.resizeTo(1024, 768); window.moveTo(0,0);");
use Perl WebDriver binding to correctly use Eli Corner's solution (adapted for Perl), not Selenium (RC) binding that you are currently using. Perl WebDriver binding is Selenium::Remote::Driver, not WWW:Selenium. You should then be able to do something like this (there is no need for the WebDriverBackedSelenium part in Perl, but it does mean you have to switch off using Selenium RC moving to WebDriver, there's no backward compatibility support, you need Java or C# for that):
$driver->set_window_position(0, 0);
$driver->set_window_size(640, 480);

Firefox add-on won't open its own file as a new xul window.

My add-on creates a FireFox File menu command that triggers callback function 'launchApp'.
function launchApp() {
var ww = Cc["#mozilla.org/embedcomp/window-watcher;1"]
.getService(Components.interfaces.nsIWindowWatcher);
var appUrl='chrome://mrT2/mrT00.xul'; // production (fails)
var appUrl='file:///C:/mpa/##mrT-2.0/mrT00.xul'; // testing (works)
var win = ww.openWindow(null, appUrl, "mrT2-window", "chrome,resizable", null);
// Summary of results of ww.openWindow() for various appUrl values:
// 'chrome:///mrT2/mrT00.xul' 'No chrome package registered for ...' (true)
// 'chrome://mrT00.xul' 'Invalid chrome URI: /' (true)
// 'chrome:///mrT00.xul' and 'chrome://mrT2/mrT00.xul' seem valid yet both give:
//Error: NS_ERROR_ILLEGAL_VALUE: Component returned failure code: 0x80070057 ...
// ... (NS_ERROR_ILLEGAL_VALUE) [nsIWindowWatcher.openWindow] (unexplained)
return true;
The above code works nicely and is great for testing mrT00.xul (because it collects the file directly from where I am editing it).
However when I interchange the two appUrl vars to try and open the exact same file as shipped via the xpi (and now internal to firefox) I get the dreaded 'illegal value' 0x80070057.
After 2 long days of research and study I cannot fault my code. Can you?
Otherwise, how may I begin tracing nsiWindowWatcher to pinpoint the error?
Bad things can happen when an extension attempts to open a xul file outside the /content directory or inside it when the chrome.manifest file in the .xpi root is not in order. Firefox handling of both these situations is not above reproach, warnings being offered in neither case.

RSpec: Is there a setting to have js: true for all the specs?

Whenever a request spec fails, I use respec gem to rerun failed examples. When doing this, I want to activate :chrome as driver (instead of headless :webkit) so I can inspect the problem visually within Chrome.
Instead of adding a driver: :chrome manually to the test case, I'd like to specify it globally like so:
# spec_helper.rb
Capybara.javascript_driver = if defined?(Respec)
:chrome # Use headed browser to inspect stuff visually when respec'ing
else
:webkit # Otherwise use headless browser
end
Sadly this doesn't seem to work for specs that don't have JavaScript enabled. Is there a way to switch JS on for all specs? Something like:
# spec_helper.rb
Capybara.javascript_driver = if defined?(Respec)
Capybara.javascript = true
:chrome # Use headed browser to inspect stuff visually when respec'ing
else
:webkit # Otherwise use headless browser
end
Thanks
javascript_driver is used only for scenarios with #javascript tag.
You should modify default_driver instead of javascript_driver if you want to run all scenarios with chrome:
if defined?(Respec)
Capybara.default_driver = :chrome
else
Capybara.default_driver = :webkit
Capybara.javascript_driver = :chrome
end

Categories