Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

question from:https://stackoverflow.com/questions/65838745/capybara-ingore-within-scope-at-specify-assertion

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
968 views
Welcome To Ask or Share your Answers For Others

1 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...