問題描述
編寫應用于站點中每個頁面的斷言的最佳方法是什么?
What's the best way to write assertions that should apply across every page in a site?
我正在測試我的網站頁腳中是否存在一個元素,因此該元素應該存在于所有頁面上.
I'm testing to see if an element exists in the footer of my site, so the element should exist on all pages.
我正在考慮編寫一個單獨的文件來測試網站的模板元素,然后將其包含在所有規(guī)范中.不過好像沒有其他人這樣做?
I am thinking of writing a separate file for testing template elements of the site, and then include this in all specs. It doesn't seem like anyone else is doing this though?
推薦答案
首先,為了編寫更清晰的測試并更好地了解目標站點的組成,請應用 頁面對象模式 并將您的網頁部分拆分為不同的頁面對象.例如,footer
、header
可以而且應該是獨立的頁面對象,可以在您網站的不同網頁中重復使用.
First of all, for writing cleaner tests and having a better understanding of what your target site consists of, apply the Page Object pattern and split the parts of your web pages into different page objects. For instance, footer
, header
can and should be separate page objects that would be reused across different web pages of your site.
更多主題:
- 使用頁面對象組織測試
- 量角器頁面對象(教程,非常詳細)
- PageObject (Martin Fowler)
- 使用頁面對象克服 Protractor 的缺點一個>
據我了解的問題,要遵循DRY"原則,您希望擁有某種共享"茉莉花規(guī)格,您可以定義一次并在多個測試套件中運行.
As far as I understand the question, to follow the "DRY" principle you want to have some sort of the "shared" jasmine specs that you can define once and run in multiple test suites.
這正是 用共享行為干燥 Jasmine Specs 文章正在描述.這個想法相當簡單——在你的測試套件中定義一個函數,并從其他測試套件中調用它.示例:
This is exactly what DRYing up Jasmine Specs with Shared Behavior article is describing. The idea is rather simple - define a function with your test suites inside and call it from other test suites. Example:
創(chuàng)建一個函數,該函數接受上下文 - 頁面對象 - 并包含特定于頁腳的可重用測試:
create a function which accepts a context - page object - and contains the footer specific reusable tests:
function testFooter(footer) {
describe("(shared)", function () {
describe("should show footer with necessary information", function () {
it("should show copyright", function () {
expect(footer.copyright.getText()).toEqual('Copyright 2014');
});
});
});
}
從傳遞上下文的其他測試套件調用該函數 - 頁腳頁面對象:
call the function from other test suites passing the context - footer page object:
var FooterPage = require('./footer.po.js');
describe('Contacts page', function () {
var scope = {};
beforeEach(function () {
browser.get('/contacts/');
browser.waitForAngular();
scope.page = new ContactsPage();
});
// other contacts page specific suites
// ...
testFooter(new FooterPage());
});
您可能需要調整和改進代碼以使其正常工作,但理念保持不變:定義一次并重用.傳遞頁面對象只會使它更干凈和透明.
You may need to adjust and improve the code to make it work, but the idea stays the same: define once and reuse. Passing around page objects just makes it a lot cleaner and transparent.
另見:
- 什么是重用測試代碼的好方法茉莉花?
- 量角器頁面對象繼承
這篇關于在量角器中測試模板?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!