Cannot create firefox browser with watir-webdriver

2019-09-06 04:04发布

问题:

I have firefox 20 which works with my version of watir-webdriver. I use the following code which can open any browser or a headless phantomjs browser. Chrome and phantom work, but firefox throws an exception.

Why? How do I fix this ?

require 'rubygems'
require 'watir-webdriver'
require 'phantomjs'

def get_browser(browser_name)
  browser = nil
  if browser_name != nil    
    puts "\n#{browser_name} created\n"  
    browser = Watir::Browser.new browser_name.to_sym
  else
    puts "creating phantom browser !"
    browser = Watir::Browser.new :phantomjs
    puts "\nphantom created\n"
  end
  return browser
end

browser_name = ARGV[0]
browser = get_browser(browser_name)
puts "\n\n"

browser.close

Exception-

C:/code/ruby/lib/ruby/gems/1.8/gems/json_pure-1.4.3/lib/json/pure/generator.rb:393:in `to_json': undefined method `ascii_only?' for {}:Hash (NoMethodError)
        from C:/code/ruby/lib/ruby/gems/1.8/gems/multi_json-1.3.7/lib/multi_json/adapters/json_common.rb:11:in `dump'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/multi_json-1.3.7/lib/multi_json.rb:115:in `dump'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/json_helper.rb:23:in `json_dump'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:254:in `write_prefs'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:253:in `each'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:253:in `write_prefs'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:252:in `open'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:252:in `write_prefs'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:232:in `update_user_prefs_in'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/profile.rb:79:in `layout_on_disk'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/launcher.rb:58:in `create_profile'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/launcher.rb:34:in `launch'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/socket_lock.rb:20:in `locked'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/launcher.rb:32:in `launch'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/firefox/bridge.rb:24:in `initialize'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/driver.rb:31:in `new'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/driver.rb:31:in `for'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver.rb:67:in `for'
        from C:/code/ruby/lib/ruby/gems/1.8/gems/watir-webdriver-0.6.2/lib/watir-webdriver/browser.rb:46:in `initialize'
        from watir-browser.rb:9:in `new'
        from watir-browser.rb:9:in `get_browser'
        from watir-browser.rb:19

回答1:

you code is working for me without issue.

$ ruby test.rb
creating phantom browser !
phantom created

$ ruby test.rb firefox
firefox created

$ ruby test.rb chrome
chrome created

$ gem list | grep webdriver
selenium-webdriver (2.45.0, 2.44.0)
watir-webdriver (0.7.0, 0.6.11)

Firefox comes with selenium-webdriver, you probably just need to update your gem(s)



回答2:

Ruby 1.8, Firefox 20, Watir 0.6.2 all look about 2ish years out of date. json_pure-1.4.3 however is 5 years old - https://rubygems.org/gems/json_pure/versions

Try updating that one.