這將是一個複製+粘貼工作給你。我爲你寫了一個自定義步驟定義Given the page contents are correct
。我還添加了一個Scenario Outline
作爲典型示例,因此它們都是相同的,但您感興趣的是自定義步驟定義。
GHERKHIN
Feature: Just testing
Scenario Outline: Multiple generic visits
Given I am on "<page>"
Then I should see "<content>"
Examples:
| page | content |
|/ | HOME PAGE |
| /login | Username |
| /profile/step_one | Name |
Scenario: Multiple dynamic visits
Given the page contents are correct
結果
FEATURECONTEXT
所有你需要的是用getPageAndContent()
查詢d atabase返回你想要的。現在我只是在$pageAndContent
中對幾個例子進行了硬編碼。
namespace Application\FrontendBundle\Features\Context;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelAwareContext;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\HttpKernel\KernelInterface;
use Exception;
use LogicException;
class FeatureContext extends MinkContext implements KernelAwareContext
{
/* @var KernelInterface */
private $kernel;
public function setKernel(KernelInterface $kernelInterface)
{
$this->kernel = $kernelInterface;
}
/**
* @Given /^the page contents are correct$/
*/
public function thePageContentsAreCorrect()
{
$pageAndContent = $this->getPageAndContent();
foreach ($pageAndContent as $page => $content) {
try {
$this->visitPath($page);
} catch (Exception $e) {
throw new LogicException(sprintf('The page "%s" does not exist.', $page));
}
try {
$this->assertSession()->pageTextContains($this->fixStepArgument($content));
} catch (Exception $e) {
throw new LogicException(sprintf('The page "%s" does not contain "%s".', $page, $content));
}
}
}
/**
* @return array
*/
private function getPageAndContent()
{
/*
$em = $this->getEntityManager();
$repo = $this->getRepository($em, 'ApplicationFrontendBundle:Pages');
$pageAndContent = [];
$pages = $repo->findAll();
foreach ($pages as $page) {
// Build you array here
$pageAndContent[$page->getUrl] = $page->getContent();
}
return $pageAndContent;
*/
return [
'/' => 'HOME PAGE',
'/login' > 'Username',
'/profile/step_one' => 'Name'
];
}
/**
* @return EntityManager
*/
private function getEntityManager()
{
return $this->kernel->getContainer()->get('doctrine')->getManager();
}
/**
* @param EntityManager $entityManager
* @param string $serviceName
*
* @return EntityRepository
*/
private function getRepository(EntityManager $entityManager, $serviceName)
{
return $entityManager->getRepository($serviceName);
}
}
好像你在暗示,正如我擔心的那樣,純粹在小黃瓜功能中沒有辦法做到這一點。例如以利益相關方友好的方式。 – aaronbauman
小黃瓜的步驟是有限的,所以在邊緣情況下,我們將不得不編寫我們自己的步驟定義,您的案例就是其中之一。如果我向您展示了我的一個Context文件,您可能會哭泣! – BentCoder