Skip to main content
blog title image

7 minute read - Test Automation JavaScript

Practice Your Software Testing

Feb 7, 2019

One way I practice my Software Testing, improve my JavaScript programming and practice my automating is by ‘hacking’ JavaScript games.

One of my bots scored 282010 on https://phoboslab.org/xtype/ This ‘bot’ is JavaScript code that runs from the Browser Dev Tools and plays the game.

Image of high score bot achieved

I have a video showing the bot in action below.

To create this I have to learn to use the dev tool to inspect the Dom, and the running memory space, and read the JavaScript. All of this is modelling the application. A recon step that helps me with my Software Testing.

As I recon the app I look at the network tab to see what files are loaded and what API calls are issued. This also informs my model. And makes me think about Injection and Manipulation points.

Perhaps I can use a proxy to trap and amend those requests? Perhaps my proxy can respond with a different file or data automatically?

These are thought process and skills that I can apply in my testing. I also learn more about HTTP, Dev tools and Proxies.

When the game is running I have to observe its execution behaviour. I build a model of the software and its functionality. This informs my Software Testing. I have to build models of the application as I test and make decisions about what to target.

To ‘hack’ the game, I have to inspect the objects which have been instantiated by the code. I can do this in the console by typing in the object names.

Inspecting a game object in memory

To learn what these objects are I have to read the game code. This improves my JavaScript because one of the best ways to learn to write code is to read code and try to understand what it does.

I can use the Snippets view in Chrome Sources to write JavaScript. This is a built in mini JavaScript IDE in the browser.

Writing code in the browser

I can write simple code here to manipulate the game objects in memory to help me cheat and hack the game. I don’t need to learn a lot of JavaScript to do this, and most of the JavaScript you need is already in the game source itself.

To write a ‘bot’… code that executes in its own thread periodically to do something, I use the ‘setInterval’ command. This is the fundamental key to writing JavaScript bots. e.g.

var infiniteLivesBot = setInterval(infiniteLives,1000);

The above line calls a function named infiniteLives every second. That infiniteLives function checks if my number of lives have decreased, and if so, increase them. e.g.

function infiniteLives(){
    if(game.lives<3){
        game.lives=3;
    }
}

var infiniteLivesBot = setInterval(infiniteLives,1000);

I can easily stop this bot using the clearInterval command.

clearInterval(infiniteLivesBot);

I can run that code from the snippets view, or paste it into the console, or convert it into a bookmarklet. Whatever is more convenient to help me when I want to hack the game. I do the same thing to support my testing e.g. setup data, delete data etc.

This is a form of ’tactical’ automating. It won’t scale, it doesn’t go into continuous integration. It supports me. It does a specific task. It automates part of my work. It is a good start to learning to automate more strategically.

To automate xtype I had to learn how to trigger mouse events to initiate the firing functionality. I couldn’t remember how to do this. I copy and pasted a snippet of code from stackoverflow. All professional programmers do this.

var event = document.createEvent("MouseEvents");
event.initEvent("mousedown", true, true);
document.getElementById("canvas").dispatchEvent(event, true);

Part of learning programming is knowing where to find a general answer. Knowing which part of the answer to isolate. Then having the confidence to bring that into your code and experiment with it.

As I do this, I learn more JavaScript. Which allows me to write scripts in other tools e.g. Postman. And I can inject code into applications e.g. using WebDriver JavaScriptExecutor. The more I practice, the more options I open up.

I take the knowledge and then write small utilities or applications to help me. I write a lot of small JavaScript utilities to help me with data extract activities and reformatting of data from Web applications.

I extended my knowledge by writing JavaScript applications and games which I bundled into:

If you want to experiment with simple games manipulation and hacking then the games in this app are designed for that purpose.

Doing this also has a knock on effect on how I view web applications. The GUI is no longer a trusted client. The GUI can be manipulated by the user. The messages it sends to the server can be manipulated by the user. The server needs to be robust.

This helps my Software Testing. I have to go deeper when I test. And by practicing I develop the skills to go deeper when I test. I can recognise when requirements need to be tested more deeply than they state on the surface.

This helps my programming. I am very aware that the server needs to validate and approach the input data with caution.

This is one way that I practice my Software Testing, JavaScript programming and automating. The benefit to me has been enormous. I recommend this, or adapt the ideas and create your own practice path.

I cover many of the skills to do this in my online Technical Web Testing 101 course

Free Video Discussing This Topic

Some Notes on Bot Creation

This was a hard bot to write Xtype is a bullet hell shooter which are challenging to play, let alone automate - and no, I’m not going to show you the code for the bot.

But this is not my first bot.

I’ve written bots to play z-type, Cookie Clicker, 3D Monster Maze

I like these games because they are all well written, I can read the code to improve my programming, and they are fun to play.

Many of my bots are not full playing bots like the x-type bot. They are support tools. And when I started to automate x-type I started with support tools:

  • an infinite lives hack
  • an auto-aimer so that I didn’t have to keep track of the boss while I was controlling the player
  • I added auto firing to the auto-aiming so I only had to concentrate on moving the player
  • I combined all three and had a bot that could play the game, although it was cheating with infinite lives.
  • only then did I approach the autonomous playing of the game

For 3D Monster Maze I only wrote support tools:

  • a heads up display so that a map of the screen was rendered on screen as I played showing me the position of Rex to help me avoid him, and showing me where the exit was
  • a trap for Rex, so that if he entered it he couldn’t move
  • a chain for Rex so that he never moved from his starting point
  • armour so that if Rex caught me, he couldn’t kill me

For many games I try to think how to add extra features to make the game more interesting. I don’t have the skill or patience to write games like these, but I do have the skill and motivation to ‘augment’ them for my learning.

This is analogous to writing ’tools’ to support our testing, or making applications more ’testable’.

Note: my games github.com/eviltester/TestingApp are not well written, but they are very easy to understand and hack.

Want to see my XType bot in action?

Where to learn more?

I have some material covering this topic:


If you found this useful then you might be interested in my Online Technical Web Testing 101 course.