Skip to content

Instantly share code, notes, and snippets.

@heath
Forked from paulbjensen/world.coffee
Created December 20, 2013 20:43
Show Gist options
  • Save heath/8061240 to your computer and use it in GitHub Desktop.
Save heath/8061240 to your computer and use it in GitHub Desktop.
# Selenium launcher is a great npm module for booting
# selenium server via a Node.js process.
selenium = require 'selenium-launcher'
soda = require 'soda'
process.env.NODE_ENV = "cucumber"
# This is our app's file.
server = require '../../server'
# We add some empty variables to store the web browser
# and selenium server objects.
browser = null
seleniumServer = null
appUrl = "http://localhost:#{server.app.config.port}"
webBrowser = "firefox" #"googlechrome"
# We use this to create multiple browser instances
# for scenarios that test x number of users
# interacting with the app
createBrowser = (cb) ->
localBrowser = soda.createClient
host : seleniumServer.host
port : seleniumServer.port
url : appUrl
browser : webBrowser
cb localBrowser
# This is a helper function used to DRY up our
# step definitions
wrap = (funk, cb) ->
funk.end (err) ->
if err?
console.log err
cb.fail err
else
cb()
World = (callback) ->
# If this is running for the 1st time,
# boot the selenium server
if browser is null
selenium (err, selenium) =>
# When cucumber.js finishes,
# kill the selenium server
process.on 'exit', -> selenium.kill()
process.on 'error', -> selenium.kill()
process.on 'uncaughtException', (err) ->
console.error err
console.log err.stack
selenium.kill()
process.exit 1
# Store the selenium server, so that
# it is used by the other scenarios
seleniumServer = selenium
# Store the browser for use in the
# step definitions
browser = soda.createClient
host : selenium.host
port : selenium.port
url : appUrl
browser : webBrowser
@browser = browser
@createBrowser = createBrowser
callback {@browser, @createBrowser}
else
# We have already booted selenium, so
# we reuse the server
@browser = browser
@createBrowser = createBrowser
callback {@browser, @createBrowser}
module.exports = {World, server, appUrl, wrap}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment