Search code examples
phpphpunitcodeception

Codeception test cases precondition


I am writing automation tests using codeception framework. I have a test cases which are verifying some functionality after user is logged in. There are around 20 test cases with different functionality. All test cases need an user to be logged in to the system, so I have written a login functionality under _before callback. When I execute all the test cases, before every test cases login functionality is checked which takes a lot of time. Can we write login functionality as precondition and once user is logged in it should execute all test cases?


Solution

  • You can use what are known as Helpers in codeception. You can generate a helper using the following command:

    vendor/bin/codecept generate:helper Login

    Then you could put a method into the class for logging in a user like so:

    <?php
    namespace Helper;
    
    // here you can define custom actions
    // all public methods declared in helper class will be available in $I
    
    class Login extends \Codeception\Module
    {
        public function login($username, $password)
        {
            /** @var \Codeception\Module\WebDriver $webDriver */
            $webDriver = $this->getModule('WebDriver');
            // if snapshot exists - skipping login
            if ($webDriver->loadSessionSnapshot('login')) {
                return;
            }
            // logging in
            $webDriver->amOnPage('/login');
            $webDriver->submitForm('#loginForm', [
                'login' => $username,
                'password' => $password
            ]);
            $webDriver->see($username, '.navbar');
            // saving snapshot
            $webDriver->saveSessionSnapshot('login');
        }
    }
    

    See http://codeception.com/docs/06-ReusingTestCode#session-snapshot for more info on snapshots.

    Your acceptance.suite.yml should look something like this:

    # Codeception Test Suite Configuration
    #
    # Suite for acceptance tests.
    # Perform tests in browser using the WebDriver or PhpBrowser.
    # If you need both WebDriver and PHPBrowser tests - create a separate suite.
    
    class_name: AcceptanceTester
    modules:
        enabled:
            # Note we must use WebDriver for us to use session snapshots
            - WebDriver:
                url: http://localhost/myapp
                browser: chrome
    
            - \Helper\Acceptance
    
            # Note that we must add the Login Helper class we generated here
            - \Helper\Login
    

    Now we have a helper class that can be reused in all our tests. Let's look at an example:

    <?php
    
    
    class UserCest
    {
        // tests
        public function testUserCanLogin(AcceptanceTester $I)
        {
            $I->login('username', 'password');
        }
    
        public function testUserCanCarryOutTask(AcceptanceTester $I)
        {
            $I->login('username', 'password');
            $I->amOnPage('/task-page');
            $I->see('Task Page');
            // other assertions below
        }
    
        public function testUserCanCarryOutAnotherTask(AcceptanceTester $I)
        {
            $I->login('username', 'password');
            $I->amOnPage('/another-task-page');
            $I->see('Another Task Page');
            // other assertions below
        }
    }
    

    Now when running the UserCest test, it should only login the user once.