Capybara Ambiguity Resolution

前端 未结 9 1308
轮回少年
轮回少年 2020-12-23 13:59

How do I resolve ambiguity in Capybara? For some reason I need links with the same values in a page but I can\'t create a test since I get the error

Failure/         


        
9条回答
  •  天命终不由人
    2020-12-23 14:46

    You can ensure that you find the first one using match:

    find('.selector', match: :first).click
    

    But importantly, you probably do not want to do this, as it will lead to brittle tests that are ignoring the duplicate-output code smell, which in turn leads to false positives that keep working when they should have failed, because you removed one matching element but the test happily found the other one.

    The better bet is to use within:

    within('#sidebar') do
      find('.selector).click
    end
    

    This ensures that you're finding the element you expect to find, while still leveraging the auto-wait and auto-retry capabilities of Capybara (which you lose if you use find('.selector').click), and it makes it much clearer what the intent is.

提交回复
热议问题