断言是否存在两个字符串中的任何一个

Assert if any of two strings are present

我的网页有两个不同的字符串,具体取决于一天中的时间 - "Good day" 或 "Good night"。

有什么方法可以做某种 assert_content(my_string) 来检查是否存在两个给定字符串之一?

出现哪一个并不重要,重要的是那个。

如果使用 MiniTest 那么你可以使用 assert_match:

assert_match /Good day|Good night/, string

如果使用 RSpec 那么你可以使用 match:

expect(string).to match /Good day|Good night/

虽然塞巴斯蒂安的答案在您检查时只要页面已加载就会起作用,但它会失去 Capybaras waiting/retrying 行为,如果用于在页面更改时检查内容(立即单击按钮等后)。

使用 minitest 时,更好的解决方案是继续使用 Capybara 提供的 assert_content 匹配器,但将其传递给正则表达式

assert_content(/Good day|Good night/)

如果使用 RSpec 你也可以这样做

expect(page).to have_content(/Good day|Good night/)

或者您可以以更易读的方式编写它

expect(page).to have_content('Good day').or(have_content('Good night'))