0

I'm writing a Gherkin-based acceptance testing PoC. I have a feature file, step object, and a page object. In my sequence I will need to log-in the test user before conducting the rest of the series. Our SUT is a legacy PHP application that didn't use any framework.

I would like to store the testuser's credentials in a params.yml or other external config file but have been unsuccessful in making this work and unable to find a complete example.

My login object is a simple Cest class for now. I didn't think it needed its own feature description, the rest of the tests will be Gherkin based where needed. My config files are currently the default configs generated by Codeception 5's bootstrap command with a gherkin section added for the one feature file I've written so far. Eventually I will run this under WebDriver to enable sessions...for now I'm just trying to establish a reusable environment we can build on for a team of developers.

The Codeception docs seem to gloss over some of these concepts or recommendations for users new to their framework.

I sincerely appreciate any ideas or concerns you may have.

<?php

namespace Tests\Acceptance;

use Codeception\Attribute\Group;
use Tests\Support\AcceptanceTester;
use Tests\Support\Page\Acceptance\LoginPage;

class LoginCest
{

    #[Group('login')]
    public function successfulLogin(AcceptanceTester $I, LoginPage $loginPage)
    {
        $loginPage->login( <testUserHere>, <goodPasshere> ); // <-this is what I want to provide
        $I->dontSeeElement('.alert-error');
        $I->amOnPage("/command.php");
    }
    public function unsuccessfulLogin(AcceptanceTester $I, LoginPage $loginPage)
    {
        $loginPage->login(getenv( <testUserHere> , 'baddpass');
        $I->seeElement('.alert-error');
        $I->amOnPage("/");
    }
}

1 Answers1

0

I ended up writing a config helper. It doesn't seem like the right solution but it worked so I could move forward. This would be a good topic for their docs to cover under re-use.

<?php
declare(strict_types=1);

namespace Tests\Support\Helper;

class Config extends \Codeception\Module
{
    protected array $requiredFields = ['testUser', 'goodPass'];
}

Then this in my suite config:

modules:
  enabled:
    - \Tests\Support\Helper\Config:
      testUser: testuser
      goodPass: supersecretpassword

I was then able to inject it into my Cest object methods.

I also injected it into my StepObject class which the Gherkin feature excutes:

    protected Config $cfg; 
    protected function _inject(Config $c)
    {
        $this->cfg = $c;
    }
    #[When('I enter the password')]
    public function iEnterThePassword()
    {
        $I = $this;
        $I->fillField('#password',$this->cfg->_getConfig('goodPass'));
    }

I'd love to know if there is a better solution.