Struggling to get Mink working with Behat

2020-06-16 01:40发布

问题:

I've been following this guide (and installed everything through composer): http://docs.behat.org/cookbook/behat_and_mink.html and am attempting to get Behat + Mink working but everytime I try and run bin/behat I get the following error:

PHP Fatal error:  Call to a member function getSession() on a non-object in vendor/behat/mink-extension/src/Behat/MinkExtension/Context/RawMinkContext.php on line 80

That line of code is:

return $this->getMink()->getSession($name);

So for some reason the mink attribute is empty but I've no idea why.

My .feature file is exactly the same as the one in the guide, the FeatureContext class is also from the guide:

use Behat\Behat\Context\ClosuredContextInterface,
    Behat\Behat\Context\TranslatedContextInterface,
    Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;     

use Behat\MinkExtension\Context\MinkContext;

/**
 * Features context.
 */
class FeatureContext extends MinkContext 
{

}

and my vendor/behat/mink/behat.yml file contains:

context:
  extensions:
    Behat\MinkExtension\Extension:
      base_url:  'http://en.wikipedia.org/'
      goutte:    ~
      selenium2: ~

I've also tried making my class extend BehatContext and then call useContext but that gives me the same error. Behat itself seems to work it's just anything with Mink produces that fatal error and I've no idea how to fix it.

回答1:

This is because you should copy vendor/behat/behat/behat.yml.dist file to your/project/root/behat.yml, rather than editing the file in the vendor dir and add extesions to the default section.

And here's what it looks like:

default:
  extensions:
    Behat\MinkExtension\Extension:
      base_url: http://lunch-time/app_dev.php
      goutte: ~
      selenium2: ~

  paths:
    features:  features
    bootstrap: features/bootstrap

annotations:
  paths:
    features: features/annotations

closures:
  paths:
    features: features/closures


回答2:

I was facing a similar issue. We need to tell Symfony to initialize the object.

Mine got fixed after adding under the default > suites > my_suite.

contexts: [Behat\MinkExtension\Context\MinkContext]

Here is how my new behat.yml looks like.

default:
    suites:
        my_suite:
            type: symfony_bundle
            bundle: AcmeProjectManagerBundle
            contexts: [Behat\MinkExtension\Context\MinkContext]
extensions:
    Behat\Symfony2Extension: ~
    Behat\MinkExtension:
        base_url: http://en.wikipedia.org
        goutte: ~
        selenium2: ~
        sessions:
            default:
                symfony2: ~


标签: behat mink