clevyr/laravel-behat-dusk

0.0.2 2021-10-21 16:56 UTC

This package is auto-updated.

Last update: 2024-09-15 04:31:39 UTC


README

GitHub release (latest SemVer including pre-releases)

Laravel Behat Dusk 是一个简单包,旨在将 Behat 与 Laravel 和 Laravel Dusk 集成。

这不是 Behat 与 Laravel 的 1:1 实现,当前目标是提供一种简单快捷的方式在 Laravel 应用程序中编写行为驱动测试。

先决条件

  • Laravel 8+
  • PHP 8.0+
  • 对行为驱动开发 (BDD) 和 Behat 有基本了解

安装

composer require clevyr/laravel-behat-dusk --dev

安装包

php artisan lbd:install

以下文件将被输出

  • features
    • bootstrap
      • FeatureContext
  • behat.yml

测试环境

Laravel Behat Dusk 的环境设置与 Laravel Dusk 完全相同。请参阅他们的文档了解环境处理。https://laravel.net.cn/docs/8.x/dusk#environment-handling

在 Docker 中运行

  • 通过运行 cp .env .env.dusk.local 创建一个 .env.dusk.local 文件,并将其添加到 .gitignore 文件中
  • 更新以下 .env.dusk.local 环境文件
APP_URL=http://laravel.test
DUSK_DRIVER_URL=http://selenium:4444/wd/hub

如果您不使用 Laravel Sail,请使用应用程序和 Selenium 容器的 docker-compose 容器名称。

  • 在应用程序容器中运行 Laravel Behat Dusk sail artisan lbd

用法

创建上下文文件

这将在 features/bootstrap 中创建一个上下文文件,并在 behat.yml 中追加相关配置数据

php artisan lbd:make ExampleContext

--profile[=PROFILE]  Create under the profile in the Behat Config [default: "default"]
--suite[=SUITE]      Create under the suite in the Behat Config [default: "default"]

运行 Behat

运行此命令以运行 Behat 测试运行器,您可以使用 Behat CLI 的所有参数

php artisan lbd

特性

在每个场景之前刷新数据库 use RefreshScenario;

示例特性文件和上下文文件

login.feature 文件

Feature: User Login

    In order to acess the site
    As a user
    I want to be able to login

    Scenario: Logging in as a User
        Given There are users
            | email            | password |
            | user@example.com | password |
        When I am on the "/login" page
        When I fill in email
        When I fill in password
        When I press the submit button
        Then I should be redirected to the "/dashboard" page

FeatureContext.php

<?php

use Behat\Gherkin\Node\TableNode;
use Clevyr\LaravelBehatDusk\BehatDuskTestCase;
use Clevyr\LaravelBehatDusk\Traits\RefreshScenario;
use Laravel\Dusk\Browser;

/**
 * Defines application features from the specific context.
 */
class FeatureContext extends BehatDuskTestCase
{
    use RefreshScenario;

    /**
     * @var array $user
     */
    public array $user;

    /**
     * @Given /^There are users$/
     * @param TableNode $table
     */
    public function thereAreUsers(TableNode $table)
    {
        foreach ($table->getHash() as $row) {
            $this->user = ['email' => $row['email'], 'password' => $row['password']];
        }
    }

    /**
     * @When /^I am on the "([^"]*)" page$/
     * @param string $page
     * @throws Throwable
     */
    public function iAmOnThePage(string $page)
    {
        $this->browse(function (Browser $browser) use ($page) {
            $browser->visit($page);
        });
    }

    /**
     * @When /^I fill in (.*)$/
     * @throws Throwable
     */
    public function iFillIn()
    {
        $this->browse(function (Browser $browser) {
            $browser
                ->type('email', $this->user['email'])
                ->type('password', $this->user['password']);
        });
    }

    /**
     * @When /^I press the submit button$/
     * @throws Throwable
     */
    public function iPressTheSubmitButton()
    {
        $this->browse(function (Browser $browser) {
            $browser
                ->press('[type="submit"]')
                ->waitForLocation('/dashboard');
        });
    }

    /**
     * @Then /^I should be redirected to the "([^"]*)" page$/
     * @param string $page
     * @throws Throwable
     */
    public function iShouldBeRedirectToThePage(string $page)
    {
        $this->browse(function (Browser $browser) use ($page) {
            $browser->assertPathIs($page);
        });
    }
}

测试

composer test

安全漏洞

请查阅我们的安全策略了解如何报告安全漏洞。

鸣谢

许可

Laravel Behat Dusk 是GPL-3.0 许可