我使用SitePrism來測試我的Web應用程序。我有許多延伸SitePrism::Page
類和延伸SitePrism::Section
如何動態添加節到SitePrism頁面對象?
class Login < SitePrism::Section
element :username, "#username"
element :password, "#password"
element :sign_in, "button"
end
class Home < SitePrism::Page
section :login, Login, "div.login"
end
一些經常使用的HTML片段通過匹配類表示的問題是,我正在應用程序是基於CMS,在通過根據預定義的內容選擇模板,然後將任意數量的可用組件拖放到頁面上來組裝頁面。
最初的開發者創建了一個頁面對象來鏡像每個可用的模板。只要測試次數少,沒有太多的頁面變體,我們不得不在我們的功能文件中測試,這樣就沒有問題。
隨着多個測試用例的添加,頁面對象開始以驚人的速度增長。
雖然我們可以很容易地通過定義第在CMS提供的每個組件和重用他們跨越頁面對象減輕代碼重複,但只是很多的屬性很少習慣。
class BlogPost < SitePrism::Page
section :logo, MySite::Components::Logo, '.logo'
section :navigation, MySite::Components::Navigation, '.primary-navigation'
section :header, MySite::Components::BlogHeader, '.header'
section :introduction, MySite::Components::Text, '.text .intro'
# and so on, a lot of dynamic staff that could potentially be dropped onto the page
# but does not neccessarily be there, going in dozens of lines
end
是否有SitePrism辦法一節動態地添加到頁面對象的實例,而不是整個班級?
Then(/^Some step$/) do
@blog = PageObjects::BlogPost.new()
@blog.load("some url")
@blog.somehow_add_a_section_here_dynamically
expect (@blog.some_added_section).to be_visible
end
這也讓我擔心的是做這樣的事情,這將可能導致CSS選擇器泄漏到步驟定義,通常是一個不好的做法。
解決此問題的另一種方法是針對與多功能模板相反的頁面的特定示例構建頁面對象。該模板頁面對象可以只含有任何真實烤成的模板和其他頁擴展對象是反映特定頁面,採取的不同治療。這聽起來像一個更簡潔的方法,所以我可能會這樣
寫我的測試反正,這個問題的技術部分代表。不管這個想法有多好或多壞,我怎樣才能動態地擴展一個頁面對象的額外部分?我只是好奇。
正如問題本身所述。我更傾向於遵循每個page_模式的_page對象,而不是每個template_的_page對象。我只是將模板作爲基類或mixin離開。我只是好奇如何在Ruby中進行元編程。 – toniedzwiedz
事實證明,重新訪問這個問題很有意思,所以我很高興你問了這個問題:-)我可能會在我的叉子上包含類似這樣的東西。 – tgf