JRuby: Watir is hanging when launching browser

2019-02-23 22:00发布

问题:

I have Windows 7 Enterprise and I am trying to run JRuby. I've set my environment variables for Ruby (1.9.3), Java (jdk - 1.6.7), and JRuby (1.7preview). I have a network drive but I created a HOME system variable which seems to be working because I can download gems for both Ruby & JRuby successfully and they are saved to my local drive.

When I try to launch a browser (ie, firefox, chrome) it seems to hang for JRuby, but not Ruby. Here is the trace for JRuby after it's been trying to execute for 10 minutes:

irb(main):003:0> require 'rubygems'
=> false
irb(main):004:0> require 'watir-webdriver'
=> true
irb(main):005:0> b = Watir::Browser.new :ie
IRB::Abort: abort then interrupt! *<--Note I killed the process after 10 minutes*
    from c:/Ruby/jruby-bin-1.7.0.preview1/jruby-1.7.0.preview1/lib/ruby/gems
    /shared/gems/selenium-webdriver-2.22.2/lib/selenium/webdriver/common 
    /port_prober.rb:24:in `free?'       
    from c:/Ruby/jruby-bin-1.7.0.preview1/jruby-1.7.0.preview1/lib/ruby/gems
    /shared/gems/selenium-webdriver-2.22.2/lib/selenium/webdriver/common  
    /port_prober.rb:5:in `above'        
    from c:/Ruby/jruby-bin-1.7.0.preview1/jruby-1.7.0.preview1/lib/ruby/gems
    /shared/gems/selenium-webdriver-2.22.2/lib/selenium/webdriver/ie/bridge.rb:17:in
   `initialize'
    from org/jruby/RubyHash.java:1429:in `delete'
    from c:/Ruby/jruby-bin-1.7.0.preview1/jruby-1.7.0.preview1/lib/ruby/gems
    /shared/gems/selenium-webdriver-2.22.2/lib/selenium/webdriver/ie/bridge.rb:17:in
    `initialize'
    from c:/Ruby/jruby-bin-1.7.0.preview1/jruby-1.7.0.preview1/lib/ruby/gems
    /shared/gems/selenium-webdriver-2.22.2/lib/selenium/webdriver/common/driver.rb:3
    5:in `for'
    from c:/Ruby/jruby-bin-1.7.0.preview1/jruby-1.7.0.preview1/lib/ruby/gems
    /shared/gems/selenium-webdriver-2.22.2/lib/selenium/webdriver.rb:65:in `for'
    from c:/Ruby/jruby-bin-1.7.0.preview1/jruby-1.7.0.preview1/lib/ruby/gems
    /shared/gems/watir-webdriver-0.6.1/lib/watir-webdriver/browser.rb:35:in   
    `initialize'
    from (irb):5:in `evaluate'
    from org/jruby/RubyKernel.java:1037:in `eval'
    from org/jruby/RubyKernel.java:1353:in `loop'
    from org/jruby/RubyKernel.java:1146:in `catch'
    from org/jruby/RubyKernel.java:1146:in `catch'
    from c:\Ruby\jruby-bin-1.7.0.preview1\jruby-1.7.0.preview1\bin\irb:13:in
    `(root)'

JRuby seems to hang because it's looking for an open port. We have to use an HTTP proxy for our network and I have setup a system variable for that as well. I really have no idea why Watir would work for Ruby but not JRuby.

回答1:

I had similar problem - selenium-webdriver was not opening browser when using JRuby and Java 6, however it worked with Java 7.

I found out that Java 6 seems to have some problems with IPv6, and it couldn't find free port on IPv6 interfaces.

Solution is to add -Djava.net.preferIPv4Stack=true to JAVA_OPTS telling Java to prefer IPv4 over IPv6 when possible.