Skip to content

Commit

Permalink
Cleanup and refactor WatirSpec
Browse files Browse the repository at this point in the history
* Remove possibility to serve HTML files without server (it's pretty
  fast to start it anyways)
* Always persist browser between specs
* Remove unused Debugger code
* Move implementation specs to separate file
  • Loading branch information
p0deje committed Oct 3, 2016
1 parent 6579864 commit ee22451
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 89 deletions.
26 changes: 6 additions & 20 deletions lib/watirspec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require "tmpdir"
require 'watirspec/guards'
require 'watirspec/implementation'
require 'watirspec/runner'
require 'watirspec/server'

module WatirSpec
class << self
attr_accessor :browser_args, :persistent_browser, :unguarded, :implementation, :always_use_server

def root
@root ||= Dir.pwd
end
attr_accessor :browser_args, :unguarded, :implementation, :always_use_server

def html
@html ||= File.expand_path("../../spec/watirspec/html", __FILE__)
Expand All @@ -17,15 +17,7 @@ def run!
end

def url_for(str, opts = {})
if opts[:needs_server] || always_use_server
File.join(host, str)
else
File.join(files, str)
end
end

def files
@files ||= "file://#{html}"
File.join(host, str)
end

def host
Expand Down Expand Up @@ -116,9 +108,3 @@ def print_browser_info_once(instance)

end # class << WatirSpec
end # WatirSpec

require 'watirspec/guards'
require 'watirspec/implementation'
require 'watirspec/runner'
require 'watirspec/server'
require 'watirspec/silent_logger'
24 changes: 1 addition & 23 deletions lib/watirspec/implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,10 @@ def matches_guard?(args)

def matching_guards_in(guards)
result = []
guards.each { |args, data| data.each {|d| result << d } if matches_guard?(args) }
guards.each { |args, data| data.each { |d| result << d } if matches_guard?(args) }

result
end

end # Implementation
end # WatirSpec

if __FILE__ == $0
require "rubygems"
require 'rspec/autorun'

describe WatirSpec::Implementation do
before { @impl = WatirSpec::Implementation.new }

it "finds matching guards" do
guards = {
[:watir_classic] => [
{name: :not_compliant, data: {file: "./spec/watirspec/div_spec.rb:108"}},
{name: :deviates, data: {file: "./spec/watirspec/div_spec.rb:114"}},
{name: :not_compliant, data: {file: "./spec/watirspec/div_spec.rb:200"}},
{name: :bug, data: {file: "./spec/watirspec/div_spec.rb:228", key: "WTR-350"}}
]
}
@impl.name = :watir_classic
@impl.matching_guards_in(guards).should == [{name: :deviates, data: {file: "./spec/watirspec/div_spec.rb:114"}}]
end
end
end
38 changes: 6 additions & 32 deletions lib/watirspec/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ module WatirSpec
module Runner

module BrowserHelper
def browser; @browser; end
end

module PersistentBrowserHelper
def browser; $browser; end
def browser
$browser
end
end

module MessagesHelper
Expand All @@ -18,7 +16,6 @@ def messages
module_function

def execute
load_requires
start_server
configure
add_guard_hook
Expand All @@ -34,34 +31,11 @@ def configure
Thread.abort_on_exception = true

RSpec.configure do |config|
config.include(BrowserHelper)
config.include(MessagesHelper)

if WatirSpec.persistent_browser == false
config.include(BrowserHelper)

config.before(:all) { @browser = WatirSpec.new_browser }
config.after(:all) { @browser.close if @browser }
else
config.include(PersistentBrowserHelper)
$browser = WatirSpec.new_browser
at_exit { $browser.close }
end
end
end

def load_requires
require "rspec"
require "fileutils"

# implementation = File.expand_path("../../../implementation.rb", __FILE__)
# load implementation

begin
require "ruby-debug"
Debugger.start
Debugger.settings[:autoeval] = true
Debugger.settings[:autolist] = 1
rescue LoadError
$browser = WatirSpec.new_browser
at_exit { $browser.close }
end
end

Expand Down
15 changes: 5 additions & 10 deletions lib/watirspec/server.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require "socket"
require 'rack'
require 'watirspec/server/app'
require 'watirspec/server/silent_logger'

module WatirSpec
class Server
class << self
attr_accessor :autorun

def run_async
if WatirSpec.platform == :java
Thread.new { run! }
Expand Down Expand Up @@ -41,7 +40,7 @@ def run!
end

def app
files = html_files
files = static_files

Rack::Builder.app do
use Rack::ShowExceptions
Expand All @@ -51,8 +50,8 @@ def app
end
end

def html_files
Dir["#{WatirSpec.html}/*.html"].map do |file|
def static_files
Dir["#{WatirSpec.html}/*"].map do |file|
file.sub(WatirSpec.html, '')
end
end
Expand All @@ -78,12 +77,8 @@ def listening?
false
end

def autorun
@autorun ||= true
end

def should_run?
autorun && !running?
!running?
end

def running?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
module WatirSpec
class SilentLogger
(::Logger.instance_methods - Object.instance_methods).each do |logger_instance_method|
define_method(logger_instance_method) { |*args| }
define_method(logger_instance_method) { |*| }
end
end
end

24 changes: 24 additions & 0 deletions spec/implementation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'watirspec_helper'

describe WatirSpec::Implementation do
before { @impl = WatirSpec::Implementation.new }

it "finds matching guards" do
guards = {
[:firefox] => [
{name: :not_compliant, data: {file: "./spec/watirspec/div_spec.rb:108"}},
{name: :deviates, data: {file: "./spec/watirspec/div_spec.rb:114"}},
{name: :not_compliant, data: {file: "./spec/watirspec/div_spec.rb:200"}},
{name: :bug, data: {file: "./spec/watirspec/div_spec.rb:228", key: "WTR-350"}}
],
[:chrome] => [
{name: :not_compliant, data: {file: "./spec/watirspec/div_spec.rb:109"}},
{name: :deviates, data: {file: "./spec/watirspec/div_spec.rb:115"}},
{name: :not_compliant, data: {file: "./spec/watirspec/div_spec.rb:201"}},
{name: :bug, data: {file: "./spec/watirspec/div_spec.rb:229", key: "WTR-349"}}
]
}
@impl.name = :firefox
expect(@impl.matching_guards_in(guards)).to eq(guards.first[1])
end
end
2 changes: 0 additions & 2 deletions spec/watirspec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ def configure
start_remote_server if remote? && !ENV["REMOTE_SERVER_URL"]
set_browser_args
set_guard_proc

WatirSpec.always_use_server = ie? || safari? || phantomjs? || remote?
end

private
Expand Down

0 comments on commit ee22451

Please sign in to comment.