Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Commit

Permalink
did some refactoring, fixing, and test writing
Browse files Browse the repository at this point in the history
  • Loading branch information
sosodev committed Oct 10, 2020
1 parent 398afda commit 5fcd418
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 34 deletions.
73 changes: 65 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,42 +1,99 @@
PATH
remote: .
specs:
kinchan (0.1.0)
kinchan (0.3.0)
require_all (~> 3.0.0)
selenium-webdriver (~> 3.142.6)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.1)
backport (1.1.2)
benchmark (0.1.0)
childprocess (3.0.0)
diff-lcs (1.3)
diff-lcs (1.4.4)
e2mmap (0.1.0)
jaro_winkler (1.5.4)
kwalify (0.7.2)
maruku (0.7.3)
mini_portile2 (2.4.0)
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
parallel (1.19.2)
parser (2.7.2.0)
ast (~> 2.4.1)
psych (3.1.0)
rainbow (3.0.0)
rake (13.0.1)
reek (6.0.1)
kwalify (~> 0.7.0)
parser (>= 2.5.0.0, < 2.8, != 2.5.1.1)
psych (~> 3.1.0)
rainbow (>= 2.0, < 4.0)
regexp_parser (1.8.1)
require_all (3.0.0)
reverse_markdown (2.0.0)
nokogiri
rexml (3.2.4)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.1)
rspec-support (~> 3.9.1)
rspec-expectations (3.9.0)
rspec-core (3.9.3)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.2)
rubyzip (2.0.0)
rspec-support (3.9.3)
rubocop (0.93.0)
parallel (~> 1.10)
parser (>= 2.7.1.5)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8)
rexml
rubocop-ast (>= 0.6.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (0.7.1)
parser (>= 2.7.1.5)
ruby-progressbar (1.10.1)
rubyzip (2.3.0)
selenium-webdriver (3.142.7)
childprocess (>= 0.5, < 4.0)
rubyzip (>= 1.2.2)
solargraph (0.39.17)
backport (~> 1.1)
benchmark
bundler (>= 1.17.2)
e2mmap
jaro_winkler (~> 1.5)
maruku (~> 0.7, >= 0.7.3)
nokogiri (~> 1.9, >= 1.9.1)
parser (~> 2.3)
reverse_markdown (>= 1.0.5, < 3)
rubocop (~> 0.52)
thor (~> 1.0)
tilt (~> 2.0)
yard (~> 0.9, >= 0.9.24)
thor (1.0.1)
tilt (2.0.10)
unicode-display_width (1.7.0)
yard (0.9.25)

PLATFORMS
ruby

DEPENDENCIES
kinchan!
rake (~> 13.0)
reek (~> 6.0)
rspec (~> 3.9.0)
rubocop (~> 0.9)
solargraph

BUNDLED WITH
2.1.2
2.1.4
3 changes: 3 additions & 0 deletions kinchan.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'require_all', ['~> 3.0.0']

spec.add_development_dependency 'rspec', ['~> 3.9.0']
spec.add_development_dependency 'reek', ['~> 6.0']
spec.add_development_dependency 'rubocop', ['~> 0.9']
spec.add_development_dependency 'solargraph'
end
69 changes: 44 additions & 25 deletions lib/kinchan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'selenium-webdriver'
require 'require_all'

# Top-level module contains all the juicy bits for the gem
module Kinchan
@browser = :chrome
@browser_options = nil
Expand All @@ -12,18 +13,24 @@ 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)
Expand All @@ -35,7 +42,7 @@ def initialize(**options)
@after_tasks = []
@options = options

Task.start_browser
Task.start_browser unless defined?(@@browser_webdriver)
end

def self.inherited(subclass)
Expand All @@ -47,35 +54,47 @@ def self.find_task(task_symbol)
end

def self.start_browser
if @@browser_webdriver.nil?
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
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

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

@after_tasks.each do |task_hash|
task = Task.find_task(task_hash[:task])
task.new(**task_hash[:options]).public_send('run') unless task.nil?
def run_tasks(tasks)
tasks.each do |task_hash|
get_task(task_hash)&.public_send('run')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kinchan/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Kinchan
VERSION = '0.2.0'
VERSION = '0.3.0'
end
12 changes: 12 additions & 0 deletions spec/kinchan_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require_relative '../lib/kinchan'
require_relative 'tasks/google'

RSpec.describe Kinchan do
it 'defines the default browser' do
Expand All @@ -19,6 +21,16 @@
Kinchan.browser_options = :dummy_options
expect(Kinchan.browser_options).to eq(:dummy_options)
end

it 'can restore the default browser settings' do
Kinchan.restore_defaults
expect(Kinchan.browser).to eq(:chrome)
expect(Kinchan.browser_options).to eq(nil)
end

it 'can run the test tasks' do
Google::HighLevel::SearchForTerm.new(query: 'kinchan').run
end
end

RSpec.describe Kinchan::Task do
Expand Down
33 changes: 33 additions & 0 deletions spec/tasks/google.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative '../../lib/kinchan'

module Google
class VisitHomePage < Kinchan::Task
def execute(browser)
browser.navigate.to 'https://www.google.com'
end
end

class ExecuteSearch < Kinchan::Task
def initialize(**options)
super

@query = options[:query]
end

def execute(browser)
browser.find_element(:name, 'q')&.send_keys(@query) # input the query
browser.find_element(:id, 'tsf')&.submit # submit the search form
end
end

module HighLevel
class SearchForTerm < Kinchan::Task
def initialize(**options)
super

@before_tasks << { task: :VisitHomePage }
@after_tasks << { task: :ExecuteSearch, options: options }
end
end
end
end

0 comments on commit 5fcd418

Please sign in to comment.