lib/kinchan.rb in kinchan-0.2.0 vs lib/kinchan.rb in kinchan-0.3.0
- old
+ new
@@ -2,30 +2,37 @@
require 'kinchan/version'
require 'selenium-webdriver'
require 'require_all'
+# Top-level module contains all the juicy bits for the gem
module Kinchan
@browser = :chrome
@browser_options = nil
def self.browser
@browser
end
- def self.browser= b
- @browser = b
+ def self.browser=(browser)
+ @browser = browser
end
def self.browser_options
@browser_options
end
- def self.browser_options= bo
- @browser_options = bo
+ def self.browser_options=(browser_options)
+ @browser_options = browser_options
end
+ def self.restore_defaults
+ @browser = :chrome
+ @browser_options = nil
+ end
+
+ # A single unit of automation in Kinchan
class Task
singleton_class.send(:attr_accessor, :browser)
singleton_class.send(:attr_accessor, :browser_options)
singleton_class.send(:attr_reader, :descendants)
@descendants = []
@@ -33,11 +40,11 @@
def initialize(**options)
@before_tasks = []
@after_tasks = []
@options = options
- Task.start_browser
+ Task.start_browser unless defined?(@@browser_webdriver)
end
def self.inherited(subclass)
Task.descendants << subclass
end
@@ -45,36 +52,50 @@
def self.find_task(task_symbol)
Task.descendants.select { |task| task.name.split('::').last.downcase == task_symbol.to_s.downcase }[0]
end
def self.start_browser
- if Kinchan.browser_options.nil?
- @@browser_webdriver = Selenium::WebDriver.for Kinchan.browser
- else
- @@browser_webdriver = Selenium::WebDriver.for(Kinchan.browser, options: Kinchan.browser_options)
- end
+ browser = Kinchan.browser
+ browser_options = Kinchan.browser_options
+
+ @@browser_webdriver = if browser_options.nil?
+ Selenium::WebDriver.for browser
+ else
+ Selenium::WebDriver.for(browser, options: browser_options)
+ end
end
def self.restart_browser
- unless @@browser_webdriver.nil?
- @@browser_webdriver.close
- @@browser_webdriver = Selenium::WebDriver.for Kinchan.browser
- end
+ return if @@browser_webdriver.nil?
+
+ @@browser_webdriver.close
+ @@browser_webdriver = Selenium::WebDriver.for Kinchan.browser
end
- def execute(browser); end
+ def execute(_browser); end
def run
- @before_tasks.each do |task_hash|
- task = Task.find_task(task_hash[:task])
- task.new(**task_hash[:options]).public_send('run') unless task.nil?
- end
-
+ run_tasks(@before_tasks)
execute(@@browser_webdriver)
+ run_tasks(@after_tasks)
+ end
- @after_tasks.each do |task_hash|
- task = Task.find_task(task_hash[:task])
- task.new(**task_hash[:options]).public_send('run') unless task.nil?
+ private
+
+ def get_task(task_hash)
+ task = Task.find_task(task_hash[:task])
+ options = task_hash[:options]
+
+ if options.nil?
+ task.new
+ else
+ task.new(**options)
+ end
+ end
+
+ def run_tasks(tasks)
+ tasks.each do |task_hash|
+ get_task(task_hash)&.public_send('run')
end
end
end
end