Created
March 28, 2012 04:42
-
-
Save joeytrapp/2223683 to your computer and use it in GitHub Desktop.
CoffeeScript: PhantomJS Jasmine Scrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Exit early if users don't pass at least 1 argument | |
if phantom.args.length < 1 | |
console.log "Usage: phantomjs run-jasmine.coffee URL [timeout]" | |
phantom.exit() | |
# Collect the passed int args or set to default value | |
url = phantom.args[0] | |
timeout = phantom.args[1] or 6000 | |
# Create the page object to load the test page in | |
page = new WebPage() | |
# Start the timer for the test | |
testStart = new Date().getTime() | |
# This method will use the test method to see if the page is ready to be scrapped | |
# and then call the scrapper function | |
defer = (test, scrapper) -> | |
start = new Date().getTime() | |
condition = false | |
func = -> | |
if (new Date().getTime() - start < timeout and not condition) | |
condition = test() | |
else | |
if not condition | |
console.log "Timeout passed before the jasmine .finished-at element was set" | |
phantom.exit() | |
else | |
time = new Date().getTime() - start | |
clearInterval interval | |
scrapper() | |
phantom.exit() | |
interval = setInterval func, 100 | |
# Open the passed in url | |
page.open url, (status) -> | |
phantom.exit() if status != "success" | |
# The method used to test if the page is ready to be scrapped | |
test = -> | |
page.evaluate -> | |
return document.querySelector ".banner .duration" | |
# Method called to scrap the test page and output the content to the console | |
scrapper = -> | |
all = page.evaluate -> | |
specs = document.querySelectorAll ".specSummary" | |
spec.getAttribute("class").search(/failed/) is -1 for spec in specs | |
list = page.evaluate -> | |
allFails = [] | |
failures = document.querySelectorAll ".specDetail.failed" | |
for fail in failures | |
obj = | |
assertion: fail.querySelector(".description").innerText | |
messages: [] | |
messages = [] | |
msgs = fail.querySelectorAll ".resultMessage.fail" | |
obj.messages.push msg.innerText for msg in msgs | |
allFails.push obj | |
allFails | |
time = page.evaluate -> | |
document.querySelector(".banner .duration").innerText | |
time = time.slice(0, 1).toUpperCase() + time.slice(1) | |
time = "\033[0;32m#{time}ms\033[0;37m" if time < 500 | |
time = "\033[0;33m#{time}ms\033[0;37m" if time >= 500 and time < 1000 | |
time = "\033[0;31m#{time}ms\033[0;37m" if time >= 1000 | |
fails = 0 | |
# Outputs a '.' or 'F' for each test | |
console.log all.reduce((str, a) -> | |
fails += 1 unless a | |
str += if (a) then "\033[0;32m.\033[0;37m" else "\033[0;31mF\033[0;37m" | |
, "") | |
console.log "" | |
# If the list of failures is not empty output the failure messages | |
if list.length | |
console.log "Failures:" | |
console.log "" | |
msgSpacer = " " | |
i = 1 | |
for fail in list | |
spacer = " " + i + ") " | |
console.log spacer + fail.assertion | |
for message in fail.messages | |
subspacer = "" | |
for i in [1..spacer.length] | |
subspacer += " " | |
console.log subspacer + "\033[0;31m#{message}\033[0;37m" | |
i += 1 | |
console.log "" | |
console.log "#{time}." | |
if fails == 0 | |
console.log "\033[0;32m#{all.length} examples, #{fails} failures\033[0;37m" | |
else | |
console.log "\033[0;31m#{all.length} examples, #{fails} failures\033[0;37m" | |
# Call the defer method passing in the test and scrapper methods | |
defer test, scrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment