我想用behat详尽地测试一组页面。
例如,此场景大纲:
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 |
...
在我的具体情况中,我有超过10个示例和50个路径,因此您可以看到它变得非常笨拙。我试图避免使用500行的不可维护的功能,每次添加新路径或更多元素时都需要对其进行编辑。
我可以将mysql查询的结果提供给&#34;&#34;参数β
或供应&#34;路径&#34;在命令行上还是通过环境?
有没有更好的方法来解决这个问题?
编辑:我确实找到了this post,它基本上从.feature文件(Gherkin)和FeatureContext(PHP)中获取了所有逻辑。但这似乎不是一个利益相关者友好的方式。这真的是最好的/唯一的方法吗?
答案 0 :(得分:2)
这对你来说几乎是一个复制+粘贴工作。我为您编写了一个自定义步骤定义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
<强> RESULT 强>
<强> FEATURECONTEXT 强>
您只需要使用getPageAndContent()
查询数据库以返回您想要的内容。现在我只是在$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);
}
}