Hack JavaScript games to improve your tests


Over the past six months or so, I have learned, intermittently, to code in javascript. this process mainly involved:

  • write simple games.

  • hack other games to see how they were written.

I’m going back to the good old days where we could ‘break’ the zx Spectrum game and see the source, or take the games apart on the atari st and hang / hack them through debuggers and monitors.

to do all of this now, javascript and browser developer tools are perfect companions.

learn to hack games

I watched

Philippe Hödtke

js unconf 2016 speech yesterday “hacking games and why you should do it”. and I added some new techniques to my “cheat / hack” directory.

I encourage you to watch Philippe’s speech.

after watching philip at work i realized:

  • I didn’t use the search feature in the source part of the browser developer tools.

  • I have over-relied on adding breakpoints and don’t need them.

  • I had not used


    from the console.

after looking i left and tried the cookie clicker game that philip demonstrates.

but i’m testing stuff, why should i care?


  • imagine that instead of having to use an automated tool to put the application in a certain state, or having to click manually to do a lot of work.

  • imagine you could just write a few lines of code in the browser console and automate there.

  • imagine you could write a single line of code that would run every second and “do things” like click a link, or close a dialog box, or .

I describe an example later in this article of an automated on-line “bot” that could play a game better than a human (me).

back to the cookie selector

and I did very well.

i used a slightly different approach than philip used and found that


loops worked better for me than


when handling while exploring, in this game.

go beyond the for loop

I went to look for games where


suited me better.

I had a few false starts with other games that looked promising but either:

  • if I had such an obscured code, I couldn’t move forward.

  • contained game crash bugs.

I tried a few typing games, but encountered the “How to trigger keyboard events” stumbling block.

automate ztype from the console

and then i went back to this old favorite zty.pe.

I have already played zty.pe. I really like it.

  • I periodically continue to play “hit the dead” on the dreamcast. Yeah. i have a dreamcast with a keyboard.

  • I periodically still play “r-type” – mostly from “r-types” on playstation one (yes, I still have a ps1).

given the above two. i really like ztype even though i’m not very good at it.

if you can’t trigger keyboard events how could you play ztype?

yes I know what you are thinking:

“but if I run into the stumbling block” how can I trigger keyboard events “, how can I automate zty.pe, which is only for keyboard events.”

Well, zty.pe is pretty well designed. it’s almost as if it was designed to be tested and automated.

  • rather than adding the “shoot” functionality in the keyboard event hook, so the only way to trigger it is to emit a keyboard event.

  • the keyboard event calls a method, so the method can be called outside of the keyboard event

if this was an application that you were testing and automating, it means that you can automate most of the application by calling the “shoot” method. and leave the risk of “keyboard events triggering on this platform” to a much smaller testing scope with a different testing approach.

human vs. bot

I only managed to reach level 15, but my automated ‘bot’ reached level 93. You can see the results of my automation here on youtube.


I’m not going to explain exactly what I did as it would spoil the fun. and the video doesn’t show you how either.

tricks to hack javascript games

but i will give some more tips, some you might see in action in philip’s video:

  • enough to print the source.

  • use ‘find’ to search for the classes and variables you find in the source.

  • if you type something into the console and it returns with ‘function’, you need to find where the class is instantiated in the code.

  • use ‘find’ to search for ‘new’ instantiations of classes.

  • you can do a lot of exploration and manipulation with ‘for’ loops.

  • for bots you will need to use


  • I attribute the result of


    to a bot, for example

    ztypebot = setinterval(...)

    so I can stop the bot later with a


  • find some “quick hacks” that you can use at the start of your investigation to give yourself more room to find a better hack, for example when automating ztype I started with a bot that had infinite times and Fired an emp every 2 seconds, and when it was working it gave me time to experiment with the objects and the source to figure out how to create a bot that can shoot properly.

  • take notes as you go along what you tried and how you found the items.

  • Sometimes i start by going through the code and looking for tips like philip shows.

  • Sometimes I start by looking for what code is triggered by event listeners in browser developer tools.

  • sometimes i have a breakpoint code that i think is interesting.

  • their key (and philip demonstrates this) is to find large namespace objects as early as possible.

and make sure you don’t submit high scores after you “cheat” or “hack” in this way, it’s very annoying for others. a proxy tool can help you to avoid accidentally sending high score by blocking all high score submissions eg fiddler auto responder works well for this.

summary of tests

when you hack / cheat javascript games in this way, you develop skills which will carry over to your tests:

  • understand javascript

  • learn how to use browser development tools

  • interact with a running application

  • explore the internal object state of an application

  • put a running application in a specific state to test automatically

  • write very small amounts of code to automate an application state

if you find any good games to play with let me know.

Source link

Leave A Reply

Your email address will not be published.