Skip to main content

Magento - Functional Testing


We have checked on how to do unit testing in magento in previous post. That is not enough to test the magento website. If we can testing the magento home page, login page, etc. it would be great. Fortunately, we have a module that deals with this situation.

Download the module magento-mink and install in your magento setup. This module using the behat mink for testing the magento website.

We have to write the test in any of local modules, as the mink module uses only local folder. You can change shell/mink.php to add community modules also.

Now we will write the testing code for the magento website. In app/code/local/VendorName/ModuleName, create a folder Test/Mink. All the files inside, this folder will be searched for mink testing.
class VendorName_ModuleName_Mink_MyTest extends JR_Mink_Test_Mink {

    public function testBasicWebsite() {
        $this->section("Test Basic Website");
        $this->setCurrentStore('default');
        $this->setDriver('goutte');
        $this->context();

        $this->_goToHomePage();
        $this->_goToLoginPage();
    }

    protected function _goToHomePage() {
        $this->output($this->bold('Go To the Homepage'));
        $url = Mage::getStoreConfig('web/unsecure/base_url');
        $this->visit($url);
        $category = $this->find('css', '#nav .nav-1-1 a', true);
        if (!$category) {
            $this->abort("No Category found");
        }
        $this->output("Homepage loaded");
    }

    protected function _goToLoginPage() {
        $this->output($this->bold('Go To the login page'));
        $loginUrl = $this->find('css', 'ul.links li.last a', true);
        if (!$loginUrl) {
            $this->abort("No login page url found");
        }

        $this->visit($loginUrl->getAttribute('href'));

        $login = $this->find('css', '#email', true);
        $pwd = $this->find('css', '#pass', true);
        $submit = $this->find('css', '#send2', true);

        if ($login && $pwd && $submit) {
            $email = 'username@domain.com';
            $password = 'password';
            $this->output(sprintf("Trying to login using %s with password", $email));
            $login->setValue($email);
            $pwd->setValue($password);
            $submit->click();
            $error = $this->find('css', 'ul.messages .error-msg span', true);
            if ($error) {
                $this->abort($error->getText());
            }
            $this->output('Customer successfully logged in');
        } else {
            $this->abort("Not login page.");
        }
    }

}

In the above code, we are first loading the home page and checking if it contains the category list in the header menu. Then, we are getting the login url and trying to login using the email and password given.

Now, to run the code just run the shell command
#show the test in the command line
php shell/mink.php

#run the test in html format
php shell/mink.php -r html > test.html



Comments

Popular posts from this blog

Magento 2 - Create Attributes in Setup

Sometimes in our module we require to create product or category attributes automatically on module install. Also, we might require to add extra fields to quote, sales order, invoice, creditmemo tables. We will go through how to do that in magento 2. You can find the sample code in the git repo .