Composition or inheritance for delegating page methods?
引用链接: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.
Composition or inheritance for delegating page methods?的更多相关文章
- 组合优于继承 Composition over inheritance
https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance 解答1 Prefer composition ...
- Effective Java 16 Favor composition over inheritance
Inheritance disadvantage Unlike method invocation, inheritance violates encapsulation. Since you don ...
- React之Composition Vs inheritance 组合Vs继承
React的组合 composition: props有个特殊属性,children,组件可以通过props.children拿到所有包含在内的子元素, 当组件内有子元素时,组件属性上的child ...
- Inheritance setUp() and tearDown() methods from Classsetup() and Classteardown
I have a general test class in my nosetests suit and some sub-classes, inheriting from it. The con ...
- selenium page object model
Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide 来源:http://www.guru99.com/page ...
- <Effective C++>读书摘要--Inheritance and Object-Oriented Design<二>
<Item 36> Never redefine an inherited non-virtual function 1.如下代码通过不同指针调用同一个对象的同一个函数会产生不同的行为Th ...
- inheritance in kentico
Visual inheritance http://devnet.kentico.com/docs/7_0/devguide/index.html?visual_inheritance.htm The ...
- Thinking in Java——笔记(17)
Containers in Depth Full container taxonomy You can usually ignore any class that begins with " ...
- 静态工厂方法VS构造器
我之前已经介绍过关于构建者模式(Builder Pattern)的一些内容,它是一种很有用的模式用于实例化包含几个属性(可选的)的类,带来的好处是更容易读.写及维护客户端代码.今天,我将继续介绍对象创 ...
随机推荐
- docker-machine create --driver virtualbox myvm1 创建失败
1. 问题描述 docker-machine create --driver virtualbox myvm1 安装完 virtualbox 后,无法创建. 输出内容为: Running pre-cr ...
- cocos2dx之lua绑定简析
一.总原则:c++对象的生命期不依赖lua gc管理,手动创建的对象要手动销毁 二.引擎层在设计上就是支持脚本概念的(也就是说脚本的使用是“侵入式”的),与lua打交道的代码都封在CCLuaEngin ...
- 8、scala面向对象编程之Trait
一.Trait基础 1.将trait作为接口使用 // Scala中的Triat是一种特殊的概念 // 首先我们可以将Trait作为接口来使用,此时的Triat就与Java中的接口非常类似 // 在t ...
- java File基本操作,以及递归遍历文件夹
java 的文件操作,相对来说是比较重要的,无论是编写CS还是BS程序,都避免不了要与文件打交道,例如读写配置文件等.虽然现在很多框架都直接帮你做好了这一步! java.io.File 底层是调用与c ...
- .Net Core中依赖注入服务使用总结
一.依赖注入 引入依赖注入的目的是为了解耦和.说白了就是面向接口编程,通过调用接口的方法,而不直接实例化对象去调用.这样做的好处就是如果添加了另一个种实现类,不需要修改之前代码,只需要修改注入的地方将 ...
- webpack@3.6.0(2) -- css及图片相关问题
本篇内容 css3前缀处理postcss 消除未使用的css部分 图片处理 css分离和分离后的图片处理 css3前缀处理postcss cnpm i -D postcss-loader autopr ...
- webpack -- 多页面简单小例
有时单页面并不能满足我们的业务需求,就需要去构建多页面应用,以下为简单小例: entry:{ index:'./src/module/index/index.js', student:'./src/m ...
- Codeforces Round #524 (Div. 2) D(思维,构造)
#include<bits/stdc++.h>using namespace std;long long dp[107];int main(){ int cnt=1; dp[1 ...
- python之05 操作系统用户密码修改
ubuntu的操作系统修改密码的操作方法: 一.在系统启动时按住shift键,出现下图的界面 二.按下e进入命令行,找到下图红色框中的文字并修改成rw init=/bin/bash 然后按F10启动, ...
- python字符串和日期相互转换