有没有办法以编程方式向behat功能提供变量? [英] Is there a way to programmatically supply variables to behat features?

查看:106
本文介绍了有没有办法以编程方式向behat功能提供变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用behat详尽地测试一组页面.

I want to exhaustively test a set of pages with 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行的不可维护的功能,每次添加新路径或更多元素时都需要对其进行编辑.

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.

我可以将mysql查询的结果输入"参数吗?

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?

编辑:我确实找到了这篇文章,这基本上将所有逻辑从.feature文件(Gherkin)中移入了FeatureContext(PHP)中.但这似乎并不是利益相关者友好的方式.那真的是最好/唯一的方法吗?

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?

推荐答案

这对您来说几乎是一份复制粘贴工作.我为您编写了一个自定义步骤定义Given the page contents are correct.我还添加了Scenario Outline作为经典示例,因此它们两者都相同,但是您感兴趣的是自定义步骤定义.

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

结果

FEATURECONTEXT

您所需要做的就是使用getPageAndContent()查询数据库以返回您想要的内容.现在,我只是在$pageAndContent中对一些示例进行了硬编码.

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);
    }
}

这篇关于有没有办法以编程方式向behat功能提供变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆