问题描述
我们使用 页面对象模式 已经有一段时间了.它绝对有助于组织端到端测试并使测试更具可读性和简洁性.
We've been using the Page Object pattern for quite a while. It definitely helps to organize the end-to-end tests and makes tests more readable and clean.
正如 使用页面对象组织测试 Protractor 文档页面向我们展示的那样,我们将每个页面对象定义为一个函数并使用 new
来实例化"它:
As Using Page Objects to Organize Tests Protractor documentation page shows us, we are defining every page object as a function and use new
to "instantiate" it:
"use strict";
var HeaderPage = function () {
this.logo = element(by.css("div.navbar-header img"));
}
module.exports = HeaderPage;
用法:
"use strict";
var HeaderPage = require("./../po/header.po.js");
describe("Header Look and Feel", function () {
var header;
beforeEach(function () {
browser.get("/#login");
header = new HeaderPage();
});
it("should show logo", function () {
expect(header.logo.isDisplayed()).toBe(true);
});
});
但是,最近在 Protractor:Angular 测试变得容易谷歌测试博客文章,我注意到一个页面对象被定义为一个对象:
But, recently in the Protractor: Angular testing made easy Google Testing Blog post, I've noticed that a page object is defined as an object:
var angularHomepage = {
nameInput : element(by.model('yourName')),
greeting : element(by.binding('yourName')),
get : function() {
browser.get('index.html');
},
setName : function(name) {
this.nameInput.sendKeys(name);
}
};
这两种引入Page Objects的方式有什么区别?我应该更喜欢一个吗?
What is the difference between these two ways to introduce Page Objects? Should I prefer one against the other?
推荐答案
归根结底,我觉得还是个人喜好问题.
Ultimately, I think it is a question of personal preference.
是的,您可以使用构造函数模式并在每个测试套件中实例化一个单例...是的,您可以使用上面的简单对象文字...是的,您可以使用工厂函数...
Yes, you can use the constructor pattern and instantiate a singleton in each test suite... yes you could use a simple object literal as above... yes you could use a factory function...
通过类"(无论是伪语法还是 ES2015 语法)使用继承来构建代码与通过 mixins 扩展的对象在一般应用程序开发中是一个更广泛的争论,更不用说 e2e 测试了!
Structuring code using inheritance via "classes" (whether pseudo- or ES2015 syntax) vs objects extended via mixins is a much wider debate within application development in general, never mind e2e tests!
主要是在您的测试套件中进行清晰、一致的实践,并尽可能提高代码的可重用性.
The main thing is clear, consistent practice across your test suites and promoting code reusability wherever possible.
这篇关于在量角器中定义页面对象的规范方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!