2

I am using Capybara(capybara 3.1.0) for system tests in Rails project(Rails 5.2.0).

What is the way to ensure element is not visible.

I was using the visible: false option until I just found it matches visible elements, too.

For example I use:

find("h1", visible: false).text

There is no exception, and the text of the h1 is printed in the console, while h1 is definitely visible.

Is this expected? What is the logic behind this behavior? And what is the correct way to ensure element is not visible?

Jeni
  • 1,088
  • 12
  • 30

1 Answers1

2

When Capybara was first released, the value of visible (true or false) meant to enable or disable the visibility filter, for legacy test reasons that has stayed. You can also specify values of :visible, :hidden, :all (https://www.rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/Finders#find-instance_method) where :visible behaves the same as true, :all behaves the same as false and :hidden will return only non-visible elements.

This means your find would need to become

find("h1", visible: :hidden).text(:all)

With the :all parameter needed if you want the text because text defaults to only visible text (https://www.rubydoc.info/github/jnicklas/capybara/Capybara/Node/Element#text-instance_method)

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78