【问题标题】:Capybara expect page to have content more than onceCapybara 期望页面有不止一次的内容
【发布时间】:2015-09-17 15:44:25
【问题描述】:

有人知道如何查找一个页面是否有不止一次的文本吗?

我知道我可以使用

expect(page).to have_content("my-text")

检查文本是否出现一次。但是,我需要类似的东西

expect(page).to have_content("my-text").twice

(这不起作用)。

我正在使用 capybara v2.1.0。

【问题讨论】:

    标签: rspec cucumber capybara


    【解决方案1】:
    expect(page).to have_content("my-text", count: 2)
    

    将在现代版本的 Capybara 中做你想做的事,不确定这是否适用于 2.1 但值得一试(2.1 现在已经超过 2 年了)

    更新:在 Capybara 3.19+ 中现在也可以这样写

    expect(page).to have_content("my-text").twice
    

    【讨论】:

    • 对于任何寻找 minitest 变体的人,它是 assert_text "my-text", count: 2
    【解决方案2】:

    当您希望文本至少显示 2 次(或 2 次或更多)时,您必须使用 minimum 而不是 count

    expect(page).to have_text(/my-text/, minimum: 2)

    【讨论】:

    • 是的,如果你想要它至少 2 但 expect(page).to have_text(/my-text/, minimum: 2, maximum: 5) 或 `expect(page).to have_text(/my-text/, between: (2.. 5))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    相关资源
    最近更新 更多