Problem
I assume that you are using watir-classic v3.7.0 or older.
In these versions, doing require 'watir-classic'
does not load all of the classes immediately. Some of the classes, including the Watir::Element, are not loaded until a browser instance is created.
This means that:
# Does not create Watir::Element#generate_ruby_code yet
require 'watir-classic'
# You create a Watir::Element#generate_ruby_code method
module Watir
class Element
def generate_ruby_code(element, method_name, *args)
puts "Print this"
end
end
end
# Watir loads the Watir::Element#generate_ruby_code, which overrides yours
browser = Watir::Browser.new
My understanding is that this is due tp watir-classic previously supporting multiple browsers - ie FireWatir and SafariWatir. Various classes were autoloaded based on the browser being used.
Solution 1 - Upgrade to v4.0 or later
The easiest solution is to upgrade your watir-classic to v4.0 or later (current latest is 4.0.1).
The autoloading was removed in this version, which means your code will now work as is.
Solution 2 - Initialize browser first
If upgrading is not an option, you need to manually force the autoload before monkey patching. You can do this by simply referencing the constant with:
Watir::IE
Simply include this at some point after requiring watir-classic and before monkey patching.
require 'watir-classic'
Watir::IE # this will load the code
module Watir
class Element
def generate_ruby_code(element, method_name, *args)
puts "Print this"
end
end
end
browser = Watir::Browser.new
browser.goto 'www.google.ca'
browser.link.click_no_wait
#=> "Print this"