ekino / behat-helpers

Behat 辅助工具

v0.3.1 2021-08-24 08:33 UTC

README

Latest Stable Version Build Status Coverage Status Total Downloads

此库提供了一些 Behat 辅助工具。

这是一个正在进行中的项目,如果您想实现某些功能,请随时提出建议或贡献以帮助我们!

安装

使用 Composer 安装

composer require --dev ekino/behat-helpers

在您的 FeatureContext 类中使用所需的辅助工具。

BaseUrlTrait

Behat 只处理一个基本 URL 来运行测试套件。为了测试多站应用程序,这个特性允许您通过套件注入基本 URL。

# behat.yml
default:
    suites:
        suite_1:
            contexts:
                - Tests\Behat\Context\MyFeatureContext:
                    - https://foo.bar.localdev
        suite_2:
            contexts:
                - Tests\Behat\Context\MyFeatureContext:
                    - https://bar.foo.localdev
// your feature context

namespace Tests\Behat\Context;

use Behat\MinkExtension\Context\MinkContext;
use Ekino\BehatHelpers\BaseUrlTrait;

class MyFeatureContext extends MinkContext
{
    use BaseUrlTrait;

    public function __construct($baseUrl)
    {
        $this->setBaseUrl($baseUrl);
    }
}

DebugTrait

这个特性可用于调试目的。当步骤失败时,它会捕获 HTML 和截图,这样您就可以看到此刻的页面。

注意,它需要来自 behat/symfony2-extensionKernelDictionary

// your feature context

namespace Tests\Behat\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelDictionary;
use Ekino\BehatHelpers\DebugTrait;

class MyFeatureContext extends MinkContext
{
    use DebugTrait;
    use KernelDictionary;
}

您还可以通过在特征或场景中添加 behat_helpers_profile 标签来分析测试。您将看到每个场景消耗的内存和执行时间。

@behat_helpers_profile
Feature: Front test
  I want to be able to access to the application
@behat_helpers_profile
Scenario: foo elt should be visible in 2 seconds or less
    Given ...
    Then I wait for "foo" element being visible for 2 seconds

ExtraSessionTrait

这个特性提供了一些会话辅助工具。

// your feature context

namespace Tests\Behat\Context;

use Behat\MinkExtension\Context\MinkContext;
use Ekino\BehatHelpers\ExtraSessionTrait;

class MyFeatureContext extends MinkContext
{
    use ExtraSessionTrait;
}
Scenario: foo elt should be visible in 2 seconds or less
    Given ...
    Then I wait for "foo" element being visible for 2 seconds

ExtraWebAssertTrait

这个特性提供了一些额外的断言。

// your feature context

namespace Tests\Behat\Context;

use Behat\MinkExtension\Context\MinkContext;
use Ekino\BehatHelpers\ExtraWebAssertTrait;

class MyFeatureContext extends MinkContext
{
    use ExtraWebAssertTrait;
}

ReloadCookiesTrait

这个特性旨在编写小/简单的场景并保留执行时间。为此,在场景之间重新加载 cookies。在多步骤表单的情况下这可能很有用:第一个场景填写第一步并提交(这里保存了 cookies),然后执行第二个场景(重新加载 cookies,因此不需要再次执行前面的步骤)并填写第二步...等等。

// your feature context

namespace Tests\Behat\Context;

use Behat\MinkExtension\Context\MinkContext;
use Ekino\BehatHelpers\ReloadCookiesTrait;

class MyFeatureContext extends MinkContext
{
    use ReloadCookiesTrait;

    /**
     * @When /^I fill the first step$/
     */
    public function fillStep1()
    {
        $this->doOnce(function () {
            $this->iAmOnHomepage();
            $this->fillField('input_step1', 'foo');
            $this->pressButton('Next');
        });
    }

    /**
     * @When /^I fill the second step$/
     */
    public function fillStep2()
    {
        $this->doOnce(function () {
            $this->fillStep1();
            $this->fillField('input_step2', 'bar');
            $this->pressButton('Next');
        });
    }
}
Scenario: I can fill the step1
    Given I fill the first step
    Then I should be on "/step2"
    
Scenario: I can fill the step2
    Given I fill the second step
    Then I should be on "/step3"

您可以通过添加 behat_helpers_no_cache 标签来避免保存/重新加载 cookies。

@behat_helpers_no_cache
Scenario: I can fill the step2
    Given I fill the second step
    Then I should be on "/step3"

您可以通过添加 behat_helpers_reset_cache 标签来清除先前保存的 cookies。

@behat_helpers_reset_cache
Scenario: I am on /step1 if previous cookies are reset
    Given I fill the second step
    Then I should be on "/step3"
    But I am on "/step1"

ReloadDatabaseTrait

这个特性允许您在场景结束时将数据库恢复到场景开始之前的状态。如果场景修改了数据库,这可以很有用,因此场景可以独立。

当然,对于大型数据库,这可能需要一些时间。

目前,只支持 MySQL。它需要安装 mysqldump 来导出数据,以及 doctrine/doctrine-bundle 来重新导入导出数据。

注意,它需要来自 behat/symfony2-extensionKernelDictionary

// your feature context

namespace Tests\Behat\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelDictionary;
use Ekino\BehatHelpers\ReloadDatabaseTrait;

class MyFeatureContext extends MinkContext
{
    use KernelDictionary;
    use ReloadDatabaseTrait;
}
@behat_helpers_restore_db
Scenario: I can fill the step2
    Given I fill the second step
    Then I should be on "/step3"

RouterAwareTrait

这个辅助工具使用来自 Symfony 的路由器,因此避免了在场景中硬编码 URL。

注意,它需要来自 behat/symfony2-extensionKernelDictionary

// your feature context

namespace Tests\Behat\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelDictionary;
use Ekino\BehatHelpers\RouterAwareTrait;

class MyFeatureContext extends MinkContext
{
    use KernelDictionary;
    use RouterAwareTrait;
}
Scenario: I can see "something" when I visit /foo
    Given I am on "my_route_id"
    Then I should see "something"

如果您的路由需要一些参数,您可以通过在路由标识符和参数之间用 ; 分隔来提供它们。

Scenario: I can see "something" when I visit /foo/1/2
    Given I am on "my_route_id;param1=1&param2=2"
    Then I should see "something"

SonataAdminTrait

这个特性将 sonata-project/admin-bundle 与一些基本功能(如与菜单、导航栏、弹出、select2 等交互)集成。您可以与 ReloadCookiesTrait 结合使用,例如,仅登录一次,并使用 RouterAwareTrait 来使用路由 ID。

注意,它需要来自 behat/symfony2-extensionKernelDictionary

// your feature context

namespace Tests\Behat\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelDictionary;
use Ekino\BehatHelpers\ReloadCookiesTrait;
use Ekino\BehatHelpers\RouterAwareTrait;
use Ekino\BehatHelpers\SonataAdminTrait;

class MyFeatureContext extends MinkContext
{
    use KernelDictionary;
    use ReloadCookiesTrait;
    use RouterAwareTrait;
    use SonataAdminTrait;

    /**
     * @When /^I login with username "(?P<username>[^"]*)" and password "(?P<password>[^"]*)"$/
     *
     * @param string $username
     * @param string $password
     */
    public function fillLoginForm($username, $password)
    {
        $this->doOnce(function () use ($username, $password) {
            $this->login($username, $password);
        });
    }
}
Scenario: I can login and then access to the admin dashboard
    Given I login with username "admin" and password "admin"
    Then I should be on "sonata_admin_dashboard"
    And I should see "Welcome to the admin dashboard"

SonataPageAdminTrait

这个特性将 sonata-project/page-bundle 与一些基本功能(如与容器、块等交互)集成。您可以与 ReloadCookiesTraitRouterAwareTrait 结合使用,以使用路由 ID。

// your feature context

namespace Tests\Behat\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelDictionary;
use Ekino\BehatHelpers\SonataPageAdminTrait;

class MyFeatureContext extends MinkContext
{
    use KernelDictionary;
    use ReloadCookiesTrait;
    use RouterAwareTrait;
    use SonataPageAdminTrait;
}
Scenario: I can see "Simple text block" when I add a SimpleTextBlockService
Given     I login
Then      I am on "admin_app_sonata_page_compose;id=1"
And       I open the container by text "Content"
And       I add the block "Simple text" with the name "Foo"
And       I open the block "Foo"
And       The block "Foo" should be opened
And       I rename the block "Foo" with "Bar"
And       I submit the block "Foo"
And       The block "Bar" should be closed
And       I should see 1 blocks
And       I delete the block "Bar"