spec/implementation.rb in watir-webdriver-0.5.3 vs spec/implementation.rb in watir-webdriver-0.5.4

- old
+ new

@@ -9,39 +9,51 @@ set_webdriver set_browser_args set_guard_proc add_html_routes - WatirSpec.always_use_server = mobile? || ie? + WatirSpec.always_use_server = mobile? || ie? || safari? end private def set_webdriver @imp.name = :webdriver @imp.browser_class = Watir::Browser end def set_browser_args - case browser - when :firefox - set_firefox_args - when :chrome - set_chrome_args - else - @imp.browser_args = [browser] + args = case browser + when :firefox + firefox_args + when :chrome + chrome_args + else + [browser, {}] + end + + if ENV['SELECTOR_STATS'] + listener = SelectorListener.new + args.last.merge!(:listener => listener) + at_exit { listener.report } end + + @imp.browser_args = args end def mobile? [:android, :iphone].include? browser end def ie? [:ie, :internet_explorer].include? browser end + def safari? + browser == :safari + end + def set_guard_proc matching_guards = [ :webdriver, # guard only applies to webdriver browser, # guard only applies to this browser [:webdriver, browser] # guard only applies to this browser on webdriver @@ -58,18 +70,18 @@ @imp.guard_proc = lambda { |args| args.any? { |arg| matching_guards.include?(arg) } } end - def set_firefox_args + def firefox_args profile = Selenium::WebDriver::Firefox::Profile.new profile.native_events = native_events? - @imp.browser_args = [:firefox, {:profile => profile}] + [:firefox, {:profile => profile}] end - def set_chrome_args + def chrome_args opts = { :switches => ["--disable-translate"], :native_events => native_events? } @@ -83,11 +95,11 @@ if path = ENV['WATIR_WEBDRIVER_CHROME_BINARY'] Selenium::WebDriver::Chrome.path = path end - @imp.browser_args = [:chrome, opts] + [:chrome, opts] end def add_html_routes glob = File.expand_path("../html/*.html", __FILE__) Dir[glob].each do |path| @@ -103,9 +115,28 @@ ENV['NATIVE_EVENTS'] == "true" end def native_events_by_default? Selenium::WebDriver::Platform.windows? && [:firefox, :ie].include?(browser) + end + + class SelectorListener < Selenium::WebDriver::Support::AbstractEventListener + def initialize + @counts = Hash.new(0) + end + + def before_find(how, what, driver) + @counts[how] += 1 + end + + def report + total = @counts.values.inject(0) { |mem, var| mem + var } + puts "\nWebDriver selector stats: " + @counts.each do |how, count| + puts "\t#{how.to_s.ljust(20)}: #{count * 100 / total} (#{count})" + end + end + end end ImplementationConfig.new(WatirSpec.implementation).configure