# frozen_string_literal: true module Wayfarer module Networking class Selenium def self.renew_on [] # TODO: Figure out when to renew end FAKE_STATUS_CODE = 200 FAKE_RESPONSE_HEADERS = {}.freeze attr_reader :browser def initialize @browser = instantiate_browser end def fetch(url) browser.navigate.to(url) Result::Success.new(live(nil)) end def live(_) Wayfarer::Page.new(url: browser.current_url, body: browser.page_source, status_code: FAKE_STATUS_CODE, headers: FAKE_RESPONSE_HEADERS) end def body browser.page_source end def renew free @browser = instantiate_browser end def free browser&.quit @browser = nil @capybara = nil end def capybara @capybara ||= instantiate_capybara_driver end private def instantiate_browser ::Selenium::WebDriver.for(*Wayfarer.config.selenium_argv) end def instantiate_capybara_driver Capybara.run_server = false Capybara.current_driver = :selenium capybara_driver = Capybara::Selenium::Driver.new(nil) capybara_driver.instance_variable_set(:@capybara, browser) session = Capybara::Session.new(:selenium, nil) session.instance_variable_set(:@driver, capybara_driver) session end end end end