Amazon Nova Act 浏览器自动化测试实战:AI 驱动的端到端测试 + pytest 集成 + OpenClaw 场景落地

Amazon Nova Act 浏览器自动化测试实战:AI 驱动的端到端测试 + pytest 集成 + OpenClaw 场景落地

写 Selenium 测试写到怀疑人生?选择器一改全挂?试试 Amazon Nova Act------亚马逊云科技推出的 AI 浏览器自动化 SDK,用自然语言描述操作,AI 自动在浏览器里执行。

这篇从零开始:安装 Nova Act → 写第一个测试 → 集成 pytest 生成报告 → 实际落地到 OpenClaw Agent 管理场景。

Nova Act 是什么

Amazon Nova Act 是亚马逊云科技推出的浏览器自动化 SDK。和传统的 Selenium/Playwright 的区别:

对比 Selenium/Playwright Nova Act
操作方式 CSS/XPath 选择器 自然语言指令
维护成本 页面一改测试就挂 AI 自适应
学习门槛 需要了解 DOM 结构 说人话就行
复杂交互 需要大量代码 一句话搞定

核心原理:Nova Act 用 AI 模型理解页面内容,根据自然语言指令操作浏览器------点击按钮、填表单、验证内容,都不需要写选择器。

环境准备

bash 复制代码
pip install nova-act pytest pytest-html pytest-html-nova-act

获取 API Key:

  1. 访问 https://nova.amazon.com/act
  2. 登录 AWS 账号
  3. 生成 API Key
  4. 设置环境变量:
bash 复制代码
export NOVA_ACT_API_KEY="your-api-key-here"

第一个 Nova Act 测试

python 复制代码
from nova_act import NovaAct

# 创建会话
nova = NovaAct(
    starting_page="https://docs.openclaw.ai",
    headless=True
)
nova.start()

# 用自然语言操作浏览器
nova.act("点击 Install 链接")
nova.act("找到 Docker 安装方式并点击")
nova.act("验证页面上有 docker pull 命令")

nova.stop()

三行 act() 调用,替代了以前十几行的选择器代码。页面改版了?只要"Install 链接"文字还在,测试就不会挂。

集成 pytest

AWS 官方提供了 pytest-html-nova-act 插件,自动在测试报告里加入 Nova Act 的操作截图和元数据。

pytest 配置

ini 复制代码
# pytest.ini
[pytest]
addopts = --html=reports/report.html --self-contained-html --add-nova-act-report

写测试用例

python 复制代码
import pytest
from nova_act import NovaAct, BOOL_SCHEMA

@pytest.fixture()
def nova_session():
    nova = NovaAct(
        starting_page="https://docs.openclaw.ai",
        headless=True
    )
    nova.start()
    yield nova
    nova.stop()

def test_install_page_loads(nova_session):
    """测试安装页面是否正常加载"""
    nova_session.act("点击 Install 导航链接")
    result = nova_session.act(
        "页面上是否有安装命令?",
        schema=BOOL_SCHEMA
    )
    assert result.parsed_response is True

def test_quick_start_flow(nova_session):
    """测试快速开始流程"""
    nova_session.act("点击 Quick Start")
    nova_session.act("滚动到代码示例部分")
    result = nova_session.act(
        "页面上是否有 openclaw 命令?",
        schema=BOOL_SCHEMA
    )
    assert result.parsed_response is True

运行测试

bash 复制代码
pytest tests/ -v

生成的 HTML 报告里会自动包含每个 act() 调用的浏览器截图,方便排查问题。

结构化数据提取

Nova Act 不只能操作浏览器,还能提取结构化数据:

python 复制代码
from nova_act import NovaAct

SCHEMA = {
    "type": "object",
    "properties": {
        "version": {"type": "string"},
        "release_date": {"type": "string"},
        "features": {
            "type": "array",
            "items": {"type": "string"}
        }
    }
}

nova = NovaAct(starting_page="https://github.com/openclaw/openclaw/releases")
nova.start()

result = nova.act(
    "提取最新版本的版本号、发布日期和主要特性列表",
    schema=SCHEMA
)

print(result.parsed_response)
# {"version": "2026.3.28", "release_date": "2026-03-29", "features": [...]}

nova.stop()

OpenClaw 场景落地

Nova Act 在 OpenClaw 运营中有几个实际应用场景:

1. 多平台发布验证

发完文章后自动检查各平台是否发布成功:

python 复制代码
def test_csdn_article_published(nova_session):
    nova_session = NovaAct(
        starting_page="https://blog.csdn.net/u012345678",
        headless=True
    )
    nova_session.start()
    
    result = nova_session.act(
        "最新文章的标题是什么?",
        schema={"type": "object", "properties": {"title": {"type": "string"}}}
    )
    assert "OpenClaw" in result.parsed_response["title"]
    nova_session.stop()

2. 竞品内容监控

定期检查竞品博客的新内容:

python 复制代码
def monitor_competitor_blog():
    nova = NovaAct(starting_page="https://example-competitor.com/blog")
    nova.start()
    
    articles = nova.act(
        "列出最近 5 篇博客文章的标题和发布日期",
        schema={
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "title": {"type": "string"},
                    "date": {"type": "string"}
                }
            }
        }
    )
    nova.stop()
    return articles.parsed_response

3. SEO 收录检查

用 Nova Act 自动查百度收录情况:

python 复制代码
def check_baidu_index(keyword):
    nova = NovaAct(starting_page="https://www.baidu.com")
    nova.start()
    
    nova.act(f"在搜索框输入 '{keyword}' 并搜索")
    result = nova.act(
        "搜索结果第一页有多少条结果?前三条结果的标题是什么?",
        schema={
            "type": "object",
            "properties": {
                "count": {"type": "number"},
                "top_results": {"type": "array", "items": {"type": "string"}}
            }
        }
    )
    nova.stop()
    return result.parsed_response

和 Selenium/Playwright 的协作

Nova Act 不是替代 Selenium/Playwright,而是互补:

  • 简单交互、验证类:用 Nova Act,写起来快、维护成本低
  • 精确控制、性能敏感:用 Playwright,执行速度更快
  • 混合使用:Playwright 负责页面导航,Nova Act 负责复杂交互

踩坑记录

  1. API Key 有效期:Nova Act API Key 需要定期刷新
  2. headless 模式 :CI/CD 环境必须用 headless=True
  3. 指令要具体"点击按钮" 不如 "点击页面上的蓝色提交按钮"
  4. 超时处理:复杂页面加载慢时需要合理设置超时

Amazon Nova Act:https://nova.amazon.com/act

pytest-html-nova-act:https://github.com/aws/pytest-html-nova-act

Nova Act 文档:https://docs.aws.amazon.com/nova/latest/userguide/nova-act.html

OpenClaw:https://github.com/openclaw/openclaw

相关推荐
深圳市九鼎创展科技4 分钟前
MT8883 vs RK3588 开发板全面对比:选型与场景落地指南
大数据·linux·人工智能·嵌入式硬件·ubuntu
CareyWYR7 分钟前
AI Coding 订阅的集体退潮:从狂欢到收紧,中间只隔了一个季度
人工智能
NineData13 分钟前
NineData 亮相香港国际创科展 InnoEX 2026,以 AI 加速布局全球市场
运维·数据库·人工智能·ninedata·新闻资讯·玖章算术
IT_陈寒30 分钟前
Vite的热更新突然失效,原来是因为这个配置
前端·人工智能·后端
威迪斯特1 小时前
AI智能分析系统在展厅的应用解决方案
人工智能·人脸识别·降本增效·算法分析·展厅·aibox·边缘分析
量子猫AI1 小时前
openclaw常用Skill分享
人工智能
peterfei2 小时前
若爱 IfAI v0.4.2 发布:技能市场上线,重新定义 AI 编辑器的可扩展性
人工智能·开源
阿杰学AI2 小时前
AI核心知识129—大语言模型之 向量数据库(简洁且通俗易懂版)
数据库·人工智能·ai·语言模型·自然语言处理·向量数据库·vector database
PILIPALAPENG2 小时前
第3周 Day 2:Function Calling —— 让 Agent 听懂人话,自己干活
前端·人工智能·python
阿里云大数据AI技术2 小时前
PAI Physical AI Notebook详解8:Isaac Lab Arena 全身机器人机动+操控工作流
人工智能