Coder_Nick
Coder_Nick

Reputation: 851

Rspec with Capybara not finding link or any elements

I am writing feature tests for my Rails 5 application. One feature is the ability to click a link with method POST.

visit "/admin/client_servers/#{client_server.id}"
click_on('Request Review From Application Owner')

I receive the error "Capybara::ElementNotFound: Unable to find link or button 'Request Review From Application Owner'"

Just experimenting, I check to see if the test can find any element I know is on the screen. I have one container div with a class of 'description' and the test is unable to find the element.

The previous test passes

  visit "/admin/client_servers/#{client_server.id}"
  expect(page).to have_text("Request Review From Application Owner")

So I know the text I want to click on appears on the page. My html is setup:

<div class="mark-ready-btn server-button" id="overall-tab" data-param="overall">
    <%=link_to admin_client_servers_update_review_status_path(status: 1, id: @server.id), class: "request-review-btn", method: 'POST' do%>
      <div class="server-mark-ready" id="overall-tab" data-param="overall">
        Request Review From Application Owner
      </div>
    <%end%>
</div>

I've checked but the test is unable to find the css tags '.mark-ready-btn server-button', '.request-review-btn', or '.server-mark-ready'. I've searched this problem online and looked into Capybara and Rspec documentation. Is there something I'm missing or a improper syntax I've written in the test? I'd appreciate any help on this issue.

Upvotes: 0

Views: 1746

Answers (3)

Matthew Jaeger
Matthew Jaeger

Reputation: 79

find("#overall-tab").click should do the work

Or if you really want to click on specific text try:

find('div', text: 'Request Review From Application Owner').click

Upvotes: 0

napster235
napster235

Reputation: 9

If Capybara can't find link or button, generally means that there is no link or button. You will have to use 'click' method.

From your html example, this should work

1.first('.server-mark-ready).click
2.page.all('.server-mark-ready)[#].click (in case there is more than one div that has that class)

Upvotes: 0

S.Kalra
S.Kalra

Reputation: 91

You can try -

within ".mark-ready-btn.server-button" do
  click_on ".server-mark-ready"
end

Or you can use "find"

find('.server-mark-ready').click

Upvotes: 1

Related Questions