(Cucumber Rails)Capybara:内部(选择器)应该在通话后持续存在

时间:2013-10-04 19:09:41

标签: ruby-on-rails session cucumber capybara

我有一个页面,我想要几个其他项目中的几个项目。所以基本上我的功能看起来像这样:

Scenario: Footer has caption Impressum
  Given I am on the index page.
  When I look at the footer
  Then I should see a caption "Impressum"

我希望工作的是:

When /I look at the footer/ do
  scope("footer") # << css selector <footer\b.*</footer>
end

Then /I should see a caption "(.*?)"/ do |caption|
  within "h3" do
    page.should have_content(caption)
  end
end

我怎样才能尽可能坚持实施?

要详细了解,应该传递的页面是:

<html>
<head></head>
<body>
<footer>
<h3>Impressum</h3>
<p>Address: Baker Street 21</p>
</footer>
</body>
</html>

虽然传递的网页是:

<html>
<head></head>
<body>
<h3>Impressum</h3>
<footer><p>Address: Baker Street 21</p></footer>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

这就是我要做的事情:

When /I look at the footer/ do
  # save a scope that should be used in a 'within' block
  @scope = "footer" # << css selector <footer\b.*</footer>
end

Then /I should see a caption "(.*?)"/ do |caption|
  # use the scope set above, or don't if it's not set
  within "#{@scope || ''} h3" do
    page.should have_content(caption)
  end
end

答案 1 :(得分:0)

我的解决方案是使用助手

# features/support/scope_helper.rb
module ScopeHelper
  def scope sel = nil
    @scope = sel if sel
    within @scope, &Proc.new if block_given?
  end
end
World(ScopeHelper)

然后,我就这样使用它:

When /^I look at the footer$/ do
  scope 'footer'
end

Then /^I should see a caption "(.*)"$/ do |caption|
  scope{ find('h3').should have_content(caption) }
end

答案 2 :(得分:0)

我对这种依赖性也有类似的挑战。在我的情况下,我有一个表格,并且想单击特定行的删除链接。我找不到合适的解决方案,所以我决定用一句话来解决:

When I search for the row containing «Beverly C. Crusher» and click on «Delete Task»