Skip to content

Instantly share code, notes, and snippets.

@paulbjensen
Created May 20, 2013 10:09
Show Gist options
  • Save paulbjensen/5611416 to your computer and use it in GitHub Desktop.
Save paulbjensen/5611416 to your computer and use it in GitHub Desktop.
This is the world.coffee file used at Axisto Media for an application in current development. It demonstrates how to boot a selenium server just once, make it available to your step definitions, and kill it once all of your cucumber.js feature have finished running.
# 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}
@DavidSouther
Copy link

How do you ensure the browser is closed?

@tthew
Copy link

tthew commented Jan 13, 2014

+1 where does this close the browser?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment