如何在命令行中使用Codeception在验收测试中使用动态URL

时间:2015-04-15 14:33:16

标签: php phantomjs codeception

我有两个php envs,我现在可以为不同的URL运行这样的东西

modules:
    enabled:
        - WebDriver
        - AcceptanceHelper
    config:
        WebDriver:
            url: 'http://localhost/'
            browser: 'phantomjs'
env:
    alpha:
         modules:
            config:
                WebDriver:
                    url: 'http://myalphasite/'
    beta:
         modules:
            config:
                WebDriver:
                    url: 'http://mybetasite/'

目前我使用命令运行它们 codecept run --env alpha codecept run --env beta

有没有办法在运行代码测试时从命令行提供url,比如codecept run site = alpha.test.com然后从配置内部抓取它而不是硬编码网址?

5 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,并且扩展了Codeception以支持动态的Server-Url。

我还可以通过以下代码调用我的Codeceptions-Test:

chdir('myPathTo: tests/codeception');
$codeception = new \Wrapper\Codecept([
    'steps' => true,
    'verbosity' => 1,
    // some other options (see Codeception docs/sources)
]);
$codeception->setBaseUrl('myServerUrl');
$codeception->run('myTestSuiteName');

这是我在Codeception中所做的扩展:

<?php

namespace Wrapper;

use Codeception\Codecept as CodeceptOriginal;

class Codecept extends CodeceptOriginal {

    private $baseUrl = null;

    public function runSuite($settings, $suite, $test = null) {
        if ($settings['modules']['enabled']) {
            foreach ($settings['modules']['enabled'] as $key => $module) {
                if (is_array($module) && $module['PhpBrowser']['url']) {
                    $module['PhpBrowser']['url'] = $this->getBaseUrl();
                    $settings['modules']['enabled'][$key] = $module;
                }
            }
        }
        return parent::runSuite($settings, $suite, $test = null);
    }

    public function getBaseUrl() {
        return $this->baseUrl;
    }

    public function setBaseUrl($baseUrl) {
        $this->baseUrl = $baseUrl;
        return $this;
    }

}

在您的情况下,您需要一些额外的编程才能将所有cli选项放入codecpetion(//请参阅其他一些选项)。

OR

您可以扩展Codecption cli接口以实例化Wrapper / Codecept而不是原始Codecept。

希望这会有所帮助,让您了解如何解决问题。

答案 1 :(得分:2)

在bootstrap文件中,可以通过访问static :: $ config来访问并更重要的是修改加载的配置。加载配置文件时,引导程序文件将作为最后一步处理。

self::$config['modules']['config']['WebDriver']['url'] = $url;

其中$ url已通过其他方式确定,例如环境变量,或者如果您准备自己解析命令行数据,它可能来自全局argv。

答案 2 :(得分:2)

Codeception文档说配置可以是merged

codecept run --env alpha,beta

您可以根据需要使用动态域创建自定义配置文件。 并使用--env参数覆盖它。

E.g。文件 beta.yml 是使用以下内容动态创建的:

modules:
    config:
        WebDriver:
            url: http://dynamic-domain-1.mysite.com

答案 3 :(得分:1)

如果您想在php中设置webdriver的网址,可以将以下行添加到tests/_bootstrap.php文件中,并从tests/acceptance.suite.yml文件中删除网址参数。

\Codeception\Configuration::append(['modules' => ['enabled' => [['WebDriver' => ['url' => 'http://YOUR-URL.COM']]]]]);

以下是一个示例,如何从laravel .env文件中设置网址。

require __DIR__ . '/../bootstrap/autoload.php';
$app = require __DIR__ . '/../bootstrap/app.php';
$app->instance('request', new \Illuminate\Http\Request);
$app->make('Illuminate\Contracts\Http\Kernel')->bootstrap();

\Codeception\Configuration::append(['modules' => ['enabled' => [['WebDriver' => ['url' => env('APP_URL')]]]]]);

答案 4 :(得分:1)

我这样做:

我的--env cofiguration很少,例如:

DevChrome.yml,DevPhantom.yml,DevFirefox.yml,NormalChrome.yml,NormalPhantom.yml,NormalFirefox.yml

如果我需要测试程序员的不同阶段,我在运行test之前使用此命令:

sed -i 's|https://old.domain.com/|https://new.domain.com/|g' tests/_envs/DevChrome.yml

我从Jenkins运行测试,我已经在GitHub中保存了测试。

相关问题