Behat 3 - passing parameters (from an imported file) to the FeatureContext constructor

I am new to Behat and YAML, and I am unable to pass large arrays of parameters to the FeatureContext constructor.

It worked fine with Behat 2:

behat.yml (short for convenience):

default:
  context:
  extensions:
    Behat\MinkExtension\Extension:
      base_url: 'http://mysite.local'
      goutte: ~
      selenium2: ~
      ...
imports:
    - config/data/mydata.yml

and mydata.yml:

default:
  context:
    parameters:
      some_long_array:
        'key1': 'value1'
        'key2': 'value2'
        ...

and I could have many such imported parameter files.

Using Behat 3 - I tried to achieve the same with the following behat.yml (shortened):

default:
  suites:
    default:
      contexts:
        - FeatureContext
  extensions:
    Behat\MinkExtension:
      base_url: 'http://mysite.local'
      goutte: ~
      selenium2: ~
  ...
imports:
    - config/data/mydata.yml

and mydata.yml:

default:
  suites:
    default:
      contexts:
        - FeatureContext:
            parameters:
              some_long_array:
                'key1': 'value1'
                'key2': 'value2'
                ...

This does not work, and nothing is passed to the constructor.

Recoverable fatal error: argument 1 passed to FeatureContext :: __ construct () must be from an array of types, none of those listed in FeatureContext β†’ __ construct ()

My FeatureContext class is as follows:

class FeatureContext implements SnippetAcceptingContext
{

  /**
   * Initializes context.
   *
   * Every scenario gets its own context instance.
   * You can also pass arbitrary arguments to the
   * context constructor through behat.yml.
   */
  public function __construct(array $parameters)
  {
    var_dump($parameters);
  }

}

If I pass inline parameters in the behat.yml file (and not in the imported file), everything works fine:

default:
  suites:
    default:
      contexts:
        - FeatureContext:
            parameters:
              some_long_array:
                'key1': 'value1'
                'key2': 'value2'
  extensions:
    Behat\MinkExtension:
      base_url: 'http://mysite.local'
      goutte: ~
      selenium2: ~
  ...

, , . , , behat.yml.

Behat 3?

+4
1

. :

default:
    autoload: [ %paths.base%/contexts ]
    extensions:
        Behat\MinkExtension:
            base_url: http://qa.api.somesite.com
            sessions:
                default:
                    selenium2: ~
        Sanpi\Behatch\Extension: ~
    suites:
        default:
            paths:    [ %paths.base%/features ]
            filters:
            contexts:
                - FeatureContext
                - Behat\MinkExtension\Context\MinkContext
        frontend:
            paths:    [ %paths.base%/features/frontend ]
            filters:
            contexts:
                - FeatureContext:
                    parameters:
                        environment: qa
                        api_url: http://api.somesite.com
                        login: some_login
                        key: 409jfa94k4tj
                - Behat\MinkExtension\Context\MinkContext
                - behatch:browser

, "frontend" "FeatureContext". , FeatureContext. .

+3

Source: https://habr.com/ru/post/1569721/


All Articles