thoughtbot / capybara-webkit

A Capybara driver for headless WebKit to test JavaScript web apps
https://thoughtbot.com/open-source
MIT License
1.97k stars 427 forks source link

Element isn't found when javascript is enabled #958

Closed majioa closed 7 years ago

majioa commented 8 years ago

Currently on the version capybara = 2.7.0 and capybara-webkit = 1.11.1 I get strange behaviour of the driver. When javascript is disabled (no @javascript tag declared in the cucumber env) the code selecting a radio button works fine:

 choose("$ 39.99 /year")

When I enable js with @javascript tag in cucumber test env, I get the following error:

Unable to find radio button "$ 39.99 /year" (Capybara::ElementNotFound)

I'm pretty sure that the HTML code is the same, I've got it with launchy's save_and_open_page method.

Why the behaviour is rise up, and how to fix it?

twalpole commented 8 years ago

Other than JS the other major difference between running in JS and non-JS mode is that when in JS mode the drivers generally fully process CSS. If you look at your html in a real browser it's highly likely your radio button elements are actually hidden and replaced with images that allow them to look the same across multiple browsers. In newer versions of Capybara there are options for dealing with that which can be passed to #choose however in version 2.7.x you'll need to manually click the label element associated with the radio. In your case that's probably

find(:label, "$ 39.99 /year").click
majioa commented 8 years ago

@twalpole which version supports that keys? and how to use them?

twalpole commented 8 years ago

@majioa It was added in 2.8.0 and then had a fix in 2.9.1 - https://github.com/jnicklas/capybara/blob/master/History.md. It can be used like

choose("$ 39.99 /year", allow_label_click: true)  # or set the global Capybara.automatic_label_click = true