sepehr/behat-laravel-js
Composer 安装命令:
composer require sepehr/behat-laravel-js
包简介
Testing javascript apps with Laravel and Behat.
README 文档
README
This is a companion package to Behat's Laravel Extension that provides utilities to work around some issues and limitations when testing Javascript applications using browser emulators, like Selenium, PhantomJS or Zombie.
The workarounds used in this package are heavily inspired by Laravel Dusk code. Read this post if you wish to have more context about the history of the issues.
Problems and workarounds
If you're here, you probably already know about the extension limitations, but in case you don't; read on.
tl;dr
Install, and use these three traits in your FeatureContext and you're done. Remember to not to use
DatabaseTransactions and/or Migrator traits.
<?php use Laracasts\Behat\Context\MigrateRefresh; use Sepehr\BehatLaravelJs\Concerns\AuthenticateUsers; use Sepehr\BehatLaravelJs\Concerns\PreserveBehatEnvironment; class FeatureContext extends MinkContext implements Context { use PreserveBehatEnvironment, AuthenticateUsers, MigrateRefresh; // ... }
Environment
To alleviate this issue, you need to use the
\Sepehr\BehatLaravelJs\Concerns\PreserveBehatEnvironmenttrait in yourFeatureContextclass.
Consider this example: Your testing environment is set to use SQLite as the database while your local/production
environment use MySQL. When you run a @javascript Behat scenario, a browser emulator dispatches a request
to your Laravel app endpoint. And to your surprise, it meets with another Laravel instance that is using the .env
file. An instance operating in a different environment: different databases, cache drivers, queues, etc.
Database Transactions
To alleviate this one, you need to use
Laracasts\Behat\Context\MigrateRefreshtrait in yourFeatureContextclass instead of usingDatabaseTransactionsandMigratortraits.
The very popular DatabaseTransactions trait and its
BLE counterpart,
begin a transaction before a scenario. The transaction will be commited only if there are no exceptions. Then after the
scenario, they will rollback it in order to keep the database state intact.
In middle of the process, when a browser emulator dispatches the request to another instance of Laravel, the transaction won't be commited and thus you will encounter unexpected results.
Consider a scenario, when you first insert a few test users into the database in the testing instance and in the next
step, you request a page (to the other instance) to see if their data exist on a page. To your surprise, the data you're
looking for won't be available.
Instead, in order to maintain a clean database state, use the MigrateRefresh trait to refresh the database before each
scenario.
Authentication
To alleviate this one, you should be using the
\Sepehr\BehatLaravelJs\Concerns\AuthenticateUserstrait in yourFeatureContextclass. It will provide you with helper authentication methods to login, logout and get the current user data.
Authentication is another problem. In the testing environment you log a user into the system, then you tell Behat to fire a Selenium session and check a protected page. As you might already know, the user won't be logged-in. The testing browser has no authentication cookie to send to the other Laravel instance.
Available methods are:
| Description | Signature |
|---|---|
| Login as a user | loginAs($user|$userId, $guard|null) |
| Logout | logout($guard|null) |
| Get current user's data | currentUserInfo($guard|null) |
| Assert that the user is authenticated | assertAuthenticated($guard|null) |
| Assert that the user is authenticated AS | assertAuthenticatedAs($user, $guard|null) |
| Assert that the user is NOT authenticated | assertGuest($guard|null) |
Just as Dusk.
Installation
Install the package using composer:
composer install sepehr/behat-laravel-js
Then, in your AppServiceProvider::boot(), register the package service provider for testing environments. Please note
that it'd be a SECURITY RISK if you enable this in your production environment. See the package service provider to find
out why.
<?php class AppServiceProvider extends ServiceProvider { public function boot() { if ($this->app->environment('local', 'testing', 'acceptance')) { $this->app->register(\Sepehr\BehatLaravelJs\ServiceProvider::class); } } // ... }
sepehr/behat-laravel-js 适用场景与选型建议
sepehr/behat-laravel-js 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.25k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2017 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「javascript」 「BDD」 「Behat」 「selenium」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sepehr/behat-laravel-js 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sepehr/behat-laravel-js 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sepehr/behat-laravel-js 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Helper that decorates any SUS with a phpspec lazy object wrapper
Behat Context for testing Symfony Api
A Behat Extension that allows custom bootstrapping
Environment processor and contexts autoloader
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Provides access to magento2 object manager from behat and allows to change magento config settings temporarly
统计信息
- 总下载量: 1.25k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-23