0

I want to execute some JS library in RSpec.

for example

I can execute "Payment Library" of JavaScript Library in HTML like below.

<!DOCTYPE html>
<html>
   <head>
      <script src="https://sample.example/payment.js"></script>
   </head>
   <body>
      <div id="foo"></div>
   </body>
   <script>
      $(document).ready(function(){
         // Payment depends on "https://sample.example/payment.js"
         Payment.init("11111");
         Payment.getToken({cardNo: "1111111111111111", expired: "2001"});
       });
   </script>
</html>

Is it possible to load external JavaScript Library like using "script" tag in RSpec like this.

js_code = <<'EOS'
{
         <script src="https://sample.example/payment.js"></script>
         Payment.init("11111");
         Payment.getToken({cardNo: "1111111111111111", expired: "2001"});
}
EOS

ExecJS.eval js_code

Pleas give me any advice.

Thanks.

Kouta Osabe
  • 121
  • 1
  • 10
  • 1
    Not a direct answer to your question, but have you thought about using a testing framework that's meant to work with js? [Jasmine](https://jasmine.github.io/), for example? It's really not a good practice to test code written in one language using a framework in another. Cross-language support is often limited, and always wonky. And if you're looking to test _integration_ between the frontend and backend, you should look into using [Capybara with Selenium](https://github.com/teamcapybara/capybara) – Glyoko Feb 24 '18 at 03:22

1 Answers1

0

A good read, recent enough to take it into account : https://robots.thoughtbot.com/headless-feature-specs-with-chrome

Basically, it'll wait for your js (the one you included in your pages) to finish execute before validating your tests. This is what you want.

From there, it'd be advisable to avoid any js injections (even if it is available), and then rely entirely on the headless browser setup

Ben
  • 5,030
  • 6
  • 53
  • 94