【发布时间】:2015-11-19 08:57:49
【问题描述】:
我正在使用 SitePrism 测试我的 Web 应用程序。我有许多扩展 SitePrism::Page 的类,并且许多常用的 HTML sn-ps 由扩展 SitePrism::Section 的匹配类表示
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
问题是,我正在开发的应用程序是基于 CMS,其中可以通过基于预定义内容选择 模板 来组装页面,然后拖放 -将任意数量的可用组件拖放到页面上。
最初的开发者创建了一个页面对象来镜像每个可用的模板。这很好,只要测试数量少,并且我们必须在功能文件中测试的页面变体不多。
随着多个测试用例的加入,页面对象开始以惊人的速度增长。
虽然我们可以通过为 CMS 中的每个可用组件定义 Sections 并在 Page Objects 中重用它们来轻松减少代码重复,但只有很多属性很少得到用过。
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 选择器泄漏到步骤定义中,这通常是一种不好的做法。
解决此问题的另一种方法是为特定的页面示例构建页面对象,而不是通用模板。 模板页面对象 可以只包含嵌入到模板中的任何内容,并由镜像特定页面的其他页面对象 扩展,以处理差异。这听起来像是一种更简洁的方法,所以我可能会这样写我的测试
无论如何,问题的技术部分仍然存在。不管它是好是坏,我怎样才能动态地扩展一个带有附加部分的页面对象?我只是好奇。
【问题讨论】:
标签: ruby cucumber capybara automated-tests site-prism