Testing Magento with Behat/Mink

It’s no secret for any Magento developer that properly testing extensions and custom code is not an easy chore, since Magento doesn’t really provide any solutions or integrated test for even the core. However, there are a couple of extensions and posts covering unit testing setup and usage as well as one extension in particular, that has won traction recently is MageTest by Alistair Stead.

MageTest basically patches the core and allows automated functional testing of your Magento installation; while MageTest is very handy and I intent to cover its usage on a later date, this post will take a look at the least known module simply called Magento-Mink.Magento-Mink by Johan Reinke, allows for developers to write a test for any page sensitive operation of a Magento site.

Mink is simply a library that can virtualize a web browser in PHP. It works with drivers (Goutte, Zombie, Selenium, …) and can automate testing of the interface of your site.

I found that Magento-Mink is extremely easy to use and is especially useful for testing front-end customization as well as new functionality.

Installation and Setup

Thanks to Modman installation, it’s just one step:

# In your Magento Root dir
modman init
modman clone git@github.com:amacgregor/magento-mink.git

That’s all that we need to do for installing Magento-Mink, if you are not familiar with Modman, you can read more about it on their wiki page.

Our First Test

Let’s start creating our first test, under:

app/code/local/Demac/Testmod/Test/Mink/Catalog.php

section('TEST ADD PRODUCT TO CART');
        $this->setCurrentStore('default');
        $this->setDriver('goutte');
        $this->context();
 
        // Go to home
        $this->output($this->bold('Go to home'));
        $url = Mage::getStoreConfig('web/unsecure/base_url');
        $this->visit($url);
        $category = $this->find('css', '#nav .nav-1-1 a');
        if (!$category) {
            return false;
        }
 
        // Go to product list
        $this->output($this->bold('Go to product list'));
        $this->visit($category->getAttribute('href'));
        $product = $this->find('css', '.category-products li.first a');
        if (!$product) {
            return false;
        }
 
        // Go to product view
        $this->output($this->bold('Go to product view'));
        $this->visit($product->getAttribute('href'));
        $form = $this->find('css', '#product_addtocart_form');
        if ($form) {
            $addToCartUrl = $form->getAttribute('action');
            $this->visit($addToCartUrl);
            $this->attempt(
                $this->find('css', '#shopping-cart-table'),
                'Product added to cart successfully',
                'Error adding product to cart'
            );
        }
    }
}

The previous test will do following actions:

  • Go to the Homepage
  • Click on the first category link from the top navigation
  • Find the first product and load the product page
  • Add products to the cart

Running the Test

We can run our test from either the command line:

php shell/mink.php

or from the browser:

http://localhost/shell/mink.php

Going Forward

Although we only covered the very basics of using Magento-Mink, this all that is needed to start adding tests to your modules under the Test folder. In the next post we will see how we can use MageTest in conjunction with Magento-Mink to extend our testing capabilities and increase our QA.