关于 behave-skill
用于使用 Python BDD 框架 Behave 编写浏览器测试的技能,涵盖 Gherkin 功能文件、步骤定义、钩子、标签、报告,以及 CI/CD 和调试参考资料。
主要功能
- 01Gherkin 功能文件与场景
- 02Python 步骤定义
- 03环境钩子与标签
- 04生成 JSON 报告
- 05CI/CD 与调试指南
适用场景
- 01编写登录流程的 BDD 场景
- 02组织可复用的 Python 步骤
- 03按标签运行指定场景
- 04生成 JSON 测试报告
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ lambdatest/ agent-skills --skill behave-skill安装后在 Claude Code 中输入/behave-skill确认已加载。
附带文件reference/advanced-patterns.mdreference/playbook.md
详细介绍
Behave BDD 技能
核心模式
Feature 文件(features/login.feature)
Feature: User Login
As a registered user
I want to log into the application
Background:
Given I am on the login page
Scenario: Successful login
When I enter "[email protected]" as email
And I enter "password123" as password
And I click login
Then I should see the dashboard
And the welcome message should say "Welcome"
Scenario: Invalid credentials
When I enter "[email protected]" as email
And I enter "wrong" as password
And I click login
Then I should see error "Invalid credentials"
Scenario Outline: Login with various users
When I enter "<email>" as email
And I enter "<password>" as password
And I click login
Then I should see "<result>"
Examples:
| email | password | result |
| [email protected] | admin123 | Dashboard |
| [email protected] | wrong | Error |
步骤定义(features/steps/login_steps.py)
from behave import given, when, then
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
@given('I am on the login page')
def step_on_login(context):
context.browser.get(context.base_url + '/login')
@when('I enter "{text}" as email')
def step_enter_email(context, text):
el = context.browser.find_element(By.ID, 'email')
el.clear()
el.send_keys(text)
@when('I enter "{text}" as password')
def step_enter_password(context, text):
el = context.browser.find_element(By.ID, 'password')
el.clear()
el.send_keys(text)
@when('I click login')
def step_click_login(context):
context.browser.find_element(By.CSS_SELECTOR, 'button[type="submit"]').click()
@then('I should see the dashboard')
def step_see_dashboard(context):
WebDriverWait(context.browser, 10).until(
EC.url_contains('/dashboard')
)
assert '/dashboard' in context.browser.current_url
@then('I should see error "{msg}"')
def step_see_error(context, msg):
error = WebDriverWait(context.browser, 5).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, '.error'))
)
assert msg in error.text
环境钩子(features/environment.py)
from selenium import webdriver
def before_all(context):
context.base_url = 'http://localhost:3000'
def before_scenario(context, scenario):
context.browser = webdriver.Chrome()
context.browser.implicitly_wait(10)
def after_scenario(context, scenario):
if scenario.status == 'failed':
context.browser.save_screenshot(f'screenshots/{scenario.name}.png')
context.browser.quit()
标签
@smoke
Feature: Login
@critical
Scenario: ...
behave --tags=@smoke
behave --tags="@smoke and not @slow"
安装:pip install behave selenium
运行:behave 或 behave features/login.feature
在 TestMu AI 上进行云端执行
设置环境变量:LT_USERNAME、LT_ACCESS_KEY
# environment.py
from selenium import webdriver
import os
def before_scenario(context, scenario):
lt_options = {
"user": os.environ["LT_USERNAME"],
"accessKey": os.environ["LT_ACCESS_KEY"],
"build": "Behave Build",
"name": scenario.name,
"platformName": "Windows 11",
"video": True,
"console": True,
"network": True,
}
options = webdriver.ChromeOptions()
options.set_capability("LT:Options", lt_options)
context.driver = webdriver.Remote(
command_executor=f"https://{os.environ['LT_USERNAME']}:{os.environ['LT_ACCESS_KEY']}@hub.lambdatest.com/wd/hub",
options=options,
)
报告:behave --format json -o report.json
深入模式
请参阅 reference/playbook.md,了解适用于生产环境的模式:
| 章节 | 内容 |
|---|---|
| §1 项目设置 | behave.ini、项目结构、依赖项 |
| §2 Feature 文件 | 使用 Scenario Outline、数据表和 Background 的 Gherkin |
| §3 步骤定义 | 类型注册、API 步骤、使用 PyHamcrest 的通用步骤 |
| §4 环境钩子 | before_all/scenario/feature、失败时截图、数据库隔离 |
| §5 页面对象 | 包含等待机制的 BasePage、LoginPage、可复用组件 |
| §6 固件与测试数据 | DatabaseHelper、事务回滚、JSON 数据加载器 |
| §7 LambdaTest 集成 | 远程浏览器创建、云端功能配置 |
| §8 CI/CD 集成 | 使用 Postgres、Selenium 和 Allure 报告的 GitHub Actions |
| §9 调试表 | 12 个常见问题及其原因和修复方法 |
| §10 最佳实践 | 包含 14 项的 BDD 测试清单 |
评论
支持 Markdown