Phantomjs could be used for frontend functional testing.
Sometimes it could be used for rich web application html snapshots for search engine crawlers
Let’s use it for cheating.
In this article I’ll describe creating process of very simple html+js online game bot that running headlessly 24/7 on remote server (I’ll use Heroku).
Login and main game logic
There’s basically two lifecycle parts that need to be maintained: authentication and main lifecycle.
First and most easy way to do authentication is just fill login/pass fields on each run. It is not the best way because game could use captcha after several requests (and Godville is using it) so let’s plug in cookies:
phantomjs --cookies-file=cookies.txt bot.js
Where bot.js is main script file. File cookies.txt will be created automatically and will remain in project folder after you stop application. It will be used on next run making your bot automatically authenticated and redirected to main game page by server.
Detecting if bot is logged in depends on game mechanic. For Godville it could be done by URL checking after main game page request (/superhero). If bot is not logged in, it will be redirected to /login page.
1 2 3 4 5 6 7 8 9 10 11 12
For debugging you could use page.render function that makes page screenshot:
and also write console.log output. By default it will not be printed in application console so additional code needed:
1 2 3
Deploy on Heroku
Create application-specific Procfile:
worker: phantomjs --cookies-file=cookies.txt bot.js
Assuming you have Heroku toolbelt installed already, do
heroku create, then add and commit Procfile and script files to git initialized by this command git repo. Add necessary for phantomjs buildpack:
and then push your changes:
git push heroku master.
Make sure that your worker is running:
heroku ps:scale worker=1
That’s it. And there’s my bot repository