引用链接:http://watirmelon.com/2011/01/24/composition-or-inheritance-for-delegating-page-methods/
Composition or inheritance for delegating page methods?
The thing I like to do when creating a page object pattern for automated web testing is delegating any methods that don’t belong to the Page object itself.
For example, a very simple page object model like this GoogleHomePage
doesn’t delegate any methods to the Browser object.
require "rubygems"
require "watir-webdriver"
class GoogleHomePage
def initialize(browser)
@browser = browser
end
def visit
@browser.goto "www.google.com"
end
end
b = Watir::Browser.new :firefox
p = GoogleHomePage.new b
p.visit
puts p.title
p.close
So, the p.title
and p.close
statements both fail with an error: undefined method `goto' for # (NoMethodError)
.
One approach would be simply to write appropriate methods for what you would do on the Browser object. For example:
require "rubygems"
require "watir-webdriver"
class GoogleHomePage
def initialize(browser)
@browser = browser
end
def visit
@browser.goto "www.google.com"
end
def title
@browser.title
end
def close
@browser.close
end
end
b = Watir::Browser.new :firefox
p = GoogleHomePage.new b
p.visit
puts p.title
p.close
But this isn’t DRY. It means every method of Browser you access to needs to be rewritten. But I often see this happen.
What we should be doing is simply delegating any methods that don’t exist on the Page object to the Browser object which is passed in at initialization. There are two ways I know of to do this: inheritance delegation and composition.
Inheritance Delegation
Inheritance delegation means changing our class so it delegates appropriately using a DelegateClass. This means anything of class Browser is delegated.
For example:
require "rubygems"
require "watir-webdriver"
class GoogleHomePage < DelegateClass(Watir::Browser)
def initialize(browser)
super(browser)
end
def visit
self.goto "www.google.com"
end
end
b = Watir::Browser.new :firefox
p = GoogleHomePage.new b
p.visit
puts p.title
p.close
From this point forward you don’t need to refer to @browser, instead you just refer to self in your class.
Composition
Composition is about composing the class of different elements, some of which are passed to the browser. This essentially involves creating a method_missing
method and passing these methods to the instance variable @browser.
require "rubygems"
require "watir-webdriver"
class GoogleHomePage
def initialize(browser)
@browser = browser
end
def method_missing(sym, *args, &block)
@browser.send sym, *args, &block
end
def visit
@browser.goto "www.google.com"
end
end
b = Watir::Browser.new :firefox
p = GoogleHomePage.new b
p.visit
puts p.title
p.close
This means that any reference to the Browser object still needs to refer to @browser throughout the class.
Inheritance or Composition?
You can see from the examples above, both approaches are very similar, but from researching these, it seems that most people prefer composition to inheritance in ruby, mainly due to maintainability of class chains. In our example, the inheritance chain is small and very simple, so I don’t think this poses a great maintainability issue.