Skip to content

Instantly share code, notes, and snippets.

@leafo
Last active August 29, 2015 14:12
Show Gist options
  • Save leafo/6d84ddc65244b8d72ceb to your computer and use it in GitHub Desktop.
Save leafo/6d84ddc65244b8d72ceb to your computer and use it in GitHub Desktop.
-- this is how you write specs:
import load_test_server, close_test_server from require "lapis.spec.server"
import request_with_phantom from require "spec_integration.helpers"
describe "integration", ->
setup ->
load_test_server!
teardown ->
close_test_server!
it "should load games page", ->
request_with_phantom "/games", {
screenshot: "browse.png"
sleep: 100
}
it "should load games page and click something", ->
request_with_phantom "/games", {
screenshot: "browse_clicked.png"
evaluate: [[
$(".show_genre_btn:first").click()
setTimeout(function() {
finished()
}, 250)
]]
}
-- spec_integration/helpers.moon
json = require "cjson"
screenshots_taken = 0
clean_screenshots = ->
os.execute "rm spec_integration/screenshots/*.png"
request_with_phantom = (url, opts={}, ...) ->
import get_current_server from require "lapis.spec.server"
server = get_current_server!
host, path = url\match "^https?://([^/]*)(.*)$"
unless host
host = "127.0.0.1"
path = url
full_url = "http://#{host}:#{server.app_port}#{path}"
if opts.screenshot
if screenshots_taken == 0
clean_screenshots!
screenshots_taken += 1
args = json.encode {
headers: opts.headers
screenshot: opts.screenshot and "spec_integration/screenshots/#{opts.screenshot}"
evaluate: opts.evaluate
sleep: opts.sleep
}
args = args\gsub "'", [['"'"']]
cmd = "phantomjs spec_integration/runner.js '#{full_url}' '#{args}'"
file = assert io.popen cmd, "r"
out = file\read "*a"
file\close!
error out unless out\match "^%s*$"
{ :request_with_phantom }
# runner.coffee, for phantomjs
phantom.onError = (msg, trace) =>
console.log "Phantom error: #{msg}"
phantom.exit(1)
page = require("webpage").create()
system = require "system"
debug = ->
system.stderr.write (a for a in arguments).join("\n") + "\n"
if system.args.length == 1
console.log "Usage: runner.js <some URL>"
phantom.exit()
url = system.args[1]
opts = system.args[2]
if opts
opts = JSON.parse opts
opts ||= {}
page.onError = (msg, trace) =>
full = for t in trace
line = " #{t.file || t.sourceURL}: #{t.line}"
line += " (in function #{t.function})" if t.function
line
full.unshift msg
full.unshift "Error requesting #{url}\n"
console.log full.join "\n"
phantom.exit(1)
# page.onConsoleMessage = (msg, line, source) =>
# debug "Console: #{msg}"
page.viewportSize = { width: 1200, height: 800 }
page.open url, {
operation: opts.method || "GET"
headers: opts.headers
}, =>
finished = ->
if opts.screenshot
page.render opts.screenshot
phantom.exit()
if opts.evaluate
page.evaluate (code) ->
action = new Function "finished", code
action -> window._async_req_finished = true
, opts.evaluate
# wait until it's over
check_finished = ->
setTimeout =>
is_done = page.evaluate ->
window._async_req_finished
if is_done
finished()
else
check_finished()
, 10
check_finished()
else if opts.sleep
setTimeout finished, opts.sleep
else
finished()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment