如何使用 capybara + minitest 断言两个选择器之一

How to assert one of two selectors with capybara + minitest

我目前有一个 def 可以在下面执行此操作,但我觉得这样做效率低下,可能还有另一种方法可以做到这一点。我知道 Rspec 有 or() 函数来帮助它,但水豚似乎没有这个。

def assert_either_selector(selector_1, selector_2)
  if has_selector?(selector_1, wait: false)
    assert_selector(selector_1)
  elsif has_selector?(selector_2, wait: false)
    assert_selector(selector_2)
  else
    flunk("Failed to match either selector \nExpected to find either: \n\t#{selector_1} or #{selector_2}")
  end
end

Capybara 确实支持 or,但仅在使用 RSpec 匹配器 (expect(page).to have_selector(selector_1).or(have_selector(selector_2))) 时才支持,因为 minitest 没有同步评估 or 支持。假设 selector_1selector_2 是 CSS 选择器,那么这里最简单的解决方案是使用 CSS 逗号并执行

assert_selector("#{selector_1}, #{selector_2}")

这将检查与两个选择器中的任何一个匹配的元素。