Posted about 5 years ago. Visible to the public. Repeats.

Capybara/Selenium: evaluate_script might freeze your browser, use execute_script

In a nutshell: Do not use evaluate_script. Use execute_script instead!

Capybara gives you two different methods for executing Javascript:

page.evaluate_script("$('input').focus()") page.execute_script("$('input').focus()")

While you can use both, the first line (with evaluate_script) might freeze your Firefox window for 10 seconds.

The reason is that evaluate_script will always return a result. The return value will be converted back to Ruby objects, which in case of complex objects (e.g. a jQuery collection) is very expensive.

Now that you know this, just forget that evaluate_script ever existed and use execute_script.

In cases where you don't care about the return value, it will just run the command. When you do need a return value, simply return it explicitly in your JavaScript snippet, like you would in any normal piece of JavaScript code:

page.execute_script("return 2 + 3") # => 5

Many Bothan spies have died to bring you this card.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Author of this card:

Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandropedia