【问题标题】:Need to use codeception parameters in test code需要在测试代码中使用codeception参数
【发布时间】:2014-04-11 03:24:40
【问题描述】:

我的acceptance.suite.yml 配置文件中有环境配置。 参数之一是语言。我需要知道实际测试代码中的这个参数值才能正确驱动测试步骤。

acceptance.suite.yml 配置内容:

 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

如何获取语言参数值?

【问题讨论】:

    标签: symfony selenium codeception


    【解决方案1】:

    使用 Scenario,您将获得您的活动“环境”,当您获得它时,阅读配置很容易。下面的例子。您可以访问 Cept 和 Cest 格式的 \Codeception\Scenario。在 Cept 中,$scenario 变量默认是可用的,而在 Cest 中您应该通过依赖注入来检索它。

    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'];
    
    }
    

    【讨论】:

      【解决方案2】:

      无法直接从测试文件中访问配置值。

      但是它可以通过帮助文件访问(确认与 Acceptance.php 一起使用)。

      在 Helper 文件中添加了以下内容:

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

      在测试文件中通过以下方式访问它:

      $I->getConfigUrl();
      

      请注意,在我的 *.suite.yml 文件中,我有以下配置:

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

      关于我的帖子的更多详细信息:http://phptest.club/t/how-to-grab-module-config-values/1616/2

      【讨论】:

        【解决方案3】:

        @George 的回答会给你所有的设置,但不是选择的环境。

        我很难使用getopt 来获取当前环境,但发现它可以很好地满足我的需求。我得到了baseUrl 值,因此包含了经过测试的代码,而不是根据问题将其编辑为“lang”,但未对其进行测试。这应该只是改变挑选出值的数组的问题。

        // 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'];
        }
        

        【讨论】:

          【解决方案4】:

          我遇到了同样的问题,并在 Codeception 论坛上找到了帮助。以下是访问配置内容的方法as mentioned by user Dan

          $config = \Codeception\Configuration::config();
          $apiSettings = \Codeception\Configuration::suiteSettings('api', $config);
          

          【讨论】:

            【解决方案5】:

            我找到了一个完美的解决方案。 我把它放到 _Bootstrap.php 文件中:

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

            【讨论】:

              猜你喜欢
              • 2017-07-06
              • 2013-09-07
              • 1970-01-01
              • 1970-01-01
              • 2015-03-18
              • 1970-01-01
              • 2014-02-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多