When I saw Google’s recent interactive Pacman doodle to celebrate the game’s 30th anniversary, my first thought was ‘Wouldn’t it be cool to automate playing Pacman using Selenium’. I know - I’m a geek!

Google's Pacman Doodle

Anyway, below is my quick proof of concept using Selenium 2 alpha 4 - the latest version of Selenium 2 is available for download here.

package tests;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverScratchPad {

	public static final long PAUSE = 2000;

	public static final Keys UP = Keys.ARROW_UP;
	public static final Keys DOWN = Keys.ARROW_DOWN;
	public static final Keys LEFT = Keys.ARROW_LEFT;
	public static final Keys RIGHT = Keys.ARROW_RIGHT;

	public static WebElement controls;

	public static void main(String [ ] args) throws InterruptedException {
		WebDriver driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		driver.get("http://www.google.com/pacman/");
		controls = driver.findElement(By.id("pcm-c"));

		move(LEFT, UP, LEFT, DOWN, LEFT, UP, LEFT, DOWN, RIGHT);
	}

	private static void move(Keys... directions) {
		for (Keys direction : directions) {
			controls.sendKeys(direction);
			try {
				Thread.sleep(PAUSE);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

Try it out for yourselves, and I extend these challenges to you:

  • Specify a distance for Pacman to travel rather than using an arbitrary pause
  • Detect if Pacman dies in his quest to eat all the pills
  • Work out the score at the end of the game

All of the above should be achievable… let me know how you get on. Also let me know what high scores you achieve playing Pacman with Selenium 2!