Skip to content

Instantly share code, notes, and snippets.

@timdiggins
Created June 4, 2021 12:26
Show Gist options
  • Save timdiggins/018804a4a9226c2bfab581551b0479a9 to your computer and use it in GitHub Desktop.
Save timdiggins/018804a4a9226c2bfab581551b0479a9 to your computer and use it in GitHub Desktop.
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'capybara'
# gem 'capybara', path: "/Users/twalpole/projects/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] ] }
Capybara.register_driver :selenium_chrome_headless do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument("no-sandbox") # or setup user per https://github.com/GoogleChromeLabs/lighthousebot/blob/master/builder/Dockerfile#L35-L40
options.add_argument("headless")
options.add_argument("window-size=1280x1280")
# options.add_argument("disable-gpu") # only windows? still? https://developers.google.com/web/updates/2017/04/headless-chrome
# comment out this line to fix it
options.add_option("w3c", false)
# FYI was doing this because of https://stackoverflow.com/questions/56522499/cromedriver-driver-manage-logs-getbrowser-fails-on-chromedriver-75-0-3770-8 but no longer needed
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
# https://stackoverflow.com/questions/46278514/capture-browser-console-logs-with-capybara
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
Capybara.register_driver :selenium_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
[
"window-size=1280x1280"
].each { |arg| options.add_argument(arg) }
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
%i[selenium_chrome selenium_chrome_headless].each do |driver|
puts "Using #{driver}"
sess = Capybara::Session.new(driver, app)
sess.visit('/')
sess.assert_selector(:field, 'sometextareaid', with: /\s*initial value\s*/)
puts "working fine with #{driver}"
end
__END__
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello!</title>
</head>
<body>
<textarea id="sometextareaid">
initial value
</textarea>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment