問題描述
我們使用 頁面對象模式 已經有一段時間了.它絕對有助于組織端到端測試并使測試更具可讀性和簡潔性.
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.
這篇關于在量角器中定義頁面對象的規范方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!