Skip to content

Instantly share code, notes, and snippets.

@redbar0n
Last active January 28, 2016 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save redbar0n/8fdc90cbc918d286725e to your computer and use it in GitHub Desktop.
Save redbar0n/8fdc90cbc918d286725e to your computer and use it in GitHub Desktop.
capybara-webkit evaluate_script vs execute_script
Regarding this article: http://makandracards.com/makandra/12317-capybara-selenium-evaluate_script-might-freeze-your-browser-use-execute_script
code:
puts page.evaluate_script("2+3;").to_i
output:
Started "Evaluate(2+3;)"
Finished "Evaluate(2+3;)" with response "Success(5)"
Wrote response true "5"
5
code:
puts page.execute_script("return 2+3;").to_i;
output:
Started "Execute(return 2+3;)"
undefined|1|SyntaxError: Return statements are only valid inside functions
Finished "Execute(return 2+3;)" with response "Failure({"class":"InvalidResponseError","message":"Javascript failed to execute"})"
Wrote response false "{"class":"InvalidResponseError","message":"Javascript failed to execute"}"
# so presumably you need to wrap it in a self-executing anonymous function like below
code:
puts page.execute_script(%Q{ (function(){ return 2+3; })(); }).to_i;
output:
Started "Execute( (function(){ return 2+3; })(); )"
Finished "Execute( (function(){ return 2+3; })(); )" with response "Success()"
Wrote response true ""
0
# but it still doesn't actually return the value to the surrounding context
# So I'm not seeing the following behavior as mentioned in the article:
page.execute_script("return 2 + 3")
# => 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment