6

I have environment configuration in my acceptance.suite.yml config file. One of the parameters is language. I need to know this parameter value in the actual test code to be able to drive test steps correctly.

acceptance.suite.yml config content:

 class_name: WebGuy
modules:
    enabled:
        - WebDriver
        - WebHelper
        - Db
    config:
        WebDriver:
            browser: firefox
env:
    eng:
        modules:
            config:
                WebDriver:
                   url: 'localhost'
                   lang: en
   esp:
        modules:
            config:
                WebDriver:
                    url: 'localhost'
                    lang: es

How can I get the language parameter value?

5 Answers 5

11

I had the same problem and found help on Codeception forums. Here's how you can access config content as mentioned by user Dan.

$config = \Codeception\Configuration::config();
$apiSettings = \Codeception\Configuration::suiteSettings('api', $config);
Sign up to request clarification or add additional context in comments.

Comments

3

With Scenario you will get your active "env", when you get it, reading config is easy. Example below. You can access \Codeception\Scenario in the Cept and Cest formats. In Cept, the $scenario variable is available by default, while in Cest you should retrieve it through dependency injection.

public function someTest(AcceptanceTester $I, \Codeception\Scenario $scenario) {

    $current_env = $scenario->current('env');
    $config = \Codeception\Configuration::suiteSettings("acceptance", \Codeception\Configuration::config());

    $current_language = $config['env'][$current_env]['modules']['config']['WebDriver']['lang'];

}

Comments

2

The answer by @George will give you all the settings, but not the selected environment.

I struggled to get the current environment using getopt, but found this to work well enough for my needs. I was getting the baseUrl value so have included the tested code for that instead of editing it for "lang" as per the question but not testing it. It should just be a matter of changing the arrays which pick out the value.

// Default to no env
$env = '';
// If we have argv settings, go through each one
if (isset($_SERVER['argv'])) {
    foreach($_SERVER['argv'] as $key => $value) {
        // If the current value is --env and we have the next one, then take the next one as the setting
        if ($value == '--env' && isset($_SERVER['argv'][$key + 1])) {
            $env = $_SERVER['argv'][$key + 1];
        }
    }
}
// By this point we either found an --env and have its value, or we
// didn't and can assume we don't have one set.
// We look in a slightly difference place depending on whether we
// have an env or not
if ( empty( $env ) ) {
    $baseUrl = $apiSettings['modules']['config']['baseUrl'];
} else {
    $baseUrl = $apiSettings['env'][$env]['modules']['config']['baseUrl'];
}

Comments

0

I found a solution which works perfectly. I put this into _Bootstrap.php file:

# Checking which language parameter is provided
$lang = $_SERVER['argv'];
$language = $lang[4];

Comments

0

Was not able to access the config value from within the test file directly.

However it was accessible via a helper file (confirmed working with Acceptance.php).

Within the Helper file this was added:

public function getConfigUrl(){
  return $this->getModule('WebDriver')->_getConfig('url');
}

Within the test file it was accessed via:

$I->getConfigUrl();

Note that within my *.suite.yml file I had the follow configuration:

paths:
    helpers: tests/_support
modules:
    enabled:
        - \Helper\Acceptance
        - WebDriver

More details on my post here: http://phptest.club/t/how-to-grab-module-config-values/1616/2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.