Asserting page load times in Magento with Magium

Validating SLAs (Service Level Agreements) is often part of executing browser tests. With Magium you can now include page timings as part of your test.

Say, for example, that part of your SLA is that a page must be rendered in less than 5 seconds. This is done by using the TTFBLoadedLessThan, DOMPageLoadedLessThan or DOMContentLoadedLessThan assertion, depending on what you are actually intending to measure.

Note: these assertions require a browser that supports the PerformanceTiming interface. The mostly newer versions of Chrome and Firefox do.

TTFBLoadedLessThan

This asssertion measures the time to first byte.

use Magium\AbstractTestCase;
use Magium\Assertions\Browser\TTFBLoadedLessThan;

class DOMTiming extends AbstractTestCase
{
    public function testTTFBInLessThanFiveSecondsPasses()
    {
        $this->commandOpen('http://www.magiumlib.com/');
        $assertion = $this->getAssertion(TTFBLoadedLessThan::ASSERTION);
        /* @var $assertion TTFBLoadedLessThan */
        $assertion->setMaxElapsedMilliseconds(5000);
        $assertion->assert();
    }
}

DOMContentLoadedLessThan

This assertion checks to ensure that the page has all of its scripts executed. Images and CSS may not have been fully loaded and/or processed yet. Because JavaScript blocks rendering, this is the first time that the page will be completely visible.


use Magium\AbstractTestCase; use Magium\Assertions\Browser\DOMContentLoadedLessThan; class DOMTiming extends AbstractTestCase { public function testDOMContentLoadedLessThanInLessThanFiveSecondsPasses() { $this->commandOpen('http://www.magiumlib.com/'); $assertion = $this->getAssertion(DOMContentLoadedLessThan::ASSERTION); /* @var $assertion DOMContentLoadedLessThan */ $assertion->setMaxElapsedMilliseconds(5000); $assertion->assert(); } }

DOMPageLoadedLessThan

This assertion validates the elapsed time before the page is completely loaded.

use Magium\AbstractTestCase;
use Magium\Assertions\Browser\DOMPageLoadedLessThan;

class DOMTiming extends AbstractTestCase
{

    public function testPageLoadedInLessThanFiveSecondsPasses()
    {
        $this->commandOpen('http://www.magiumlib.com/');
        $assertion = $this->getAssertion(DOMPageLoadedLessThan::ASSERTION);
        /* @var $assertion DOMPageLoadedLessThan */
        $assertion->setMaxElapsedMilliseconds(5000);
        $assertion->assert();
    }