2014年3月10日星期一

Use cucumber.js in browser without Node

This is just a detailed guide from question Running Cucumber.js in Browser where people asked followup on my answer.

Usage

Here's the steps on how to run the example with node runtime:

    $ git clone https://github.com/cucumber/cucumber-js.git
    $ cd cucumber-js/example
    $ npm install
    $ node server.js
 
Then open your browser and navigate to http://localhost:9797/, click the button Run feature. you should see the test result in green background.

Now you would think you must depends on node to use cucubmer.js, but you will find nodejs just make you get the live result easier, NOT a mandatory runtime dependency.

Resources

Start with server.js, it's a connect application to host a web server so that you can access the example over http. all resources you get over http can be saved as static content and you can host them with any web server:

  • /index.html - static file
  • /example.css - static file
  • /vendor/* - static files
  • /example.js - static file
  • /cucumber.js - this is the key resource and yes, it can be saved as static file.

/cucumber.js

In a nutshell, it's compiled javascript. cucumber.js is designed for nodejs runtime where the dependencies are managed by npm, the example code is introduced to use browsers in this commit. no surprise, it uses browserify.

You can get compiled cucumber.js for browsers by the following command:

    $ cd cucumber.js
    $ node -e "var str = require('./bundler')().bundle(); require('fs').writeFileSync('./cucumber.js', str);"


没有评论: