Yes, crap is a technical term!

Functional automation takes long enough what with all the opening of browsers, fetching of content, etc. to have to wait for all the 3rd party crap that exists in the page that does not directly affect the functionality of your website. Ideally each of those things would have a feature switch that you could throw in the automation environments and they wouldn’t get injected at all, but let’s face it, we live in the real world not ideal one.

When faced with this situation what you want to do is just block things at some sort of scriptable proxy (like the BrowserMob Proxy). As of about an hour ago, SaunterPHP grew this ability in version 1.0.2.

Here is the blacklisting illustrated with EBay to block Facebook (which has nothing to do with EBay and just slows down the page load).

<pre lang="php">namespace WebDriver;

require_once 'tailored/EBayTestCase.php';
require_once 'pages/ShirtPage.php';

class BlacklistTest extends EBayTestCase {
    /**
    * @test
    * @group shallow
    * @group ebay
    * @group blacklist
    */
    public function collar_style() {
        $this->client->blacklist("http://www\\.facebook\\.com/.*", 200);
        $this->client->blacklist("http://static\\.ak\\.fbcdn\\.com/.*", 200);
        $sp = new ShirtPage($this->session);
        $sp->go_to_mens_dress_shirts();
        $sp->change_collar_style("Banded (Collarless)");
        $this->assertTrue($sp->is_collar_selected("Banded (Collarless)"));
        sleep(15);
    }

}
  • $this->client is the PHPBrowserMobClient client
  • blacklist takes two parameters; a regex of the site you are blacklisting, and the http response code to feed back to the browser (I generally keep this at 200)

In order to enable this you need two new lines in your saunter.inc

<pre lang="php">$GLOBALS['settings']['proxy'] = "localhost:9090";  
$GLOBALS['settings']['proxy.browsermob'] = true;

As is the situation with the Selenium server, SaunterPHP does not control the BrowserMob Proxy. It is up to you using something like Puppet to make sure it is installed where you expect it to and remains running.