关于 behat-skill
behat-skill 是在 PHP 中构建和运行 Behat BDD 测试的参考技能,涵盖 Gherkin 功能文件、自定义上下文、Mink 浏览器步骤、远程 Selenium 执行和 CI/CD 模式。
主要功能
- 01Gherkin 功能文件与场景
- 02自定义 PHP 上下文类
- 03Mink 导航和表单步骤
- 04标签与 Behat 配置
- 05远程 Selenium 和 CI/CD 指南
适用场景
- 01用 Gherkin 描述登录行为
- 02通过 PHP 上下文添加自定义步骤
- 03按标签运行选定场景
- 04通过远程 Selenium 执行浏览器测试
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ lambdatest/ agent-skills --skill behat-skill安装后在 Claude Code 中输入/behat-skill确认已加载。
附带文件reference/advanced-patterns.mdreference/playbook.md
详细介绍
Behat BDD 技能
核心模式
功能文件 (features/login.feature)
Feature: User Login
As a user I want to log in
Scenario: Successful login
Given I am on "/login"
When I fill in "email" with "[email protected]"
And I fill in "password" with "password123"
And I press "Login"
Then I should see "Dashboard"
And I should be on "/dashboard"
Scenario: Invalid credentials
Given I am on "/login"
When I fill in "email" with "[email protected]"
And I fill in "password" with "wrong"
And I press "Login"
Then I should see "Invalid credentials"
自定义上下文 (features/bootstrap/LoginContext.php)
<?php
use Behat\MinkExtension\Context\MinkContext;
use Behat\Behat\Context\Context;
class LoginContext extends MinkContext implements Context
{
/**
* @When I login as :email with password :password
*/
public function iLoginAs(string $email, string $password): void
{
$this->visit('/login');
$this->fillField('email', $email);
$this->fillField('password', $password);
$this->pressButton('Login');
}
/**
* @Then I should see the dashboard
*/
public function iShouldSeeTheDashboard(): void
{
$this->assertSession()->addressEquals('/dashboard');
$this->assertSession()->pageTextContains('Welcome');
}
/**
* @Then the response time should be under :ms milliseconds
*/
public function responseUnder(int $ms): void
{
// Custom performance assertion
}
}
内置 MinkContext 步骤
# Navigation
Given I am on "/path"
When I go to "/path"
When I reload the page
# Forms
When I fill in "field" with "value"
When I select "option" from "select"
When I check "checkbox"
When I uncheck "checkbox"
When I press "button"
When I attach the file "path" to "field"
# Assertions
Then I should see "text"
Then I should not see "text"
Then I should be on "/path"
Then the response status code should be 200
Then the "field" field should contain "value"
Then I should see an "css-selector" element
Then print current URL
behat.yml
default:
suites:
default:
contexts:
- LoginContext
- Behat\MinkExtension\Context\MinkContext
extensions:
Behat\MinkExtension:
base_url: 'http://localhost:3000'
sessions:
default:
selenium2:
browser: chrome
wd_host: 'http://localhost:4444/wd/hub'
标签
./vendor/bin/behat --tags=@smoke
./vendor/bin/behat --tags="@smoke&&~@slow"
设置:composer require --dev behat/behat behat/mink-extension behat/mink-selenium2-driver
初始化:./vendor/bin/behat --init
在 TestMu AI 上云端执行
设置环境变量:LT_USERNAME、LT_ACCESS_KEY
# behat.yml
default:
extensions:
Behat\MinkExtension:
base_url: 'https://your-app.com'
selenium2:
wd_host: 'https://hub.lambdatest.com/wd/hub'
capabilities:
browser: 'chrome'
extra_capabilities:
'LT:Options':
user: '%env(LT_USERNAME)%'
accessKey: '%env(LT_ACCESS_KEY)%'
build: 'Behat Build'
name: 'Behat Test'
platformName: 'Windows 11'
video: true
console: true
network: true
运行:./vendor/bin/behat 或 ./vendor/bin/behat features/login.feature
深入模式
生产级模式请参阅 reference/playbook.md:
| 章节 | 内容 |
|---|---|
| §1 项目设置 | 包含测试套件、Mink 扩展、配置文件和项目结构的 behat.yml |
| §2 功能文件 | 使用 Scenario Outline、Background 和 TableNode 数据的 Gherkin |
| §3 上下文类 | 步骤定义、依赖注入、API 上下文和断言 |
| §4 钩子 | BeforeSuite/Scenario/Step、失败时截取屏幕截图、回滚事务 |
| §5 页面对象 | 使用元素映射和可复用组件的页面对象模式 |
| §6 LambdaTest 集成 | 远程 Selenium 配置、云端浏览器配置文件 |
| §7 自定义格式化器 | HTML 报告格式化器、结果收集 |
| §8 CI/CD 集成 | 使用 MySQL、Selenium 和 JUnit 报告的 GitHub Actions |
| §9 调试表 | 12 个常见问题及其原因和解决方法 |
| §10 最佳实践 | 包含 14 项内容的 BDD 测试清单 |
评论
支持 Markdown