Amazon Nova Act 浏览器自动化深度实践:pytest 集成 + 结构化数据提取 + CI/CD 落地

写 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 模型理解页面内容,根据自然语言指令操作浏览器——点击按钮、填表单、验证内容,都不需要写选择器。

环境准备

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. 设置环境变量:
export NOVA_ACT_API_KEY="your-api-key-here"

第一个 Nova Act 测试

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 配置

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

写测试用例

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

运行测试

pytest tests/ -v

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

结构化数据提取

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

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. 多平台发布验证

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

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. 竞品内容监控

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

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 自动查百度收录情况:

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

posted @ 2026-03-31 10:31  亚马逊云开发者  阅读(8)  评论(0)    收藏  举报