基于BDD和页面对象模型的Selenium MCP服务器测试自动化
项目描述
此项目使用 Selenium MCP服务器 使用以下命令创建测试自动化 行为驱动开发(BDD), python,以及 页面对象模型(POM) 图案。该系统能够仅使用特征文件中描述的步骤构建完整的自动化,并能够自动修复损坏的场景。
主要特点
- 🤖 智能自动化:从自然语言描述生成自动化代码(Gherkin)
- 🔧 汽车修理:能够检测并自动修复失败的测试场景
- 📋 BDD与行为:使用Python的Behave框架进行测试实现
- 🏗️ 页面对象模型:测试架构遵循POM模式,以提高可维护性
- 🔌 MCP集成:使用Selenium MCP服务器通过MCP工具与浏览器交互
视频演示
您可以在以下视频中看到该项目如何运作的一个实际示例:

直接链接: 在YouTube上观看视频
视频显示了一个简短的示例,说明如何使用Selenium MCP服务器创建BDD和页面对象模型模式的自动化。
项目架构
mcp-qa-server-python/
├── features/ # BDD feature files (Gherkin)
│ ├── sign_in.feature # Example feature for registration on utest.com
│ └── steps/ # Step definitions
├── pages/ # Page Objects following the POM pattern
│ └── base_page.py # Base class for all page objects
├── selenium_mcp_server/ # MCP server references
├── requirements.txt # Project dependencies
├── behave.ini # Behave configuration
└── README.MD # This file使用的技术
- python:主要编程语言
- 硒:Web自动化框架
- 表现:Python的BDD框架
- WebDriver管理器:自动浏览器驱动程序管理
- MCP(模型上下文协议):与Selenium服务器通信的协议
安装与配置
先决条件
- Python 3.7或更高版本
- 已安装Chrome浏览器
- 通过互联网下载驱动程序
安装步骤
- 克隆存储库 (如适用):
git clone
cd mcp-qa-server-python- 创建虚拟环境 (推荐):
python -m venv venv- 激活虚拟环境:
- 窗户:
venv\Scripts\activate- Linux/Mac:
source venv/bin/activate- 安装依赖项:
pip install -r requirements.txt依赖项
该项目需要以下依赖项(请参见 requirements.txt):
behave==1.2.6:Python的BDD框架selenium==4.15.2:Web自动化框架webdriver-manager==4.0.1:自动驾驶员管理
运作原理
工作流程
自动化过程遵循以下步骤:
- 场景定义A.
.feature文件是用Gherkin语言创建的测试场景
- 使用MCP服务器执行:MCP LLM客户端(游标、Visual Studio代码等)使用Selenium_MCP_Server工具来:
- 打开浏览器 - 导航到URL - 与元素交互(单击、键入、选择) - 验证页面状态
- 代码生成:LLM检索使用的选择器并生成:
- 页面对象:表示遵循POM模式的网页的类 - 步骤定义:要素文件中定义的步骤的实现
- 执行和验证:使用以下命令执行测试:
behave features/sign_in.feature- 汽车修理:如果测试失败,系统会尝试自动修复场景,直到它正常工作
用法示例
特征文件(Gherkin)
Feature: Register on the utest.com website
@TC-01
Scenario: Fill in all the required fields for step 1
Given I open de browser
When I navigate to "utest.com"
And I click on the "Join Now" button
Then I wait for the page "https://utest.com/signup/personal" to load
When I fill the fields of the form
| type | field_name | value |
| input | First name: | Ruben |
| input | Last name: | Smith |
| input | Email address: | rsmith@example.com |
| select | Month | February |
| select | Day | 5 |
| select | Year | 2000 |
Then I valid that all fields have been filled correctly
When I click on the "Next: Location" button
Then I wait for the page "https://utest.com/signup/address" to load提示LLM
Execute the steps described in the sign_in.feature file in Chrome using selenium-mcp-server and create the step definitions using the selectors you use to follow them; create the necessary page objects following the Page Object Model pattern.
After creating the step definitions and page models, run the command behave features/sign_in.feature to test the correct operation of the created code. If it fails, repair the test.页面对象结构
该项目使用页面对象模型模式来组织代码:
基础页面
这 BasePage 类为所有页面对象提供通用功能:
wait_for_url():等待URL与预期的URL匹配wait_for_element():等待元素出现wait_for_element_clickable():等待元素可点击
所有页面对象都继承自 BasePage 以保持一致性和代码可重用性。
测试执行
运行特定功能
behave features/sign_in.feature与Selenium MCP服务器集成
该项目使用 Selenium MCP服务器 其提供MCP工具用于:
- 启动和控制浏览器
- 导航到URL
- 与页面元素交互
- 捕获屏幕截图
- 验证元素是否存在
- 以及许多其他网络自动化操作
这种方法的优点
- 可维护性:代码按照既定模式(POM)组织
- 可读性:测试是用自然语言(Gherkin)编写的
- 可重复使用性:页面对象可以在多个测试中重用
- 智能自动化:LLM可以自动生成和修复代码
- 可扩展性:易于添加新功能和场景
