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

getContext() method doesn't seem to exist #386

Closed
phenaproxima opened this issue Jun 19, 2017 · 8 comments
Closed

getContext() method doesn't seem to exist #386

phenaproxima opened this issue Jun 19, 2017 · 8 comments

Comments

@phenaproxima
Copy link

After updating to Drupal Extension 3.3.0, I get this error when running acquia/lightning's tests:

Fatal error: Call to undefined method Behat\Behat\Context\Environment\UninitializedContextEnvironment::getContexts() in vendor/drupal/drupal-extension/src/Drupal/DrupalExtension/Context/DrupalSubContextBase.php on line 64

I'm not sure what's happening here -- any ideas would be welcome.

@balsama
Copy link

balsama commented Jun 20, 2017

I traced this problem to line 70 of DrupalSubContextBase. It thinks $environment is a \Behat\Behat\Context\Environment\InitializedContextEnvironment, but it's actually \Behat\Testwork\Environment\Environment, which doesn't have the getContexts() method (InitializedContextEnvironment does).

It doesn't look like that's changed in a long time though.

@pfrenssen
Copy link
Collaborator

I think this might be related to the Behat update that we did in the latest release. We were stuck on Behat 3.1 before. In Behat 3.2 a change was introduced that properly reinitializes Context classes at the start of every scenario and any code that relies on data being present in contexts from before the scenario started will now fail. Ref. Behat/Behat#758

I had a look at one of the contexts that threw a failure in the build that was linked, and it seems that this is the cause of the problem:

https://github.com/acquia/lightning/blob/02b18b602fb75f0990f802fa8d60f2a59234f82b/src/LightningExtension/Context/BlockContext.behat.inc#L21

  /**
   * Gathers required contexts.
   *
   * @BeforeScenario
   */
  public function gatherContexts() {
    $this->minkContext = $this->getContext(MinkContext::class);
  }

The @BeforeScenario means this code runs before the scenario is started. I haven't looked at the actual implementation but it seems likely that the contexts are not yet initialized at that point so they cannot be retrieved from the environment.

This also seems like very inefficient code, since this will be executed at the start of every single scenario, even if the step definitions defined in the context are not even used in the scenario.

Can you try changing this so the contexts are retrieved when they are actually being used instead of on the @BeforeScenario hook? Something like this:

-    $this->minkContext->fillField('id', $id);
+   $this->getContext(MinkContext::class)->fillField('id', $id);

@pfrenssen
Copy link
Collaborator

I have proposed a PR that throws a more helpful exception, that will hopefully in the future help developers understand & debug this problem.

Can you confirm if the @BeforeScenario is the root of the problem?

@balsama
Copy link

balsama commented Jun 28, 2017

@pfrenssen yes, moving the getContext calls out of @BeforeScenario fixed this. Thanks for the help.

@pfrenssen
Copy link
Collaborator

You're welcome! Glad to hear it has been fixed!

@kmajzlik
Copy link

Can somebody help me with this?
How to use Scenario Outline with DrupalExtension 3.3 and Behat 3.3 (both needed because running Drupal 8.4-dev)?

If i tried somehow remove this call because of UninitializedContextEnvironment class then i have
WebDriver\Exception\CurlExec: Curl error thrown for http POST to http://selenium-hub:4444/wd/hub/session with params
error.

@jhedstrom
Copy link
Owner

#390 might have helped with this too.

@kmajzlik
Copy link

Thank you very much, it seems that using
"drupal/drupal-extension": "3.3.x-dev",
saved me.

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

No branches or pull requests

5 participants