Rafael Hassegawa
Rafael Hassegawa

Reputation: 11

How do I click this button using Ruby Capybara?

I'm trying to click a button, but I'm getting the following error:

Unable to find link or button "My Tasks" (Capybara::ElementNotFound)

This is what I get when I inspect it on Chrome:

<a class="btn btn-lg btn-success" href="/tasks" role="button">My Tasks</a>

And this is my code (my steps.rb):

 # go to my tasks page
    def visitTasksPage
      page.has_content?('Signed in succesfully') do
        clicK_link 'My Tasks'
      end
      self
    end

I'm new to Ruby Capybara, can anyone help me please?

Upvotes: 1

Views: 278

Answers (1)

geminae
geminae

Reputation: 23

The problem might be that it's really a link that you're trying to click (an <a href="/some-link">Some Text</a> tag) not a button (a <button type="button">Some text</button> tag). To not have this problem, I recommend that you use the following method:

click_on('My Tasks') # clicks on either links or buttons

Or it could be that the link or button does not appear on the page before Capybara times out. Capybara by default waits 2 seconds before it times out, but you can extend it with

Capybara.default_max_wait_time = 9

Another thing you can do to debug this problem is add a call to binding.pry right before clicking the link/button and check if the button is actually there with the text you expect it to have:

page.has_content?('Signed in succesfully') do
  binding.pry # at this point you can test if the link is really there with page.has_content?('My Tasks')
  click_link 'My Tasks'
end

Note you would need to install pry by adding it to you Gemfile or with gem install pry.

Upvotes: 1

Related Questions