只是为了给你一个背景,我正在使用 Ruby 以及 Selenium、Cucumber、Capybara 和 SitePrism 来创建自动化测试。我有一些测试需要检查页面上某个元素的文本,例如:
def get_section_id
return section.top.course.section_id.text
end
但是,我想在调用.text
嵌套course_and_section_id
元素之前检查所有父元素是否存在。例如,要检查这个特定元素的文本,我会这样做:
if(has_section? && section.has_top? && section.top.has_course? && section.top.course.has_section_id?)
return section.top.course.section_id.text
end
有没有办法递归检查Ruby中是否存在这样的东西?可以这样称呼的东西:has_text?(section.top.course.section_id)
也许?