How to return a value when using execute_script in capybara?

后端 未结 1 762
半阙折子戏
半阙折子戏 2021-01-17 07:26

I have a really simple line in a test that calls execute script like this:

puts page.execute_script(\"return somefunction();\").to_i.inspect
<
相关标签:
1条回答
  • 2021-01-17 07:46

    The Poltergeist driver is designed to return nil for execute_script:

    def execute_script(script)
      browser.execute(script)
      nil
    end
    

    Poltergeist will only return a value if you use the evaluate_script:

    def evaluate_script(script)
      browser.evaluate(script)
    end
    

    Capybara has corresponding methods for each - ie Session#execute_script and Session#evaluate_script. Your code should work if you switch to using evaluate_script (and as @AndreyBotalov points out, you also need to remove the return):

    puts page.evaluate_script("somefunction();").to_i.inspect
    
    0 讨论(0)
提交回复
热议问题