我正在使用 SitePrism 来测试我的 Web 应用程序。我有许多扩展的类,SitePrism::Page
并且许多常用的 HTML 片段由扩展的匹配类表示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,其中可以通过选择基于预定义内容的模板,然后将任意数量的可用组件拖放到页面上来组装页面。
最初的开发人员创建了一个Page Object来镜像每个可用的Template。只要测试的数量很少,并且我们必须在功能文件中测试的页面变体不多,这就很好。
随着多个测试用例的添加,页面对象开始以惊人的速度增长。
虽然我们可以通过为 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 选择器泄漏到步骤定义中,这通常是一种不好的做法。
解决此问题的另一种方法是为特定的页面示例构建页面对象,而不是通用模板。模板页面对象可以只包含嵌入到模板中的任何内容,并由镜像特定页面的其他页面对象扩展,以处理差异。这听起来像一个更清洁的方法,所以我可能会这样写我的测试
无论如何,问题的技术部分仍然存在。不管它是好是坏,我怎样才能动态地扩展一个带有附加部分的页面对象?我只是好奇。