问题
I want to exhaustively test a set of pages with behat.
For example, this Scenario Outline:
Scenario Outline:
Given I am at <path>
When I click some button
Then I should see <hidden-stuff>
| path | hidden-stuff |
| path1 | element1 |
| path1 | element2 |
| path1 | element3 |
| path2 | element1 |
| path2 | element2 |
| path2 | element3 |
| path3 | element1 |
| path3 | element2 |
| path3 | element3 |
...
In my specific case, I have over 10 examples and 50 paths, so you can see how this becomes extremely unwieldy. I'm trying to avoid an unmaintainable feature with 500 rows, which needs to be edited every time I add a new path or more elements.
Can I feed the results of a mysql query into the "" parameter?
Or supply "path" on the command line or via environment?
Is there a better way to approach this problem?
edit: I did find this post, which basically wrests all the logic out of the .feature file (Gherkin) and into the FeatureContext (PHP). But that doesn't seem like a stakeholder-friendly way to do behat. Is that really the best / only approach?
回答1:
This will be pretty much a copy+paste job for you. I wrote a one custom step definition Given the page contents are correct
for you. I also added a Scenario Outline
as a classic example as well so both of them do the same but the one you're interested in is custom step definition one.
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
RESULT
FEATURECONTEXT
All you need is to use getPageAndContent()
to query the database to return what you want. For now I just hard-coded a few examples in $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);
}
}
来源:https://stackoverflow.com/questions/34026535/is-there-a-way-to-programmatically-supply-variables-to-behat-features