lib/wayfarer/networking/selenium.rb in wayfarer-0.4.0 vs lib/wayfarer/networking/selenium.rb in wayfarer-0.4.1

- old
+ new

@@ -1,70 +1,43 @@ # frozen_string_literal: true module Wayfarer module Networking class Selenium - def self.renew_on - [] # TODO: Figure out when to renew - end + include Strategy - FAKE_STATUS_CODE = 200 - FAKE_RESPONSE_HEADERS = {}.freeze + MOCK_STATUS_CODE = 200 + MOCK_RESPONSE_HEADERS = {}.freeze - attr_reader :browser - - def initialize - @browser = instantiate_browser + def create + ::Selenium::WebDriver.for(Wayfarer.config.selenium.driver, **options) end - def fetch(url) - browser.navigate.to(url) - Result::Success.new(live(nil)) + def destroy(instance) + instance.quit end - def live(_) - Wayfarer::Page.new(url: browser.current_url, - body: browser.page_source, - status_code: FAKE_STATUS_CODE, - headers: FAKE_RESPONSE_HEADERS) + def navigate(instance, url) + instance.navigate.to(url) end - def body - browser.page_source + def live(instance) + success(url: instance.current_url, + body: instance.page_source, + status_code: MOCK_STATUS_CODE, + headers: MOCK_RESPONSE_HEADERS) 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) + def options + Wayfarer.config.selenium.options.merge(http_client: http_client) 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 + def http_client + ::Selenium::WebDriver::Remote::Http::Default.new.tap do |client| + client.read_timeout = Wayfarer.config.selenium.client_timeout + end end end end end