custom_extensions.rb 1001 B

12345678910111213141516171819202122232425262728293031323334
  1. class Capybara::Node::Element
  2. # This is an extension to each node to check if the element
  3. # is moving or in a fixed position. This is especially helpful
  4. # for animated elements that cause flanky tests.
  5. # NOTE: In CI env a special sleep is performed between checks
  6. # because animations can be slow.
  7. #
  8. # @param [Integer] checks the number of performed movement checks
  9. #
  10. # @example
  11. # find('.clues-close').in_fixed_position.click
  12. # => waits till clues moved to final position and performs click afterwards
  13. #
  14. # @raise [RuntimeError] raised in case the element is
  15. # still moving after max number of checks was passed
  16. #
  17. # @return [Capybara::Node::Element] the element/node
  18. def in_fixed_position(checks: 100, wait: 0.2)
  19. previous = native.location
  20. (checks + 1).times do
  21. sleep wait
  22. current = native.location
  23. return self if previous == current
  24. previous = current
  25. end
  26. raise "Element still moving after #{checks} checks"
  27. end
  28. end