何时使用XPath以及为什么使用它?
以下两行代码均匹配值为“Post”的提交按钮。在使用XPath方面有哪些优点呢?
it { should have_selector("input[type=submit][value='Post']") }
it { should have_xpath("//input[@value='Post']") }
此外,在RSpec中是否存在必须使用XPath的情况?
何时使用XPath以及为什么使用它?
以下两行代码均匹配值为“Post”的提交按钮。在使用XPath方面有哪些优点呢?
it { should have_selector("input[type=submit][value='Post']") }
it { should have_xpath("//input[@value='Post']") }
如果存在歧义,您还可以添加
match: :prefer_exact
要“引导”选择,例如,如果您的页面上同时存在“密码”和“确认密码”字段,则可以添加类似以下内容的代码:
fill_in 'Password', with: @visitor[:password], match: :prefer_exact
根据我的个人经验,通过使用prefer_exact,您可以大多数情况下解决歧义,因此很少需要使用XPath...