robotframework-aitester---向robotframework中引入大模型能力

1.简介

robotframework-aitester 是一个用于跨Web、API 和移动端的自主 AI 驱动测试的 Robot Framework 库。通过结合 Strands Agents SDK 与原生 Robot Framework 库的复用,它允许测试人员指定测试内容,而不是手动编写每个交互细节。

 

2.安装(Python >=3.10,robotframework>=6.0)

# 基础安装
# 包括 OpenAI-, Gemini-, and Ollama-compatible model support
pip install robotframework-aitester

# 支持 web 测试
pip install robotframework-aitester[web]

# 支持 API 测试
pip install robotframework-aitester[api]

# 支持 mobile 测试
pip install robotframework-aitester[mobile]

# 支持所有测试模式
pip install robotframework-aitester[all]

# 支持 Bedrock
pip install robotframework-aitester[all,bedrock]

# 支持 Anthropic
pip install robotframework-aitester[all,anthropic]

# Development
pip install robotframework-aitester[all,anthropic,bedrock,dev]

 

推荐的生产环境安装:

pip install robotframework-aitester[web]     基于Selenium的UI测试
pip install robotframework-aitester[api]     基于RequestsLibrary的API测试
pip install robotframework-aitester[mobile]     基于Appium的移动端测试

基础安装已通过 strands-agents[openai,ollama,gemini] 包含 OpenAI、Gemini 和 Ollama 提供者的支持
仅在您选择的 GenAI 后端需要这些可选提供者时,才添加 [anthropic] 或 [bedrock]

 

3.快速入门

前置条件

  • 对于确定性网页运行,在使用 SeleniumLibrary 调用 Run AI Test 或 Run AI Exploration 之前,需先打开目标浏览器。
  • 对于确定性移动端运行,在使用 AppiumLibrary 调用 Run AI Mobile Test 或 Run AI Exploration 之前,需启动 Appium 服务器、设备或模拟器,并打开应用程序。
  • 对于 API 运行,需在测试套件中加载 RequestsLibrary,并在适当情况下提供 base_url 或已初始化的会话上下文。
  • 安装针对目标模式和提供者的必要扩展。
  • 如果 SeleniumLibrary、RequestsLibrary 或 AppiumLibrary 使用别名导入,请传递相应的 selenium_library 、 requests_library 或 appium_library 构造函数参数,以便 AITester 可以附加到现有会话。

 

Web 测试

*** Settings ***
Library    SeleniumLibrary
Library    AITester    platform=OpenAI    api_key=%{OPENAI_API_KEY}    model=gpt-4o

*** Test Cases ***
AI Login Flow Test
    [Documentation]    AI agent autonomously tests the login functionality
    Open Browser    https://myapp.example.com    chrome
    ${TEST_STEPS}=    Set Variable
    ...    Test Steps:
    ...    1. Open the login page
    ...    2. Attempt login with valid credentials and verify success
    ...    3. Attempt login with invalid credentials and verify error message
    ${status}=    Run AI Test
    ...    test_objective=Test the login functionality including valid credentials,
    ...        invalid credentials, empty fields, and password recovery flow
    ...    app_context=E-commerce web application with email/password login
    ...    test_steps=${TEST_STEPS}
    ...    max_iterations=50
    Log    ${status}
    [Teardown]    Close All Browsers

AI Exploratory Testing
    [Documentation]    AI agent freely explores and tests the application
    Open Browser    https://myapp.example.com    chrome
    ${status}=    Run AI Exploration
    ...    app_context=E-commerce platform with product catalog, shopping cart and checkout
    ...    focus_areas=navigation, search, product filtering, cart operations
    ...    max_iterations=100
    Log    ${status}
    [Teardown]    Close All Browsers
  由 Open Browser 打开的浏览器会被代理重用。如果会话已经处于活动状态,AITester 将重用它并拒绝打开新的会话。
  当提供编号为 test_steps 的步骤时,这些步骤被视为主流程,并按顺序直接执行,无需单独的规划交接。
  这些步骤被视为意图检查点,而不是像素级完美的脚本。只要请求的业务流程保持完整,代理可能会插入最小的支持操作,例如关闭 Cookie 横幅、打开菜单、等待页面稳定或遇到临时拦截器后重试。
如果流程遇到硬性障碍,代理不会停止并等待人工干预。它会首先尝试通过 get_rf_variable 获取套件提供的数据、安全的替代可见路径以及证据捕获,然后精确地失败被阻塞的步骤。
 

API 测试

*** Settings ***
Library    RequestsLibrary
Library    AITester    platform=Ollama    model=llama3.3

*** Test Cases ***
AI REST API Test
    Create Session    api    https://api.example.com
    ${TEST_STEPS}=    Set Variable
    ...    Test Steps:
    ...    1. Create a user via POST /users
    ...    2. Fetch the user via GET /users/{id}
    ...    3. Update the user via PUT /users/{id}
    ...    4. Delete the user via DELETE /users/{id}
    ${status}=    Run AI API Test
    ...    test_objective=Test the user management API endpoints including
    ...        CRUD operations, authentication, error handling, and edge cases
    ...    base_url=https://api.example.com
    ...    api_spec_url=https://api.example.com/openapi.json
    ...    test_steps=${TEST_STEPS}
    ...    max_iterations=30
    Log    ${status}

 

Mobile 测试

*** Settings ***
Library    AppiumLibrary
Library    AITester    platform=Gemini    api_key=%{GEMINI_API_KEY}

*** Test Cases ***
AI Mobile App Test
    Open Application    http://localhost:4723/wd/hub
    ...    platformName=Android    app=com.example.app
    ${TEST_STEPS}=    Set Variable
    ...    Test Steps:
    ...    1. Complete the onboarding flow
    ...    2. Navigate to the main dashboard
    ...    3. Open settings and verify key options
    ${status}=    Run AI Mobile Test
    ...    test_objective=Test the onboarding flow, main navigation and settings screen
    ...    app_context=Android banking application
    ...    test_steps=${TEST_STEPS}
    ...    max_iterations=40
    Log    ${status}
    [Teardown]    Close Application
  对于移动端测试,AITester 需要一个活跃的 AppiumLibrary 会话,并且当 app_context 和编号的 test_steps 能够明确目标屏幕、账户状态和预期路径时,效果最佳。现在它能够等待加载指示器、处理常见的选择器、隐藏屏幕键盘、切换混合上下文,并使用返回导航,而无需降级到原始的 Appium 命令。
  对于移动探索性测试,导入库时使用 test_mode=mobile ,然后调用 Run AI Exploration 。
 

4.支持的 AI 平台

Platform
Default Model
Provider
Notes
OpenAI
gpt-4o
OpenAI API
需要OPENAI_API_KEY
Ollama
llama3.3
本地 Ollama
免费,本地推理
Docker Model
ai/qwen3-vl:8B-Q8_K_XL
本地 Docker 模型运行器
免费,本地推理
Gemini
gemini-2.0-flash
Google AI
需要GEMINI_API_KEY
Anthropic
claude-sonnet-4-5
Anthropic API
需要ANTHROPIC_API_KEY
Bedrock
us.anthropic.claude-sonnet-4-5-20251101-v1:0
AWS Bedrock
使用 AWS 凭证
Manual
用户指定
OpenAI 兼容
使用您自己的兼容端点;通常与显式的model和base_url配合使用

 

模型参数

Parameter
Default
Description
platform
OpenAI
AI 平台(OpenAI、Ollama、Gemini 等)
model
(varies)
模型 ID
api_key
(env var)
API 密钥;对于 Docker 模型始终使用 dummy ,因此会被忽略
base_url
(varies)
AI 提供者基础 URL
max_iterations
50
最大 agent 迭代次数
test_mode
web
默认测试模式(web、api、mobile)
headless
False
作为配置元数据存储;浏览器/应用启动仍由 SeleniumLibrary/AppiumLibrary 管理
screenshot_on_action
True
保留用于未来截图策略调整;当前提示/工具调用仍决定何时进行截图
verbose
False
启用详细代理日志记录
selenium_library
SeleniumLibrary
SeleniumLibrary 已有会话的名称/别名
requests_library
RequestsLibrary
RequestsLibrary 已有会话的名称/别名
appium_library
AppiumLibrary
AppiumLibrary 已有会话的名称/别名
timeout_seconds
600
配置 SafetyGuard 超时元数据
max_cost_usd
None
配置 SafetyGuard 成本限制元数据
  如果你使用别名导入 SeleniumLibrary/RequestsLibrary/AppiumLibrary,请传递相应的 *_library 参数,以便 AI 工具连接到已打开的会话。
  对于 platform=DockerModel ,AITester 会自动将 api_key=dummy 传递给兼容 OpenAI 的 Strands 客户端。对于该平台,无需设置环境变量或构造函数参数。
  对于 platform=Manual ,请自行提供与 OpenAI 兼容的端点详细信息,通常需要同时包含 model= 和 base_url= ,如有需要,还需包含 api_key= 。
  重要提示:AITester 只能驱动由 SeleniumLibrary/AppiumLibrary 创建的会话。如果你手动打开浏览器/应用或通过其他工具打开,代理将无法与其交互。

 

5.关键字

Keyword
Description
Run AI Test
从测试目标执行自主测试(支持test_steps,scroll_into_view)
Run AI Exploration
使用库中配置的test_mode(支持scroll_into_view)进行探索性测试,并关注重点区域
Run AI API Test
执行自主 REST API 测试(支持test_steps,scroll_into_view)
Run AI Mobile Test
执行自主移动应用测试(支持test_steps,scroll_into_view)
Get AI Platform Info
返回配置的平台信息
AI Step
由 agent tools 使用的步骤级日志关键字
AI High Level Step
用于 RF 日志分组的通用步骤标记
 

6.优势

  • 可以以传统 SeleniumLibrary、RequestsLibrary、AppiumLibrary 为主,辅以 AI 能力,在不改变已有脚本的情况下加入 AI 能力
  • 定位器易写的元素仍以常规方式编写,不易写的定位器可以使用 AI 能力辅助

 

posted @ 2026-04-21 10:42  TotheAnts  阅读(52)  评论(0)    收藏  举报