Skip to content

Instantly share code, notes, and snippets.

@ToJans
Last active December 14, 2015 21:38
Show Gist options
  • Save ToJans/5152005 to your computer and use it in GitHub Desktop.
Save ToJans/5152005 to your computer and use it in GitHub Desktop.
Zombify demo I'm currently using for a client POC
Client = require './client'
System = require './system'
should = require 'should'
client = new Client()
system = new System()
Feature "Authentication",
"As a site owner",
"I want the user",
"To be able to authenticate",
"So that I can provide a personalized user experience", ->
Scenario "An anonymous user does not require authentication before he can use the system", ->
Given "an anonymous user", (cb) -> client.visit cb,system.logout
When "he tries to use the system", (cb) -> client.visit cb,system.search
Then "he can use the system", -> should.exist(client.element(system.search))
Scenario "Successful authentication allows the user to use the system", ->
Given "an anonymous user", (cb) -> client.visit cb,system.logout
When "he authenticates", (cb) -> client.submit cb, system.login,system.login.data.valid
Then "he should be able to use the system", -> should.exist(client.element(system.search))
Scenario "Unsuccessful authentication asks for authentication again", ->
Given "an anonymous user", (cb) -> client.visit cb,system.logout
When "the user authenticates with invalid credentials", (cb) -> client.submit cb, system.login,system.login.data.invalid
Then "he receives an error message", -> client.element_text(system.login.error).should.not.be.empty
And "he is asked to authenticate again", -> should.exist(client.element(system.login))
Browser = require('zombie')
class Client
constructor: (base) ->
@base = base
@browser = new Browser()
element: (selector) ->
selector = selector.selector if selector.selector?
@browser.query(selector)
elements: (selector) ->
selector = selector.selector if selector.selector?
@browser.queryAll(selector)
element_text: (selector) ->
selector = selector.selector if selector.selector?
@browser.text(selector)
submit: (done,submit_button,data,allowed_statuscodes) =>
pars = submit_button
todo = () =>
if pars.submit_button?
submit_button = pars.submit_button
data = pars.data unless data?
allowed_statuscodes = pars.allowed_statuscodes unless allowed_statuscodes?
for key,val in data
@browser.fill("[input[name=#{key}]",val)
@browser.pressButton(submit_button,@handle_visit(done,allowed_statuscodes))
@browser.wait(pars.wait_in_Ms) if pars.wait_in_Ms
if (pars.url?)
@visit(todo,pars.url,allowed_statuscodes)
else
todo()
visit: (cb,url,allowed_statuscodes) =>
pars = url
url = pars.url if pars.url?
allowed_statuscodes = pars.allowed_statuscodes unless allowed_statuscodes?
@browser.visit(url, @handle_visit(cb,allowed_statuscodes,pars.wait_in_Ms))
handle_visit: (cb,allowed_statuscodes,wait_in_Ms) =>
=>
allowed_statuscodes?=[200]
if(@browser.statusCode not in allowed_statuscodes)
try
message = @browser.querySelector('h1').textContent
stacktrace = @browser.querySelector('tbody pre').textContent
console.error(message, stacktrace)
catch exception
throw exception
@browser.wait(wait_in_Ms) if wait_in_Ms
cb() if (cb?)
module.exports = Client
Driver = require 'zombify'
Client = require './client'
class System
constructor: ->
@driver = new Driver ''
@root =
url: "http://devserver/Orchard"
@login =
url: "#{@root.url}/Login"
selector: '#bxsLoginForm'
submit_button: '#bxsLoginButton'
wait_in_Ms: 250
data:
valid: { 'username-email':'[email protected]', password:'zzzz' }
invalid: { 'username-email':'[email protected]', password:'wwww' }
error:
selector: "#BXSLogInError"
@logout =
url: "#{@root.url}/qqqq/Login/Logout"
allowed_statuscodes: [200,500]
@search =
url: "#{@root.url}/Search"
selector: '#bxsSearch'
submit_button: "#bxsSearchButtonBig"
data:
valid:
'bxs.searchModel.IataFrom': 'Brussels, Belgium'
'bxs.searchModel.IataTo': 'Madrid, Spain'
@flights =
url: "#{root.url}/Flights"
start: (done) =>
@driver.start done
stop: (done) =>
@driver.stop done
add_client: =>
console.log "adding new client for #{@driver.baseHref}"
new Client()
console.log "client ready"
module.exports = System
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment