Created
February 7, 2015 19:31
-
-
Save joelataylor/58ec133e5e06b135bd5f to your computer and use it in GitHub Desktop.
PhantomJS test
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
var page = new WebPage(), | |
testindex = 0, | |
loadInProgress = false, | |
fs = require('fs'); | |
page.onConsoleMessage = function(msg) { | |
console.log(msg); | |
}; | |
page.onLoadStarted = function() { | |
loadInProgress = true; | |
console.log("load started"); | |
}; | |
page.onLoadFinished = function() { | |
loadInProgress = false; | |
console.log("load finished"); | |
}; | |
var steps = [ | |
function() { | |
console.log("Load Login Page"); | |
page.settings.userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.6 Safari/537.11"; | |
page.open("http://the.webs.com/v.htm"); | |
}, | |
function() { | |
console.log("Enter Credentials"); | |
page.injectJs("jquery.min.js"); | |
page.evaluate(function() { | |
$("input[name='user']").val('abc'); | |
$("input[name='pwd']").val('123'); | |
console.log(document.title); | |
}); | |
}, | |
function() { | |
console.log('login'); | |
page.evaluate(function() { | |
console.log(document.title); | |
$("input[type='submit']").click(); | |
}); | |
}, | |
function() { | |
console.log('logged in?'); | |
page.evaluate(function() { | |
var videos = []; | |
$('a[href*="youtube.com"]').each(function(){ | |
var link = $(this).attr('href'); | |
var title = $(this).contents().filter(function() {return !!$.trim(this.innerHTML||this.data);}).first().text(); | |
var videoObj = {}; | |
videoObj.link = link; | |
videoObj.title = $.trim(title); | |
console.log('push video obj to array'); | |
videos.push(videoObj); | |
}); | |
console.log(JSON.stringify(videos)); | |
$.ajax({ | |
url:'https://mysite.com/super/receive.php', | |
type: 'POST', | |
crossDomain: true, | |
dataType: 'jsonp', | |
data: {data:videos} | |
}) | |
.always(function(results){ | |
console.log('exit'); | |
}) | |
.fail(function(error){ | |
console.log('did we error?'); | |
console.log(error); | |
}); | |
console.log('after ajax'); | |
}); | |
//writeToFile(result); | |
} | |
]; | |
interval = setInterval(function() { | |
if (!loadInProgress && typeof steps[testindex] == "function") { | |
console.log("step " + (testindex + 1)); | |
steps[testindex](); | |
//page.render("images/step" + (testindex + 1) + ".png"); | |
testindex++; | |
} | |
if (typeof steps[testindex] != "function") { | |
console.log("test complete!"); | |
phantom.exit(); | |
} | |
}, 50); | |
function writeToFile(content) { | |
console.log('writing to file?'); | |
var path = 'videos.txt'; | |
fs.write(path, content, 'w'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment