Skip to content

Instantly share code, notes, and snippets.

@seriallos
Last active December 16, 2015 14:38
Show Gist options
  • Save seriallos/5449668 to your computer and use it in GitHub Desktop.
Save seriallos/5449668 to your computer and use it in GitHub Desktop.
Playing around with ZombieJS and Mocha
Browser = require 'zombie'
assert = require 'assert'
getBrowser = () ->
browser = new Browser()
browser.site = "https://www.infinitecrisis.com"
return browser
describe "login", () ->
before () ->
@browser = getBrowser()
# visit the page fresh before each test
beforeEach ( done ) ->
@browser.visit "/en/auth/login", ( e, browser ) ->
done()
it "should refuse empty submissions", (done) ->
# make sure to pull this variable into function scope for callback handling
browser = @browser
browser.pressButton( "#submit" ).then( () ->
# error text
browser.assert.text( '#login #identity-element li.error', "This field is required." )
# error CSS classes
browser.assert.hasClass( '#identity', 'error' )
browser.assert.hasClass( '#secret', 'error' )
return
).then( done, done )
it "shows an error on bad password", (done) ->
browser = @browser
# fill in the form with a bad password
browser.fill( "#identity", "*************@gmail.com" )
browser.fill( "#secret", "bad password" )
browser.pressButton( "#submit" ).then( () ->
# make sure we're back on the login page
browser.assert.url( pathname: "/en/auth/login" )
# make sure an error is shown
browser.assert.element( "#auth-login ul.failure li.error" )
return
).then( done, done )
it "takes the user to the account page on success", (done) ->
browser = @browser
# fill in the form with good data
browser.fill( "#identity", "**********@gmail.com" )
browser.fill( "#secret", "*********" )
browser.pressButton( "#submit" ).then( () ->
# did we get redirected?
browser.assert.redirected("Not redirected after submit")
# are we on the account page?
browser.assert.url( pathname: "/en/account", "visitor not redirected to account page" )
return
).then( done, done )
$ mocha -t 20000 -R spec --compilers coffee:coffee-script test.coffee
login
✓ should refuse empty submissions (79ms)
✓ shows an error on bad password (1838ms)
✓ takes the user to the account page on success (4367ms)
3 tests complete (11 seconds)
$ mocha -t 20000 -R xunit --compilers coffee:coffee-script test.coffee
<testsuite name="Mocha Tests" tests="3" failures="0" errors="0" skip="0" timestamp="Wed, 24 Apr 2013 04:42:40 GMT" time="14.614">
<testcase classname="login" name="should refuse empty submissions" time="0.225"/>
<testcase classname="login" name="shows an error on bad password" time="2.638"/>
<testcase classname="login" name="takes the user to the account page on success" time="4.369"/>
</testsuite>
$ mocha -t 20000 -R nyan --compilers coffee:coffee-script test.coffee
3 -_-__,------,
0 -_-__| /\_/\
0 -_-_~|_( ^ .^)
-_-_ "" ""
3 tests complete (16 seconds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment