Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ability to use custom params from console in tests #3978

Closed
wants to merge 2 commits into from

Conversation

yuriy-chabaniuk
Copy link

It is small test plan for this.

  1. Run test with option -p or --params
php codecept run -p "site-id: 123456, group: admin" -p "duty: free"

In the test it should be available like following:

$I = new AcceptanceTester($scenario);
.....
$I->amOnPage('/');
.....
$I->fillField('.site-id-input', \Codeception\Configuration::getParam('site-id'));
.....

Also I updated file tests/_support/Helper/Acceptance.php like so

namespace Helper;

use Codeception\Configuration;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class Acceptance extends \Codeception\Module
{

    public function getParam($key)
    {
        return Configuration::getParam($key);
    }
}

And I was able to use this:

$I = new AcceptanceTester($scenario);
.....
$I->amOnPage('/');
.....
$I->fillField('.site-id-input', $I->getParam('site-id'));
.....

It probably is better way how to use params in the tests, and if you have ideas how to do it better let me know.

}

return $updatedParams;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Line indented incorrectly; expected 4 spaces, found 5
  • Closing brace indented incorrectly; expected 4 spaces, found 5

return $res;
}

public static function getParam($key) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opening brace should be on a new line

@DavertMik
Copy link
Member

The implementation is pretty ok but we already have Params concept so it would be confusing to have them in different places.

I think what you trying to achieve can be implemented using environment configuration and a custom helper:

namespace Helper;

class Acceptance extends \Codeception\Module
{
   protected $config = ['site_id' => null];

   function enterSiteId($field)
   {
       $this->getModule('WebDriver')->fillField($field, $this->config['site_id']);
    }
}

this helper cna be configured in acceptance.suite.yml:

modules:
    - WebDriver:
        url: localhost
        browser: chrome
    - \Helper\Acceptance:
        site_id: 1 

and reconfigured with environments.

Probably this way is a bit complcated than your params, but to me it looks cleaner and more reliable, as it moves the params to configuration.

@yuriy-chabaniuk
Copy link
Author

yuriy-chabaniuk commented Feb 2, 2017

Thank you @DavertMik for fast review.

Params concept that you currently have looks pretty solid and flexible. Also environment concept sounds great for this case. But there is some difference why neither is suitable for me.

I will explain why.
We have platform to quick and delivery sites for real estate.
Lets imagine that we have script like this to test important functionality:

$I = new AcceptanceTester($scenario);
.....
$I->amOnPage('/');
.....
$I->fillField('.site-id-input','123456');
// Do something

Using this script we should test functionality on different sites (without updating test for specific site)

Then lets imagine that we have 50 new sites per day. And we need to test all of them.

Using current 'Params concept' or environment configuration with helpers, as you described above, require as to change (update) config file each time when we need to test new site. Am I right or I missed something.

Using dynamic params directly from the console should:

  1. give as ability to run tests without updating any config files.
  2. run sites for different sites at once. (in multiple streams)

Also some explanation in the issue #3974

@DavertMik
Copy link
Member

We have -o --override option which can dynamically update the config. http://codeception.com/docs/reference/Commands#Run

Just put your site_id to Helper's config as shown above and change them on run

@vedro-compota
Copy link

this was cool PR, It's really problem to get user parameter from terminal in codeception now....

@Naktibalda
Copy link
Member

@vedro-compota
Copy link

@Naktibalda yes, but there I've only red about params from .env/config (as I understand), not from command line (terminal).
As result I started use overriding config params instead of using custom console params (e.g. http://fkn.ktu10.com/?q=node/11401)

@Naktibalda
Copy link
Member

Naktibalda commented Mar 2, 2020

It is very easy to set environment variables in command line:

URL=https://example.org ./vendor/bin/codecept run

@vedro-compota
Copy link

@Naktibalda thank you!

@BobRay
Copy link

BobRay commented Jan 23, 2021

URL=https://example.org codecept run acceptance

'URL' is not recognized as an internal or external command,
operable program or batch file.

This happens in the Cmder console emulator on Windows 10.

Is there any other way to pass in an environment variable?

@Naktibalda
Copy link
Member

Naktibalda commented Jan 23, 2021

Try set URL "https://example.org/" on separate line.
Though you should get a better terminal, e.g. Git Bash, or install Linux on WSL.

@BobRay
Copy link

BobRay commented Jan 24, 2021

Environment variable URL "https://example.org/" not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants