Musings about Entrepreneurship, Technology and Software Development

Accidental Technologist

  • Home
  • About
  • Still River Software
  • Privacy Policy

When You Can’t Check a Checkbox using Capybara

Tweet

In my last post I documented an issue I was having using Capybara on a client’s Ruby on Rails application with a ReactJS frontend.  I wanted to share another issue I came  across during my feature testing escapades.

The Problem

During my recent feature testing project I had a form which has a checkbox on it. The checkbox had a label with it. Did I mention this is a ReactJS frontend? I’m not sure if this is specific to ReactJS, but I suspect it isn’t. I think other frontend JavaScript frameworks may exhibit the same problem.

The Ruby code for my feature test is dead simple:

  check “English”

That’s it. The test should run and when it finds the checkbox with a label of English, the checkbox should be checked. But, it doesn’t work. After many attempts at making this work and more Google searches than I can remember..I ended up at the Capybara mailing list.

The Solution

Thomas Walpole was kind enough to reply with his thoughts on the matter:

99.9% sure your checkbox isn’t actually visible on the screen.  

What you’re describing as the “checkbox” is probably an image (probably added via CSS pseudo elements) being shown in place of the actual checkbox input element to ensure the same styling across different browsers.  If the checkbox has a label element correctly attached to it you can use `check(‘whatever’, allow_label_click: true)` – https://www.rubydoc.info/ github/jnicklas/capybara/ Capybara/Node/Actions#check- instance_method –  to tell Capybara to click the label element instead of the hidden checkbox input to toggle state.  If you don’t have an associated label then you’ll need to find whatever element is actually visible on the page and click it instead.

Changing my test to include this for the checkbox, worked perfectly. 

  check(“English", allow_label_click: true)

I hope someone finds this valuable and will save them some time and hair pulling.

Share this:

  • LinkedIn
  • Twitter
  • Facebook
  • Email
  • More
  • Pinterest
  • Tumblr
  • Pocket
  • Reddit

December 5, 2018 Posted in Ruby on Rails Tagged With: capybara, rspec, Ruby on Rails, tests

Popular Posts

  • 10 Alternative Ruby Web Frameworks
  • 7 Resources Every JavaScript Developer Should Know
  • Setting up SQLite3 for Ruby on Rails Development
  • Running Rails 3 on Windows
Micro.blog

Categories

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Latest Tweets

  • RT @dhh: Away CEO apologies for past bad behavior, then continues the abuse in the afternoon. How incompetent do you have to… https://t.co/LEIoyv7hy1

    1 day ago
  • Amazon is really screwing up with their deliveries this holiday season. As a Prime member I usually get shipments i… https://t.co/ihyf16Yyc4

    1 day ago
  • Reading @Avdi’s tweets about trying to get a tech job are depressing. It’s sad the process doesn’t respect the cand… https://t.co/Zkt9vBh9cL

    1 day ago
  • After upgrading to iPadOS, my iPad Pro gets terrible battery life. Come on @Apple, get your act together.

    2 days ago
  • RT @scottw: Tailwind CSS - Next to Rails no other library, tool, framework has done more to make it easier to ship new things t… https://t.co/MXMo3hVAfZ

    5 days ago

Tags

Agile amazon Android Apple App Store ASP.NET MVC book bootstrapping Business conference Customer Service Droid X email entrepreneurship functional programming Google InfoQ InstantRails iOS iPad iPhone JavaScript mac microconf Microsoft mixergy mobile objective-c Open Source podcast rails Rails3 railsconf RSS Ruby Ruby on Rails scala sinatra Software swift twitter Windows WordPress WPEngine xcode

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2019 · Genesis Minimal Notebook on Genesis Framework · WordPress · Log in

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.