3

Is it possible to have Cest files that extend a parent class and make use of a common test "login" that other tests depend on using the @depends

So my Cest file looks similar to the one found in this post which explains how to login and re-use the cookie in another test - https://stackoverflow.com/a/25547268/682754

I've got this common class but the test in the child class doesn't run and outputs...This test depends on 'commonCest::login' to pass.

<?php

class commonCest {

    const COOKIE_NAME = 'PHPSESSID';

    protected $cookie;

    /**
     * Most tests will need to depend on this function as it logs the user in and stores the session cookie, use the
     * "@depends" phpdoc comment
     *
     * The cookie in then  re-used by calling the following in tests:
     *
     *     $I->setCookie(self::COOKIE_NAME, $this->cookie);
     *
     * @param \AcceptanceTester $I
     */
    public function login(AcceptanceTester $I) {
        $I->wantTo('Login');
        $I->amOnPage('/login');
        $I->fillField(array('name' => 'username'), 'aaaaaa');
        $I->fillField(array('name' => 'password'), 'abcdef');
        $I->click('.form-actions button');
        $I->seeElement('.username');
        $this->cookie = $I->grabCookie(self::COOKIE_NAME);
    }
}

<?php
use \AcceptanceTester;

class rolesCest extends commonCest 
{

    /**
     * @depends login (This doesn't work)
     * @depends commonCest:login (This doesn't work)
     * @depends commonCest::login (This doesn't work)
     */
    public function listUsers(AcceptanceTester $I)
    {
        // tests
    }

?>
Community
  • 1
  • 1
Carlton
  • 5,533
  • 4
  • 54
  • 73

1 Answers1

0

Simply don't have commonCest under your cest directory. Login will then be run for every case of a class extending commonCest in your cest directory as it exists in all of them. You however shouldn't use @depends for this. Rather login should be in your actor or a helper, and that should be called from _before in your parent class.

Or just use stepobjects https://codeception.com/docs/06-ReusingTestCode#stepobjects and call your needed functions from _before

Shardj
  • 1,800
  • 2
  • 17
  • 43