14

I have two php envs and I am currently able to run something like this for different urls

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/'

Currently I run them using commands codecept run --env alpha , or codecept run --env beta

Is there a way to provide the url from commandline while running the codeception tests , something like codecept run site=alpha.test.com and then grabbing it from inside the config instead of hardcoding urls ?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Supra
  • 1,612
  • 1
  • 18
  • 36
  • What is the reason to not hardcode URLs? I see some cases when changing the URLs dynamically can be helpful, e.g. different environments in the same server, in that case you can use a separate configuration files placed in the directory specified by envs option in paths configuration, see: http://codeception.com/docs/07-AdvancedUsage#Environments. What is your case? – Mauricio Sánchez Jul 01 '15 at 13:42
  • 4
    we have different dev environments for each developer, and every time I add a new dev and env, I dont want to change the config file – Supra Jul 01 '15 at 14:55
  • @Supra did you ever come across a good solution for this? I have a similar situation where our build pipeline produces an artifact like `my-application-4.26.phar` which would need to be passed to codecept as an argument. – Josh Johnson Apr 17 '17 at 17:11
  • 1
    @JoshJohnson Not yet unfortunately. Try setting it somewhere in the env variable, not sure if it would work though. worth a try. – Supra Apr 20 '17 at 08:25
  • 1
    @Supra ended up using envs (like you did) with a custom loader that we can pass `commandLocation: 'php my-application-1.*.phar'` – Josh Johnson Apr 25 '17 at 18:16

6 Answers6

3

i had the same Problem and did extend Codeception to support a dynamic Server-Url.

I can call my Codeceptions-Test by php in addition by the following code:

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');

Here is the extension i did in 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;
    }

}

In your case you need some additional programming to get all cli option into codecpetion (//see some other options).

OR

You can extend the Codecption cli interface to instantiate the Wrapper/Codecept and not the original Codecept.

Hope this helps a bit and gives you an idea how to fix your issue.

Gizzmo
  • 691
  • 8
  • 21
3

Codeception documentation says configuration can be merged.

codecept run --env alpha,beta

You can create custom config file with dynamic domain as you need. And override it with --env parameter.

E.g. file beta.yml is created dynamically with this content:

modules:
    config:
        WebDriver:
            url: http://dynamic-domain-1.mysite.com
ZZromanZZ
  • 183
  • 3
  • 13
2

In the bootstrap file it is possible to access and more importantly modify the loaded configuration by accessing static::$config. The bootstrap file is processed as one of the last steps when loading the configuration file.

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

Where $url has been determined by other means e.g. environment variable, or it could come from the global argv if you are prepared to parse the command line data yourself.

Richard Cross
  • 396
  • 3
  • 12
2

If you want to set webdriver's url within php, you can add the following line to your tests/_bootstrap.php file and remove the url param from your tests/acceptance.suite.yml file.

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

Here is an example, how to set the url from a laravel .env file.

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')]]]]]);
rcx1
  • 936
  • 7
  • 6
1

I am doing this:

I have few --env cofiguration, for example:

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

and if I need to test a different stage from the programmer, I use this command before I run test:

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

I run test from Jenkins and I have saved tests in GitHub.

Frits
  • 7,341
  • 10
  • 42
  • 60
rebas
  • 11
  • 1
0

Referring to this question, you could put the url into a .env file or the environment. Then, you declare that you're using them using the params tag so they can be referenced elsewhere.

params:
    - .env
modules:
    enabled:
        - WebDriver
        - AcceptanceHelper
    config:
        WebDriver:
            url: '%URL%' # declared in the .env file
            browser: 'phantomjs'

Before you use this, it is worth noting that you will probably have to do some futzing to get it to work, but this should be a valid alternative to the other answers.

gfaster
  • 157
  • 2
  • 12