Posted over 4 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:

Copy
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:

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

Many Bothan spies have died to bring you this card.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Author of this card:

Avatar
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