Announcing PHPBrowserMobProxy

In what will hopefully be a series of anncouments / releases this week I am releasing the first public release of PHPBrowserMobProxy which is, unsurprisingly, a PHP wrapper around the BrowserMob Proxy. Having a scriptable proxy is important to a full-scale WebDriver automation effort. Why? WebDriver is not going to grow the ability to manipulate or interrogate the low-level HTTP traffic.

To install it (via PEAR)

$ sudo pear channel-discover element-34.github.com/pear
$ sudo pear install -f element-34/PHPBrowserMobProxy

This should also install the Requests package. If you don’t see it, then you will also want to do

$ sudo pear install -f element-34/Requests

(Yes, I am hosting a PEAR package of Requests, but claim to credit for its awesomeness over cURL.)

Unlike some proxies (Squid for instance) you do not connect directly to the advertised port of the proxy. Rather, you connect to that port and it tells you a new port that you are going to actual proxy with. This hurts the brain for a bit before you realize that it is far easier to keep track of traffic on a port-by-port, session-by-session basis rather than interleaving things.

Here is a succinct PHPUnit example of how to use it.

require_once 'PHPWebDriver/WebDriver.php';
require_once 'PHPWebDriver/WebDriverProxy.php';
require_once 'PHPBrowserMobProxy/Client.php';
 
class ProxyTest extends PHPUnit_Framework_TestCase {
  protected static $driver;
  protected static $client;
 
  public function setUp() {
      self::$driver = new PHPWebDriver_WebDriver();
      self::$client = new PHPBrowserMobProxy_Client("localhost:9090");
  }
 
  public function tearDown() {
      $this->session->close();
      self::$client->close();
  }
  public function testFirefox() {
      $additional_capabilities = array();
      $proxy = new PHPWebDriver_WebDriverProxy();
      $proxy->httpProxy = self::$client->url;
      $proxy->add_to_capabilities($additional_capabilities);
      $this->session = self::$driver->session('firefox', $additional_capabilities);
      $this->session->open("http://github.com/adamgoucher");
  }
}

Once the proxy is established, you can do all sorts of interesting things like blacklist all the 3rd party crap that gets injected into a page that gets in the way of functional automation. You can also request a HAR file of the load profile of a page which is useful for doing things like failing a page load on the presence of 404s, etc. To see everything you can look right at the class on Github.

Any issues, etc. should also go on Github.

Post a Comment

Your email is never published nor shared. Required fields are marked *