Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test in Capybara if an element is clickable?

I need to test in Capybara if on a page an input text box is visible and clickable. I know how to test about visibility, but I couldn't find a method like .clickable or something in that spirit. How does one do this in Capybara?

like image 888
Alexander Popov Avatar asked Feb 19 '14 16:02

Alexander Popov


2 Answers

It sounds like the OP's needs have been met, but for future explorers, here's some tools to tell if something is clickable.

Test whether the thing can be clicked (Rspec && Capybara):

it "is clickable" do
  expect{ find('.your_selector').click }
    .not_to raise_error(Capybara::Poltergeist::MouseEventFailed)
end
it "isn't clickable" do
  expect{ find('.your_selector').click }
    .to raise_error(Capybara::Poltergeist::MouseEventFailed)
end

Click it if possible and do something else if not, per Joe Susnick's answer:

clickable = expect{ find('.your_selector').click }.not_to raise_error(Capybara::Poltergeist::MouseEventFailed)
if clickable
  plan_a
else
  plan_b
end
like image 116
kaimonster Avatar answered Nov 06 '22 12:11

kaimonster


Putting this as an answer, from comments on the original question.

You don't need to test clickability (i.e getting the input field to blink when clicked on), because I think that's browser dependent. You don't need to do anything to get that functionality. So if it's visible, and an input field, clicking in it will get that result. Otherwise, if it's not visible, it can't be clicked on anyways, so you're fine. I don't think you need to test anything about clickability, just visibility.

like image 1
MrDanA Avatar answered Nov 06 '22 11:11

MrDanA