ekino / behat-helpers
Helpers for Behat
Installs: 18 759
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 14
Forks: 6
Open Issues: 3
Requires
- php: ^7.2 || ^8.0
- behat/mink-extension: ^2.3
- behat/mink-selenium2-driver: ^1.3
- behat/symfony2-extension: ^2.1
- cocur/slugify: ^3.1 || ^4.0
- symfony/stopwatch: ^2.8 || ^3.0 || ^4.0
Requires (Dev)
- ekino/phpstan-banned-code: ^0.4
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^0.12
- phpstan/phpstan-phpunit: ^0.12
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-11-14 19:05:08 UTC
README
This library provides some helpers over Behat.
This is a work in progress, so if you'd like something implemented please feel free to ask for it or contribute to help us!
Installation
Require it using Composer:
composer require --dev ekino/behat-helpers
And use the helpers you need in your FeatureContext class.
BaseUrlTrait
Behat handles only one base URL to run the tests suites. In order to test a multisite-application, this trait allows you to inject a base URL by suite.
# 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
This trait can be used for debug purposes. It captures both HTML and screenshot when a step fails, so you can see the page at this moment.
Note that it requires KernelDictionary
from behat/symfony2-extension.
// 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; }
You can also profile your tests by adding the tag behat_helpers_profile
on
the feature or scenario. You'll see the consumed memory and the execution time
for each scenario.
@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
This trait provides some helpers over the session.
// 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
This trait provides some extra asserts.
// your feature context namespace Tests\Behat\Context; use Behat\MinkExtension\Context\MinkContext; use Ekino\BehatHelpers\ExtraWebAssertTrait; class MyFeatureContext extends MinkContext { use ExtraWebAssertTrait; }
ReloadCookiesTrait
This trait aims to write small/simple scenarii and preserve execution time. To do so, the cookies are reloaded between the scenarii. This can be useful in case of a multistep form: the first scenario fills the first step and submits (here the cookies are saved), then the second scenario is executed (the cookies are reloaded so no need to do the previous step again) and fills the second step...etc.
// 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"
You can add the tag behat_helpers_no_cache
to avoid cookies being saved/reloaded:
@behat_helpers_no_cache Scenario: I can fill the step2 Given I fill the second step Then I should be on "/step3"
You can add the tag behat_helpers_reset_cache
to clear cookies previously saved:
@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
This trait allows you to restore the database at the end of a scenario as it was before the scenario starts. It can be useful if a scenario alters the database, so the scenarii can be independent.
Of course, it can take a while with a big database.
For now, only MySQL is supported. It requires mysqldump
to be installed to
export the data, and doctrine/doctrine-bundle to re-import the dump.
Note that it requires KernelDictionary
from behat/symfony2-extension.
// 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
This helper uses the router from Symfony and so avoids hard-coded URL in your scenarii.
Note that it requires KernelDictionary
from behat/symfony2-extension.
// 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"
If your route requires some parameters, you can provide them by separating them
to the route identifier with a ;
:
Scenario: I can see "something" when I visit /foo/1/2 Given I am on "my_route_id;param1=1¶m2=2" Then I should see "something"
SonataAdminTrait
This trait integrates sonata-project/admin-bundle with some basics like interaction with menu, navigation bar, poping, select2... You can combined it with the ReloadCookiesTrait in order to login only once for instance, and with RouterAwareTrait to use route ids.
Note that it requires KernelDictionary
from behat/symfony2-extension.
// 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
This trait integrates sonata-project/page-bundle with some basics like interaction with container, block... You can combined it with the ReloadCookiesTrait and with RouterAwareTrait to use route ids.
// 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"