rails 3.1, capybara-webkit, why page.execute_script fails?

simo picture simo · Jan 22, 2012 · Viewed 8k times · Source

After installing capybara-webkit, I tried to try a test inside a spec, here is my test:

Capybara.default_driver = :webkit

page.execute_script("$('#submit_direction').attr('value', '1');$('#quizForm').submit()")

Then, I get this error:

/home/sam/.rvm/gems/ruby-1.9.3-p0@global/gems/bundler-1.0.21/lib/bundler/runtime.rb:136: warning: Insecure world writable dir /home/sam/Documents/RoR in PATH, mode 040777 undefined|0|ReferenceError: Can't find variable: $

Capybara::Driver::Webkit::WebkitInvalidResponseError: Javascript failed to execute

Any idea please ?

Answer

Kevin Davis picture Kevin Davis · Jan 23, 2012

I'm guessing the capybara-webkit JS driver is a bit dodgy.

From the capybara github page:

For simple expressions, you can return the result of the script. Note that this may break with more complicated expressions:

result = page.evaluate_script('4 + 4');

However, from the code you have there, it looks like you can do quite a few things with Capybara itself.

$('#submit_direction').attr('value', '1')

could be replaced with:

page.find("#submit_direction").set('1')

and

$('#quizForm').submit()

could be replaced with:

page.click_on('quizForm')

see the capybara docs on capybara actions