Capybara ingore `within` scope at specify assertion

137 Views Asked by At

Suppose I'm testing a form with Capybara and Minitest, the form has a text input, which using bootstrap-datepicker, I wanna unscope(like ActiveRecord) the within('form') scope only at assert_selector '.datepicker-dropdown', count: 1, as .datepicker-dropdown is appended to body not the form

within "form" do
  # other tests...
  find("input.date-picker").click
  assert_selector '.datepicker-dropdown', count: 1 
  # failed because .datepicker-dropdown is appended to body
end

Though bootstrap-datepicker have an option container to specify where to append the datepicker-dropdown widget, but not suitable for this case.

1

There are 1 best solutions below

0
On BEST ANSWER

There are two ways of escaping the scope of within. The first would be to use XPath and taking advantage of the Xpath-trap (https://github.com/teamcapybara/capybara#beware-the-xpath--trap) by intentionally breaking the scope

assert_xpath '//*', class: 'datepicker-dropdown', count: 1

The second (and probably clearer) method would be to use the page.document method to escape the current scope

assert_selector page.document, '.datepicker-dropdown', count: 1