Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

system testing - Capybara ingore `within` scope at specify assertion

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...