问题描述
我相信我对BehatContext和MinkContext尚不清楚,也不确定为什么我会从我的应用程序中继承一个.基本上我不清楚为什么我必须在每个函数中实例化一个新的Client对象.我应该可以使用$ this,因为我的behat.yml文件中已装入了goutte.
I believe I am not clear on BehatContext versus MinkContext and am not sure why I would inherit from one or the other in my app. Basically I am not clear on why I have to instantiate a new Client object in every function. I should be able to use $this since I have goutte loaded in my behat.yml file.
请问有什么提示吗?
<?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();
}
}
推荐答案
BehatContext
是基本上下文,对于大多数您自己的上下文,它将是默认选择.
BehatContext
is a basic context which would be a default choice for most of your own contexts.
MinkContext
是一个特殊的上下文,可让您访问Mink会话.它还包含一些基本步骤定义.这就是为什么您永远都不要扩展它(因为您只能扩展一次,因为步骤定义不能重复).
MinkContext
is a specialized context giving you access to the Mink session. It also contains some basic step definitions. That's why you should never extend it ('cause you'd be only able to extend it once, since step definitions cannot be duplicated).
如果您需要访问Mink会话,请扩展RawMinkContext
.就像没有步骤定义的MinkContext
一样.
If you need to access the Mink Session extend the RawMinkContext
. It's like the MinkContext
without step definitions.
您为什么要自己实例化客户端?只需使用Mink Session,它将为您完成:
Why are you trying to instantiate the Client yourself? Just use the Mink Session, which will do it 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();
}
// ...
}
查看MinkContext
中的步骤定义以获取灵感.
Look at the step definitions in the MinkContext
for inspiration.
要阅读(认真阅读,请先阅读文档):
To read (seriously, read the docs first):
- Mink: http://mink.behat.org/
- MinkExtension: http://extensions.behat.org/mink/index.html
- MinkContext类: https://github .com/Behat/MinkExtension/blob/master/src/Behat/MinkExtension/Context/MinkContext.php
- RawMinkContextClass: https://github.com com/Behat/MinkExtension/blob/master/src/Behat/MinkExtension/Context/RawMinkContext.php
- Mink: http://mink.behat.org/
- MinkExtension: http://extensions.behat.org/mink/index.html
- MinkContext class: https://github.com/Behat/MinkExtension/blob/master/src/Behat/MinkExtension/Context/MinkContext.php
- RawMinkContextClass: https://github.com/Behat/MinkExtension/blob/master/src/Behat/MinkExtension/Context/RawMinkContext.php
这篇关于我的FeatureContext有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!