Skip to content

Commit

Permalink
Introduce driver registration (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmcgarvey authored Jan 14, 2022
1 parent 7f43cb7 commit da9c947
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 87 deletions.
5 changes: 4 additions & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ require "./support/**"

include LuckyFlow::Expectations

LuckyFlow::Spec.setup

server = TestServer.new(3002)

Spec.before_each do
Expand All @@ -16,8 +18,9 @@ LuckyFlow.configure do |settings|
settings.stop_retrying_after = 40.milliseconds
end

Habitat.raise_if_missing_settings!

Spec.after_suite do
LuckyFlow.shutdown
server.close
end

Expand Down
13 changes: 13 additions & 0 deletions src/ext/spec/item.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Spec
module Item
# :nodoc:
def _lucky_flow_all_tags : Set(String)
all_tags = tags || Set(String).new
temp = parent
if temp.is_a?(Spec::Item)
all_tags += temp._lucky_flow_all_tags
end
all_tags
end
end
end
50 changes: 32 additions & 18 deletions src/lucky_flow.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,45 @@ class LuckyFlow; end

require "./lucky_flow/**"
require "file_utils"
require "./ext/spec/item"

class LuckyFlow
include LuckyFlow::Expectations
SERVER = LuckyFlow::Server::INSTANCE

@@driver : LuckyFlow::Driver?

class_property default_driver : String = "headless_chrome"

Habitat.create do
setting screenshot_directory : String = "./tmp/screenshots"
setting base_uri : String
setting retry_delay : Time::Span = 10.milliseconds
setting stop_retrying_after : Time::Span = 1.second
setting driver_path : String?
setting browser_binary : String? = nil
setting driver : LuckyFlow::Driver.class = LuckyFlow::Drivers::HeadlessChrome
end

def HabitatSettings.chromedriver_path=(_chromedriver_path)
{% raise "'chromedriver_path' has been renamed to 'driver_path'" %}
def self.driver : LuckyFlow::Driver
@@driver ||= LuckyFlow::Registry.get_driver(self.default_driver)
end

def self.driver(name : String) : LuckyFlow::Driver
@@driver = LuckyFlow::Registry.get_driver(self.default_driver)
end

def self.session : Selenium::Session
driver.session
end

def self.shutdown : Nil
LuckyFlow::Registry.shutdown_all
end

def self.use_default_driver
@@driver = nil
end

def self.reset : Nil
@@driver.try(&.reset)
end

def visit(path : String)
Expand Down Expand Up @@ -52,9 +74,9 @@ class LuckyFlow

def take_screenshot(filename : String = generate_screenshot_filename, fullsize : Bool = true)
if fullsize
with_fullsized_page { session.screenshot(filename) }
with_fullsized_page { driver.screenshot(filename) }
else
session.screenshot(filename)
driver.screenshot(filename)
end
end

Expand Down Expand Up @@ -188,19 +210,11 @@ class LuckyFlow
STDIN.gets
end

def session
def session : Selenium::Session
self.class.session
end

def self.session
SERVER.session
end

def self.shutdown
SERVER.shutdown
end

def self.reset
SERVER.reset
def driver : LuckyFlow::Driver
self.class.driver
end
end
17 changes: 17 additions & 0 deletions src/lucky_flow/driver.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
abstract class LuckyFlow::Driver
@retry_limit : Time = 2.seconds.from_now

getter session : Selenium::Session { start_session }

abstract def start_session : Selenium::Session
abstract def stop

def reset : Nil
@session.try &.cookie_manager.delete_all_cookies
end

def shutdown : Nil
@session.try &.delete
stop
end

def screenshot(path : String)
FileUtils.mkdir_p(File.dirname(path))
session.screenshot(path)
end

protected def retry_start_session(e)
if Time.utc <= @retry_limit
Expand Down
40 changes: 21 additions & 19 deletions src/lucky_flow/drivers/chrome.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
class LuckyFlow::Drivers::Chrome < LuckyFlow::Driver
private getter driver : Selenium::Driver do
service = Selenium::Service.chrome(driver_path: driver_path)
Selenium::Driver.for(:chrome, service: service)
end

@capabilities : Selenium::Chrome::Capabilities

def initialize(&block)
@capabilities = Selenium::Chrome::Capabilities.new
yield @capabilities
end

def start_session : Selenium::Session
capabilities = Selenium::Chrome::Capabilities.new
capabilities.chrome_options.args = args
capabilities.chrome_options.binary = browser_binary
driver.create_session(capabilities)
driver.create_session(@capabilities)
rescue e : IO::Error
retry_start_session(e)
end
Expand All @@ -12,26 +21,19 @@ class LuckyFlow::Drivers::Chrome < LuckyFlow::Driver
@driver.try(&.stop)
end

protected def args : Array(String)
[] of String
end

@driver : Selenium::Driver?

private def driver : Selenium::Driver
@driver ||= begin
service = Selenium::Service.chrome(driver_path: driver_path)
Selenium::Driver.for(:chrome, service: service)
end
end

private def driver_path
LuckyFlow.settings.driver_path || Webdrivers::Chromedriver.install
rescue err
raise DriverInstallationError.new(err)
end
end

LuckyFlow::Registry.register :chrome do
LuckyFlow::Drivers::Chrome.new { }
end

private def browser_binary : String?
LuckyFlow.settings.browser_binary
LuckyFlow::Registry.register :headless_chrome do
LuckyFlow::Drivers::Chrome.new do |config|
config.chrome_options.args = ["no-sandbox", "headless", "disable-gpu"]
end
end
5 changes: 0 additions & 5 deletions src/lucky_flow/drivers/headless_chrome.cr

This file was deleted.

21 changes: 21 additions & 0 deletions src/lucky_flow/registry.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class LuckyFlow::Registry
@@registry = Hash(String, Proc(LuckyFlow::Driver)).new
@@running_registry = Hash(String, LuckyFlow::Driver).new

def self.register(name : String | Symbol, &block : -> LuckyFlow::Driver)
@@registry[name.to_s] = block
end

def self.available : Set(String)
Set.new(@@registry.keys)
end

def self.get_driver(name : String) : LuckyFlow::Driver
@@running_registry[name] ||= @@registry[name].call
end

def self.shutdown_all
@@running_registry.values.each(&.shutdown)
@@running_registry.clear
end
end
44 changes: 0 additions & 44 deletions src/lucky_flow/server.cr

This file was deleted.

18 changes: 18 additions & 0 deletions src/lucky_flow/spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module LuckyFlow::Spec
macro setup
Spec.around_each do |spec|
if driver_name = (spec.example._lucky_flow_all_tags & LuckyFlow::Registry.available).first?
LuckyFlow.driver(driver_name)
end

spec.run

LuckyFlow.reset
LuckyFlow.use_default_driver
end

Spec.after_suite do
LuckyFlow.shutdown
end
end
end

0 comments on commit da9c947

Please sign in to comment.