Capybara will skip invisible elements by default

While working on an acceptance test for a date range filter in GO, we were having an issue where Capybara couldn’t find an element on the page, even though we could verify it was there. Eventually we realized that the element had an opacity of 0, and that Capybara was passing it over. To illustrate, imagine you have an element with the id #myElement.

CSS:

#myElement { opacity: 0; }

And in your Rails spec:

page.find("#myElement");

The spec will fail, because #myElement can’t be found.

Fortunately, there is a visible option that can be set to false so that Capybara doesn’t skip the element. So now, changing the line in the spec to:

page.find("#myElement", visible: false);

will cause it to pass.

Written on February 24, 2017 by jerridanquiring