Firefox Browser Automation with mozrepl – A PHP Class

Browser automation can be an effective method where command line clients like cURL will fail. There are a number of better known browser automation tools like Selenium and AutoIt that can achieve a lot in themselves. The prevailing use for browser automation is to imitate a real-world user and should only be used where necessary, as it’s a lot more efficient to use a command line client which uses far less computer resources.

A lesser known plugin for Firefox called mozrepl is an excellent browser automation tool which gives complete control of your browser from top to bottom. Think of everything your browser does, its extensions and more…

mozrepl gets its name from the generic definition of REPL, and I assume the “moz” refers to Mozilla (Firefox). mozrepl is a firefox-plugin that allows you to telnet into your browser, whether it is local or on a remote machine. You communicate with mozrepl via telnet, giving it Javascript commands which it’ll process synchronously and return the resulting data back to you, if any.

I could go on and on about the various ways you can utilise it, instead I’ll post some real-world examples and code that can inspire you. Meanwhile, the remainder of this article will describe how to get mozrepl running, as well as using a generic PHP class that’ll communicate with mozrepl via telnet.

How to Install Mozrepl

Install the add-on
– Restart your browser if required
– Go to Tools > Mozrepl and ensure Activate on Startup is checked. Also click Start if mozrepl has not already started
– There is also an option to Allow outside connections should you wish to control the browser over a network. For now, testing on your local machine should be fine.

A basic PHP class to use mozrepl via telnet

Basic Example

After including the above, you can test it is working by using some sample Javascript.

Run this from the command line. You can run it in your browser if you like but it makes more sense to run it from the command line.

I’d recommend spending an hour reading through the mozrepl guide to get an idea of the general scope of the add-on. Keep checking back to innvo.com for some real-world examples.