Feature test tips using capybara


Can not find element PART 1

Capybara can not find invisible element by default.
You need set visible option like this.

find(selector, visible: false)
  • true - only finds visible elements.
  • false - finds invisible and visible elements.
  • :all - same as false; finds visible and invisible elements.
  • :hidden - only finds invisible elements.
  • :visible - same as true; only finds visible elements.

Can not find element PART 2

Capybara retry to find after 3 seconds by default when Capybara can not find the element. (Capybara.default_wait_time)
You could need more then 3 seconds to wait to find element. (e.g. the element is appeared after ajax request.)

try to set wait option. :)

## some async logic .....

find(selector, wait: 4) # can find

You had better not set this option a lot. Because Capybara stop test to wait so test time can be long.

Can not find element which is inside head tag

As mention above, Capybara can not find invisible element.
The elements which is inside head, <script> and <style> can not be found by default too.

So you need to set visible: false option.

Can not find text

As mention above, Capybara can not find invisible element.
The text which is inside invisible element can not be got by default too.

You need to set visible option to get text which is inside invisible element.

find(selector, visible: false).text(:all)
  • :all → all text (visible or not)
  • :visible → only visible text

If the result which is test links is not expected You need to check visiblity of that links.
You can not test invisible links using have_link so you need to check link text and href attribute individually.


link_tag = find(selector) # link is invisible
expect(link_tag).to have_link(text, href: "http://stores.jp") # failed


link_tag = find(selector, visible: alse) # find invisible link

expect(link_tag).to have_text(:all, text) # check link text
expect(link_tag[:href]).to eq("http://stores.jp") # check href attribute

Can not get screenshot

You need to set js:true option in describe or context

describe "test", js:true do
  it "screenshot test" do

Too slow

To visit page in feature test is too slow.
You need to reduce access page.

Page object is old

check this article