Roman
Roman

Reputation: 763

How to check for a disabled div that behaves like a button using Rails Capybara?

I have an div that work as button:

<div class="dropdown">
<div class="btn dropdown-toggle" data-toggle="dropdown" disabled="">Export to file</div>
   <div class="dropdown-menu">
      <a class="dropdown-item" fref="/1">1</a>
      <a class="dropdown-item" href="/2">2</a>
      <a class="dropdown-item" href="/3">3</a>
   </div>
</div>
</div>

I need to check that it is not clickable using Capybara, i try this:

scenario 'do not allow export if tracking time empty' do
  have_css(".dropdown>.btn[disabled].dropdown-toggle")
end
# and this 
expect(page).to have_button("Export to", :disabled => true)

But this don't work, any suggestion ? Thankee)

Upvotes: 0

Views: 740

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49920

For the HTML you've shown

expect(page).to have_css('.btn.dropdown-toggle[disabled]', text: 'Export to file')

should verify the HTML you've shown is on the page and visible. It won't actually test that the button is disabled - just that the element has the specified classes/attributes. That might be acceptable if you're using a 3rd party library to handle these buttons which has its own tests you trust. If you're handling the disabling of the button using your own JS though you might want to actually verify that it's disabled with something like

page.find('div.btn.dropdown-toggle[disabled]', text: 'Export to file').click
expect(page).not_to have_link('2', class: 'dropdown-item')

You can't use have_button because that finds HTML standard buttons elements (button, input type=button, etc).

Note: If you're using a lot of these buttons in your app you might want to look into writing a custom Capybara selector for dealing with them.

Upvotes: 1

Greg
Greg

Reputation: 6659

I'd suggest: don't test for elements attributes. Try to click it then test that nothing happens.

Upvotes: 1

Related Questions