What is wrong with my FeatureContext?

I believe that I do not understand BehatContext versus MinkContext and not sure why I would inherit from one or the other in my application. Basically, I don’t understand why I have to instantiate a new Client object in each function. I would have to use $ this, since I have a goutte loaded in my behat.yml file.

Any tips please?

<?php namespace Main\ReferralCaptureBundle\Features\Context; use Main\ReferralCaptureBundle\Features\Context\FeatureContext; use Symfony\Component\HttpKernel\KernelInterface; use Behat\Symfony2Extension\Context\KernelAwareInterface; use Behat\MinkExtension\Context\MinkContext; use Behat\MinkExtension\Context\RawMinkContext; use Behat\Behat\Context\BehatContext, Behat\Behat\Exception\PendingException; use Behat\Gherkin\Node\PyStringNode, Behat\Gherkin\Node\TableNode; use Goutte\Client; // // Require 3rd-party libraries here: // require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Framework/Assert/Functions.php'; // /** * Feature context. */ class FeatureContext extends RawMinkContext //MinkContext if you want to test web implements KernelAwareInterface { private $kernel; private $parameters; /** * Initializes context with parameters from behat.yml. * * @param array $parameters */ public function __construct(array $parameters) { $this->parameters = $parameters; $this->useContext('behat', new BehatContext); $this->useContext('mink', new MinkContext); } /** * Sets HttpKernel instance. * This method will be automatically called by Symfony2Extension ContextInitializer. * * @param KernelInterface $kernel */ public function setKernel(KernelInterface $kernel) { $this->kernel = $kernel; } /** * @Given /^I am on homepage$/ */ public function iAmOnHomepage() { // $this->getSession()->visit("/"); $client = new Client(); $crawler = $client->request('GET', 'http://local.referral.com/'); $link = $crawler->selectLink('I am a Physician')->link(); if (!count($link)>0) { throw new Exception("Home Page Not Loaded:\n"); } } /** * @Given /^I follow "([^"]*)"$/ */ public function iFollow($arg1) { $client = new Client(); $crawler = $client->request('GET', 'http://local.referral.com/'); $link = $crawler->selectLink('Register')->link(); $crawler = $client->click($link); } /** * @When /^I fill in "([^"]*)" with "([^"]*)"$/ */ public function iFillInWith($arg1, $arg2) { throw new PendingException(); } /** * @Given /^I press "([^"]*)"$/ */ public function iPress($arg1) { throw new PendingException(); } /** * @Then /^I should see "([^"]*)"$/ */ public function iShouldSee($arg1) { throw new PendingException(); } /** * @Given /^I should be on homepage$/ */ public function iShouldBeOnHomepage() { throw new PendingException(); } } 
+1
source share
1 answer

I believe that I do not understand BehatContext versus MinkContext and not sure why I would inherit from one or the other in my application.

BehatContext is the base context that will be used by default for most of your own contexts.

MinkContext is a specialized context that gives you access to a Mink session. It also contains some basic step definitions. This is why you should never extend it (because you could only extend it once, since the definitions of steps cannot be duplicated).

If you need to access a Mink Session, open RawMinkContext . It is like a MinkContext without a step definition.

Basically, I don’t understand why I have to instantiate a new Client object in each function. I would have to use $ this, since I have a goutte loaded in my behat.yml file.

Why are you trying to create a client instance yourself? Just use a Mink session that will do this for you:

 <?php namespace Main\ReferralCaptureBundle\Features\Context; // your use statements here... class FeatureContext extends RawMinkContext { /** * @Given /^I am on homepage$/ */ public function iAmOnHomepage() { $session = $this->getSession(); $session->visit($this->locatePath('/')); $link = $session->getPage()->findLink('I am a Physician'); if (null === $link) { throw new Exception("Home Page Not Loaded:\n"); } } /** * @Given /^I follow "([^"]*)"$/ */ public function iFollow($arg1) { $link = $this->getSession()->findLink('Register'); if (null === $link) { throw new \LogicException('Could not find the "Register" link'); } $link->click(); } // ... } 

Look at step definitions in the MinkContext for inspiration.

To read (seriously, read the docs first):

+3
source

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


All Articles