Acceptance Testing VII -Testing the Manager

Using Codeception to Test the MODX Manager


In the previous article, we looked at XPath locators. In this one, we'll get ready to create a more complex test of the MODX Manager.


MODX logo

This article assumes that you've installed and configured Composer, Codeception, PhpUnit, MODX, Java, WebDriver, and ChromeDriver as described in earlier articles, and that you've created the acceptance test support files from those articles.


The Challenge

Ultimately, our automated test will perform the following tests in the MODX Manager:

  1. Log in
  2. Create users in the manager
  3. Verify that the user's data in the database
  4. Delete the users
  5. Confirm the deletion

In this article, we'll start getting things ready for our test.


_Bootstrap.php

We're going to need the $modx object for this test. If you've been following along in this series, you've already created a _build/tests/_bootstrap.php file containing this code (with the paths corrected for your platform):

<?php
use Codeception\Util\Fixtures;

require_once 'c:/xampp/htdocs/addons/vendor/autoload.php';
require_once 'c:/xampp/htdocs/addons/vendor/phpunit/phpunit/src/Framework/Assert/Functions.php';

require_once 'c:/xampp/htdocs/test/core/model/modx/modx.class.php';
echo "Getting MODX";
$modx = new modX();
$modx->getRequest();
$modx->getService('error', 'error.modError', '', '');
$modx->initialize('mgr');
Fixtures::add('modx', $modx);

The code above instantiates MODX and places it in the Fixtures array so we can use it in our test.

At this point, we're going to move the MODX fixture code from the _build/tests/_bootstrap.php file into a new file.

Create a new _bootstrap.php file in the _build/tests/acceptance directory. Copy the code above to the new _bootstrap.php file in the _build/tests/acceptance directory. Then (in the new file) remove the two include lines — we'll leave them in the original file.

Now, go back to the original _build/tests/_bootstrap.php file and remove (or comment out) everything but the two includes. They could be removed too, but since we used require_once in the acceptance suite's code, it won't hurt to have them there in case they're needed in other suites.

Check the acceptance suite .yml file (_build/tests/acceptance.suite.yml) and make sure it has these two lines near the top:

actor: AcceptanceTester

bootstrap: _bootstrap.php

You should already have a version of the new _bootstrap.php file in the tests/integration/ directory. If not copy it there so the integration tests will still run.


Why the New Bootstrap File?

A while later in the series, we're going to look at a technique for using multiple bootstrap files and selecting the appropriate one for a test at run time. This will allow us to modify the environment of the test so that, for example, the same test can be used in MODX 2, MODX 3 and potentially, future versions of MODX. Since the modifications will only apply to acceptance tests, we need the acceptance suite to have its own bootstrap files.


Login PageObject

We're also going to need the Login page object we created two articles ago. That file is _build/tests/_support/Page/Acceptance/Login.php, and contains this code:

<?php
namespace Page\Acceptance;

class Login
{
    // include url of current page
    public static $managerUrl = 'manager/';
    public static $usernameField = '#modx-login-username';
    public static $passwordField = '#modx-login-password';
    public static $username = 'JoeTester';
    public static $password = 'TesterPassword';
    public static $loginButton = '#modx-login-btn';
    public static $userMenu = '#modx-user-menu';
    public static $logoutLink = "//a[contains(@href,'?a=security/logout')]";
    public static $yesButton = "//button[contains(text(), 'Yes')]";

     /**
     * @var $tester \AcceptanceTester;
     */
    protected $tester;

    public function __construct(\AcceptanceTester $I)
    {
        /* Set $this->tester to passed-in $I so it's
            available in other methods */
        $this->tester = $I;
    }

    public function login($username = '', $password = '') {
        /** @var \AcceptanceTester $I */
        $I = $this->tester;
        $username = empty($username) ? self::$username : $username;
        $password = empty($password) ? self::$password : $password;

        $I->amOnPage(self::$managerUrl);
        $I->fillField(self::$usernameField, $username);
        $I->fillField(self::$passwordField, $password);
        $I->click(self::$loginButton);
        return $this;
    }

    /** @throws \Exception */
    public function logout() {
        /** @var \AcceptanceTester $I */
        $I = $this->tester;
        $I->moveMouseOver(self::$userMenu);
        $I->waitForElementVisible(self::$logoutLink, 3);
        $I->click(self::$logoutLink);
        $I->wait(1);
        $I->click(self::$yesButton);
    }
}

If you don't already have it, create the _build/tests/_support/Page/Acceptance/Login.php file and paste in the code above (also available at GitHub here).

Finally, run codecept build while in the _build directory in case Codeception needs to update any of the actor files.


Coming Up

In the next article, we'll create two more page objects, one for the Manager dashboard, and one for the Create/Edit User panel.




For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)