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)的一些内容,它是一种很有用的模式用于实例化包含几个属性(可选的)的类,带来的好处是更容易读.写及维护客户端代码.今天,我将继续介绍对象创 ...
随机推荐
- eclipse tomcat 无法加载导入的web项目,There are no resources that can be added or removed from the server. .
应该是项目自己的setting文件夹下的描述信息和.project文件的描述信息,不能适用于这个eclipse和tomcat. 解决方法: 1,找相同类型的工程(tomcat能引用的)2,把新建项目里 ...
- qt数据库sql语句使用c++中的变量
void SerialWidget::on_btnMysql_clicked() { qDebug()<<QSqlDatabase::drivers()<<endl; /*列出 ...
- css 雪碧图
CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问 该页面时,载入的图片就不会像以前那样一幅一幅地 ...
- linux-java环境安装以及ssh
1 下载Java8 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2 使用 ...
- [matlab]一道笔试题
x=[1 1; 1 -1; -1 -1; -1 1]'; X=-2:0.01:2; Y=X; N=length(X); [X,Y]=meshgrid(X,Y); Z1=0;Z2=0;Z3=0;Z4=0 ...
- p2371&bzoj2118 墨墨的等式
传送门(bzoj) 题目 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存 ...
- Entity Framework Code-First(9.11):DataAnnotations - InverseProperty Attribute
DataAnnotations - InverseProperty Attribute: We have seen in the Code-First Convention section that ...
- 15.Nginx 解析漏洞复现
Nginx 解析漏洞复现 Nginx解析漏洞复现. 版本信息: Nginx 1.x 最新版 PHP 7.x最新版 由此可知,该漏洞与Nginx.php版本无关,属于用户配置不当造成的解析漏洞. 使用d ...
- Equals 和 == 的区别--转
在比较Equals 和 ==的区别前.我们先来了解下相关的知识 C#数据类型 1.值类型 值类型有: 值类型包括:简单类型.结构类型.枚举类型. byte(1).sbyte(1).short(2).u ...
- oracle 导入sql文件乱码
查看 http://blog.csdn.net/fireofjava/article/details/53980966