Skip to content

Instantly share code, notes, and snippets.

@schneems
Created Dec 27, 2018
Embed
What would you like to do?
require 'capybara'
Capybara.register_driver :selenium_chrome_headless_no_scroll do |app|
Capybara::Selenium::Driver.load_selenium
browser_options = ::Selenium::WebDriver::Chrome::Options.new
browser_options.args << '--headless'
browser_options.args << '--disable-gpu' if Gem.win_platform?
browser_options.args << '--hide-scrollbars'
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end
module Rundoc
class CodeCommand
class Website
end
end
end
class Rundoc::CodeCommand::Website
class Driver
def self.tasks
@tasks
end
@tasks = {}
def self.add(name, value)
raise "Task named #{name.inspect} is already started, choose a different name" if @tasks[name]
@tasks[name] = value
end
def self.find(name)
raise "Could not find task with name #{name.inspect}, known task names: #{@tasks.keys.inspect}" unless @tasks[name]
@tasks[name]
end
def self.next_screenshot_name
@count ||= 0
@count += 1
"screenshot_#{@count}.png"
end
attr_reader :session
def initialize(name: "name", url: )
@session = Capybara::Session.new(:selenium_chrome_headless_no_scroll)
end
def visit(url)
@session.visit(url)
end
def screenshot
filename = "tmp/#{self.class.next_screenshot_name}"
session.save_screenshot(filename)
filename
end
def current_url
session.current_url
end
def scroll(value = 100)
session.execute_script "window.scrollBy(0,#{value})"
end
def teardown
session.reset_session!
end
end
end
url = "https://www.google.com"
driver2 = Rundoc::CodeCommand::Website::Driver.new(name: "google", url: url)
driver2.visit(url)
driver2.screenshot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment