Skip to content

Instantly share code, notes, and snippets.

@timdiggins
Last active June 6, 2021 12:06
Show Gist options
  • Save timdiggins/65c4371a71d025ec9d9192ee4848a4e0 to your computer and use it in GitHub Desktop.
Save timdiggins/65c4371a71d025ec9d9192ee4848a4e0 to your computer and use it in GitHub Desktop.
demonstration of lack of need for non-w3 mode to retrieve logs from chromedriver
Using selenium_chrome
Capybara starting Puma...
* Version 5.3.1 , codename: Sweetnighter
* Min threads: 0, max threads: 4
* Listening on http://127.0.0.1:59751
browser logs empty from selenium_chrome
--------------------------------------------------------------------------------
Using selenium_chrome_w3c
browser logs empty from selenium_chrome_w3c
--------------------------------------------------------------------------------
Using selenium_chrome_non_w3c
{:browser_logs_class=>"Array", :v=>[#<Selenium::WebDriver::LogEntry:0x00007fddfcaf2cb0 @level="INFO", @timestamp=1622979812498, @message="http://127.0.0.1:59751/ 9:10 \"Logs r us\"">]}
got logs from selenium_chrome_non_w3c
--------------------------------------------------------------------------------
Using selenium_chrome_headless
browser logs empty from selenium_chrome_headless
--------------------------------------------------------------------------------
Using selenium_chrome_headless_w3c
browser logs empty from selenium_chrome_headless_w3c
--------------------------------------------------------------------------------
Using selenium_chrome_headless_non_w3c
{:browser_logs_class=>"Array", :v=>[#<Selenium::WebDriver::LogEntry:0x00007fde0c1f27e0 @level="INFO", @timestamp=1622979814739, @message="http://127.0.0.1:59751/ 9:10 \"Logs r us\"">]}
got logs from selenium_chrome_headless_non_w3c
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'capybara'
gem 'puma'
gem 'selenium-webdriver'
gem 'webdrivers'
gem 'byebug'
gem 'rexml'
end
require 'selenium-webdriver'
require "capybara/dsl"
Capybara.default_max_wait_time = 5
html = DATA.read
app = proc { |env| [200, { "Content-Type" => "text/html" }, [html]] }
["w3c", "non_w3c"].each do |opt|
["headless_", nil].each do |headless_opt|
Capybara.register_driver "selenium_chrome_#{headless_opt}#{opt}".to_sym do |app|
options = Selenium::WebDriver::Chrome::Options.new
if headless_opt
options.add_argument("no-sandbox") # or setup user per https://github.com/GoogleChromeLabs/lighthousebot/blob/master/builder/Dockerfile#L35-L40
options.add_argument("headless")
end
options.add_argument("window-size=1280x1280")
# options.add_argument("disable-gpu") # only windows? still? https://developers.google.com/web/updates/2017/04/headless-chrome
if opt == "non_w3c"
options.add_option("w3c", false)
end
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
# https://stackoverflow.com/a/59778547/109175
"goog:loggingPrefs": { browser: "ALL" }
)
service = Selenium::WebDriver::Service.chrome(
args: {
verbose: true,
log_path: "./chromedriver.log"
}
)
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options, desired_capabilities: capabilities, service: service)
end
end
end
%i[selenium_chrome selenium_chrome_w3c selenium_chrome_non_w3c selenium_chrome_headless selenium_chrome_headless_w3c selenium_chrome_headless_non_w3c].each do |driver|
puts "-" * 80
puts "Using #{driver}"
sess = Capybara::Session.new(driver, app)
sess.visit('/')
sess.assert_selector("p#some-id", text: "Other content")
browser_logs = sess.driver.browser.manage.logs.get(:browser)
if !browser_logs
puts "no browser logs got from #{driver}"
elsif browser_logs.empty?
puts "browser logs empty from #{driver}"
else
p(browser_logs_class: browser_logs.class.name, v: browser_logs)
puts "got logs from #{driver}"
end
end
__END__
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello!</title>
</head>
<body>
<p id="some-id">Some content</p>
<script>
console.log("Logs r us")
document.getElementById("some-id").innerText = "Other content"
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment