Skip to content
This repository has been archived by the owner on Jun 6, 2018. It is now read-only.

Commit

Permalink
Add support for watir-classic 4.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmo committed Oct 5, 2013
1 parent f46c507 commit 4e2fa0a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
19 changes: 7 additions & 12 deletions watir/lib/watir/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

module Watir
class Browser
class << self

def new(browser=nil, *args)
load_driver_for browser

# remove this class method to avoid endless loop
singleton_class = class << self; self end
singleton_class.send :remove_method, :new
def initialize(browser=nil, *args)
self.class.load_driver_for browser

new browser.nil? && Watir.driver == :webdriver ? :firefox : browser, *args
end
# execute just loaded driver's #initialize
initialize browser.nil? && Watir.driver == :webdriver ? :firefox : browser, *args
end

class << self
def start(url, browser=nil, *args)
load_driver_for browser

Expand All @@ -26,11 +22,10 @@ def start(url, browser=nil, *args)

def method_missing(name, *args, &block)
Watir.load_driver
return super unless respond_to? name
send name, *args, &block
end

private

def load_driver_for(browser)
if browser && browser.respond_to?(:to_sym) && browser.to_sym != :ie && Watir.driver == :classic
Watir.driver = :webdriver
Expand Down
2 changes: 1 addition & 1 deletion watir/watir.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ Gem::Specification.new do |s|

if ENV["build_for_windows"]
s.platform = Gem::Platform::MINGW
s.add_dependency "watir-classic", "~> 3.2"
s.add_dependency "watir-classic", "~> 4.0"
end
end

13 comments on commit 4e2fa0a

@mhodgson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ummm, I think Browser.new just always infinite loops now.

@jarmo
Copy link
Member Author

@jarmo jarmo commented on 4e2fa0a Oct 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For which driver are you seeing this behavior? Or did you just think that it does it by looking at the code?

@mhodgson
Copy link

@mhodgson mhodgson commented on 4e2fa0a Oct 8, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jarmo
Copy link
Member Author

@jarmo jarmo commented on 4e2fa0a Oct 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show the code? I'm suspecting that you're requiring watir-webdriver manually.

@mhodgson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, here's the factory we use to make browsers sessions:

require 'watir-webdriver'
require 'webdriver-user-agent'
require './config/application'
include Selenium

class WatirBrowserFactory

  def self.create(browser_type=nil)
    client = Selenium::WebDriver::Remote::Http::Default.new
    client.timeout = 120

    browser_type = ENV['CAPYBARA'] || :firefox
    puts browser_type
    case browser_type.to_sym
      when :chrome
        return Watir::Browser.new :chrome, http_client: client
      when :safari
        return Watir::Browser.new :safari, http_client: client
      when :ie9
        Watir::Rails.host = local_ip
        driver = Selenium::WebDriver.for :remote, :url => 'http://win7-ie9:4444/wd/hub', :desired_capabilities => :ie, http_client: client
        driver.manage.timeouts.implicit_wait = 10
        driver.manage.window.maximize
        return Watir::Browser.new driver
      when :ie10
        Watir::Rails.host = local_ip
        driver = Selenium::WebDriver.for :remote, :url => 'http://win7-ie10:4444/wd/hub', :desired_capabilities => :ie, http_client: client
        driver.manage.timeouts.implicit_wait = 10
        driver.manage.window.maximize
        return Watir::Browser.new driver
      when :mobile_firefox
        driver = Webdriver::UserAgent.driver(:browser => :firefox, :agent => :iphone, :orientation => :portrait)
        return Watir::Browser.new driver
      when :mobile_simulator
        driver = Selenium::WebDriver.for :remote, :url => "http://localhost:5555/wd/hub", :desired_capabilities => :iphone
        driver.manage.timeouts.implicit_wait = 30
        return Watir::Browser.new driver
      else
        profile = Selenium::WebDriver::Firefox::Profile.new
        profile.native_events = false
        browser = Watir::Browser.new :firefox, http_client: client, profile: profile
        browser.driver.manage.window.maximize
        #width = browser.execute_script("return screen.width;")
        #height = browser.execute_script("return screen.height;")
        #browser.driver.manage.window.move_to(0,0)
        #browser.driver.manage.window.resize_to(width,height)
        return browser
    end
  end

  def self.local_ip
    orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true  # turn off reverse DNS resolution temporarily

    UDPSocket.open do |s|
      s.connect '64.233.187.99', 1
      s.addr.last
    end
  ensure
    Socket.do_not_reverse_lookup = orig
  end
end

@jarmo
Copy link
Member Author

@jarmo jarmo commented on 4e2fa0a Oct 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And where are you using watir gem? Do you have it in your Gemfile or Gemfile.lock file?

@mhodgson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's in the Gemfile (and therefor also in the lock file).

@jarmo
Copy link
Member Author

@jarmo jarmo commented on 4e2fa0a Oct 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that you're manually loading watir-webdriver thus watir Browser#initialize will not be overridden. You could avoid that by loading the driver manually.

require "watir"
Watir.driver = :webdriver # since you're already tightly coupled with it then there's no need to use :classic ever
Watir.load_driver

# and no need to load watir-webdriver anymore

Also make sure that in the Gemfile is only watir and not watir-webdriver.

I agree that there should be some more cleverness built into watir gem to avoid this kind of problems.

Does this work for you?

@jarmo
Copy link
Member Author

@jarmo jarmo commented on 4e2fa0a Oct 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create an issue here https://github.com/watir/watir/issues

@mhodgson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jarmo that worked thanks. I think as long as you update the README to indicate that 'watir-webdriver' should never be explicitly required it would probably be fine.

@jbielick
Copy link

@jbielick jbielick commented on 4e2fa0a May 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite a hacky override chain.

there should be some more cleverness

There ought to be less cleverness.

watir/watir#24

@jarmo
Copy link
Member Author

@jarmo jarmo commented on 4e2fa0a May 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbielick totally agree with you. However, at the time I wanted to create a solution which would not have any backward-incompatible changes when using gem watir (now watir-classic).

Thinking of it now maybe backward-incompatible solution would have been better since it would not have been hacky :)

In long term plans this problem will resolve itself since this gem is planned to be deprecated and replaced with watir-webdriver gem instead.

@jbielick
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jarmo makes sense. In this case it's somewhat isolated to watir-rails usage I suppose so just FYI!

Please sign in to comment.