序言
在软件测试领域,我们正站在一个历史性的转折点。传统测试方法的局限性日益凸显,而人工智能技术的飞速发展为测试行业带来了前所未有的机遇。本文将深入探讨智能体驱动测试,如何重新定义测试的本质,以及为什么智能体应该成为测试的第一性原理。
第一部分:理念与变革
传统测试的困境:一个无法持续的现状
当前,大多数QA团队面临着令人沮丧的现实:60-80%的测试自动化时间花费在维护上,而不是发现缺陷或提高质量。测试脚本过于脆弱,哪怕界面有细微改动,也可能导致大面积失效。团队陷入了”写脚本-看失败-修脚本”的无尽循环,这种模式不仅效率低下,更是对人力资源的巨大浪费。
更严重的是,传统测试方法建立在一系列错误假设之上:界面是稳定的、元素定位器是可靠的、测试环境是一致的。在敏捷开发和持续交付的时代,这些假设都不复存在。我们需要一种全新的测试范式,一种能够适应变化、理解意图、自主学习的测试方法。
智能体驱动测试:重新定义测试的本质
智能体驱动测试的核心在于将测试从”指令执行”转变为”智能行为”。这不是简单的AI辅助测试,而是让AI成为测试的主体,具备以下特征:
自主决策能力:智能体能够设定子目标,自主决定如何响应变化,而不是被动等待指令。当遇到UI变化时,它能够分析情况,选择最佳适应策略。
上下文理解:智能体理解测试的业务背景,知道为什么这个测试重要,它验证的是什么业务功能。这种理解使它能够做出符合业务目标的决策。
持续学习:每个测试执行都是学习机会。智能体从成功和失败中积累经验,不断优化测试策略和方法。
协作智能:智能体不是替代人类,而是与人类形成协作关系。它处理重复性、技术性工作,让人类专注于战略性、创造性工作。
自愈合测试:技术奇迹背后的原理
自愈合测试可能是智能体驱动测试最引人注目的特性。它如何解决测试维护的痛点?关键在于多层级的智能识别系统:
多模态元素识别:结合视觉识别、语义分析、DOM结构分析和文本内容分析,智能体能够像人类一样”理解”界面元素。它不再寻找”#login-button”,而是理解”认证区域的主要操作按钮,用于用户访问系统”。
智能回退策略:当主要定位策略失败时,智能体启动多层次回退机制:上下文感知的备用定位器、视觉相似性匹配、语义角色识别等。这种冗余设计确保了高可靠性。
预测性适应:通过分析历史变更模式,智能体能够预测可能的变更并提前准备适应策略。它学会了开发团队的编码习惯和变更倾向。
自然语言编程:测试的民主化革命
用自然语言编写测试不仅是技术突破,更是组织变革的催化剂。当产品经理能够直接创建验收测试,当业务分析师能够自动化业务流程验证,测试从专业技术活动转变为团队协作过程。
这种民主化带来了几个关键优势:
知识保真度:业务专家的直接参与消除了需求传递过程中的信息损失。测试准确反映了业务意图,而不是技术人员的理解。
覆盖完整性:不同视角的团队成员能够识别不同的测试场景。技术团队可能忽略的业务边界情况,业务团队能够及时发现。
响应速度:当业务需求变化时,相关测试可以立即更新,无需等待技术团队排期。
智能体成为测试第一性的重要意义
将智能体作为测试的第一性原理,意味着我们承认几个根本性的转变:
从确定性到概率性:传统测试追求确定性结果 – 相同的输入永远产生相同的输出。智能体驱动测试接受概率性现实 – 在复杂系统中,完美预测是不可能的,但可以通过学习和适应达到高可靠性。
从静态到动态:测试不再是静态的脚本集合,而是动态的智能系统。它能够感知环境变化,调整自身行为,在不断变化的世界中保持有效性。
从孤立到集成:测试不再是开发流程的独立阶段,而是集成到整个软件生命周期的智能能力。它存在于设计、开发、部署、监控的每个环节。
从工具到伙伴:AI不是被动工具,而是主动的合作伙伴。它有”主见”,能提出建议,有时甚至会说”不”。这种关系重新定义了人机协作的边界。
实施路径:从理论到实践
拥抱智能体驱动测试需要系统性的方法:
认知转变:首先,团队必须接受测试本质的变化。这不是工具的升级,而是范式的转换。管理者需要理解,投资AI测试不是成本,而是战略能力。
技能重构:传统自动化工程师需要学习AI协作技能 – 如何训练智能体、如何解释AI决策、如何优化AI性能。同时,非技术人员需要学习如何有效地与AI沟通。
渐进实施:从低风险、高价值的场景开始,逐步扩展AI测试的范围。让成功驱动进一步的采用,而不是试图一次性替换所有现有测试。
持续优化:AI系统需要持续调优和训练。建立反馈循环,让AI从每次执行中学习,不断提高准确性和效率。
未来展望:测试的终结与质量的开始
智能体驱动测试的发展指向一个更宏大的愿景:测试作为一个独立学科的终结,以及质量作为内在属性的开始。
当AI能够持续、自主、智能地验证软件行为时,”测试”不再是一个单独的活动。它成为软件的一部分,像免疫系统一样持续工作,识别问题、适应变化、自我修复,人类测试专家的角色将演变为:
质量架构师:设计整体质量策略,定义什么是”好”,建立质量标准和评估框架。
AI训练师:教导AI系统理解业务上下文,识别关键风险,做出符合组织价值的决策。
创新策动者:从日常测试工作中解放出来,专注于探索新的测试方法、识别新兴风险类型、推动质量实践的创新。
第二部分:技术与落地
AgentCore Browser Tool:为智能体而生的浏览器
传统的浏览器自动化需要复杂的基础设施管理、安全防护和开发专业知识。
而全托管的云端浏览器自动化解决方案通过简化基础设施管理、提供企业级安全、支持全球可用与弹性扩展,并实现成本优化,解决了这些关键问题。
企业无需再自行部署、维护和扩展浏览器实例,AgentCore 负责底层基础设施,使开发者专注于智能代理能力构建。
云端浏览器自动化运行在隔离环境中,并受 AgentCore 安全控制保护,降低数据外泄和未授权访问风险。
依托 AgentCore 全球基础设施,可即时部署浏览器实例以实现自动化的全球扩展。
通过托管服务模式,企业可采用按使用量计费,避免维持常开式基础设施,从而显著降低间歇性任务成本。
基于 AgentCore Browser Tool 在测试与质量保障中的应用场景
1. 自动化回归与重复性验证测试
随着 AgentCore Browser Tool 的推出,企业如今能够在大规模上实现智能化浏览器测试自动化。
云端浏览器测试自动化特别适用于减少跨 Web 界面的重复性验证任务。
AI 测试代理可在多个系统中自动填写复杂表单、校验输入内容的正确性与一致性,并验证其是否符合业务规则。
这些代理还可自动访问内部仪表盘,提取关键性能指标并生成质量报告,无需人工干预。
对于依赖用户生成内容的平台,AI 测试代理还可在多平台上执行内容质量与合规性预检,从而提升整体数据可靠性。
2. 智能监测与测试数据采集
借助云端浏览器自动化,AI 测试代理可成为持续监测与质量验证的智能助手。
它们可自动跟踪目标网站的价格变化、新产品发布或内容更新,从测试角度验证系统监控机制的准确性与实时性。
企业可利用这些代理在论坛、评论网站和社交媒体上采集用户反馈数据,用于测试市场响应和用户体验质量。
通过 AgentCore Browser Tool,还能构建自动化验证系统,定期扫描可信信息源,确保内部知识库或推荐算法的测试数据始终最新、可靠。
3. 跨系统一致性与集成测试
许多企业依赖的 Web 应用之间缺乏统一的工作流,这给集成测试带来了挑战。
在 API 不可用的情况下,可使用 AgentCore Browser Tool 实现跨多个系统的端到端流程验证,自动化地执行用户创建、配置与权限验证测试。
测试代理可实时监控供应商门户、库存系统和物流服务,验证系统间数据同步与接口稳定性。
通过在多个内部系统间自动化执行测试场景,企业能够持续验证业务流程一致性,降低因人工测试遗漏导致的错误风险。
4. 用户体验与性能质量测试
基于云的浏览器自动化可在大规模上实现全面的端到端测试。
利用 AgentCore Browser Tool,可在不同场景、设备与浏览器上并行验证前端交互性能与用户体验一致性。
部署 AI 测试代理可持续监控关键业务系统,主动发现性能瓶颈并向团队发出警报,防止用户体验劣化。
此外,工具还能定期执行无障碍性测试、安全漏洞扫描与法规合规性验证,确保系统满足企业级质量标准。
5. 旧系统兼容性与可访问性测试
许多企业仍运行缺乏现代 API 的遗留系统,这对测试提出了特殊要求。
通过 AgentCore Browser Tool,AI 测试代理可在无需修改系统的前提下与这些旧式 Web 应用交互,从而执行自动化兼容性与稳定性测试。
这种方式使企业能够验证旧系统在接入新流程或前端界面时的响应行为,确保其在整体架构中仍保持可用与安全。
同时,定期执行的自动化测试还能提取旧系统中的关键数据,用于分析质量趋势并验证数据一致性。
AgentCore Browser Tool 的核心能力
AgentCore Browser Tool 使 AI 代理能够以人类方式与网页内容交互。它提供一套全托管的远程浏览器基础设施,既减少了传统部署的复杂性,也确保企业级的安全与可扩展性。
网页交互能力
- 支持在多个页面和复杂导航工作流中的完整导航控制;例如从首页跳转、分页浏览、按需加载内容等。
- 能够与大量 JavaScript 的动态内容交互,例如现代单页应用(SPA)或组件动态更新内容的页面;也支持异步内容加载的监测与处理。
- 表单操作能力完善,包括输入文本、选择下拉菜单选项、文件上传等;代理可以填写表单并提交验证。
- 模拟人类互动的行为,例如滚动页面、悬停、点击各种元素等,以便触发页面上的动态效果或懒加载内容。
无服务器浏览器基础设施
- 完全零运维的浏览器集群,自动处理补丁更新、安全性更新等基础设施方面的运维。
- 可根据需求从单个会话扩展到成千上万会话,无需预先固定容量。
- 全球部署选项,并且计费模式是按使用量,即只为实际消耗的资源付费,无需为闲置资源承担成本。
- 性能经过优化,不需要用户自己布置或管理底层基础设施所带来的负担。
视觉理解能力
- 支持整页截图,让 AI 能够“看到”整个页面布局与内容,便于判断布局、结构、可见性等问题。
- 可识别视觉元素,根据其外观与在页面中的位置来判断(例如按钮、图像、图标等)。
- 从图形元素中提取内容,例如从图像或图表中获取数据,或从屏幕快照中识别文字/元素。
- 支持不同分辨率与设备仿真,以测试网页在不同设备上的显示与互动效果。
人机协作式整合
- 提供实时的可视化视图,让人工操作员能观察或者在必要时介入并控制当前会话,以便调试、监督或处理异常情况。
- 会话录制功能,用于回顾、培训、审核与合规检查,确保每次交互可追踪、可复盘。
企业级安全
- 每个浏览器实例都被隔离,确保一个代理/一个任务的操作不会泄露到其他任务或造成资源共享风险。
- 通过 AWS 身份与访问管理(IAM)控制谁可以使用这些浏览器工具或访问哪些资源。
- 每次使用后浏览器状态会被重置,以防止残留数据或状态被滥用。
复杂 Web 应用支持
- 完全兼容现代 JavaScript 框架,如 React、Vue、Angular 等。这样前端高度依赖 JS 的 SPA 应用也能被正确交互与测试。
- 支持身份验证与会话持久化,使得在带登录或复杂权限管理的应用中,也能保持状态跨页面跳转。
- 处理异步内容和实时更新,例如 WebSockets、AJAX 请求或动态加载组件等,代理需要等待、监听这些更新才能正确获取或验证内容。
- 识别并与复杂 UI 模式交互,例如拖放、模态对话框、多层次菜单、悬浮菜单等用户交互丰富的界面。
审计与合规性
- 详细记录所有的交互日志与会话 ,保证每一步操作都有记录,便于追踪问题或审计责任链。
- 与 AWS CloudTrail 集成,实现对工具使用、会话创建与访问的全面追踪与监控。
可观测性
- 提供性能指标(例如延迟、资源使用率等),帮助团队掌握自动化任务的运行效率与瓶颈。
- 与 Amazon CloudWatch 集成,能统一监控代理启动、执行、错误率等关键指标。
- 支持会话记录与回放功能,可以重现某些测试场景以便调试、验证或展示给别人。
这组能力真正填补了 AI 代理与为人类设计的 Web 内容交互之间的空缺:AI 不再仅限于调用开放 API 或处理静态内容,而是能够处理完整的用户型 Web 应用,理解用户界面、处理动态内容、模拟人类行为并且在严格安全、合规的框架下操作。对企业来说,这意味着可以:
- 在测试、监控、质量保证等环节中,实现更广泛、更可靠的自动化覆盖;
- 在“API 不存在/API 不足”的情况下,也能对系统进行端到端测试与验证;
- 减少因环境差异、界面变更或 JS 动态效果导致的问题缺陷;
- 更快把试验/原型项目推进到生产/规模化;
- 同时不牺牲安全性、审计与合规性要求。
AI 代理如何使用 AgentCore Browser Tool
1. 用户输入自然语言指令
比如 “去亚马逊搜索鞋子”
2. 指令传递给 Agentic 框架
这里可以用 Amazon Nova Act 或 Browser Use。
- 这些框架的作用是:接收自然语言,把它交给 大语言模型 (LLM) 来“思考”。
- 你可以理解为它们是“中间层”,负责协调 LLM 和浏览器。
3. 大语言模型推理
- LLM 接收到指令(例如“搜索鞋子”),它会推理:
- 然后 LLM 不会直接控制浏览器,而是生成一个 结构化的动作计划,例如:
[
{"action": "goto", "url": "https://www.amazon.com"},
{"action": "type", "selector": "input[name='field-keywords']", "value": "shoes"},
{"action": "click", "selector": "input[type='submit']"}
]
4. Agentic 框架把计划翻译成浏览器命令
- Nova Act 或 Browser Use 把 JSON 里的动作,映射成 浏览器自动化命令(Playwright / Puppeteer / Selenium)。
- 例如:
goto → playwright.page.goto("https://www.amazon.com")
type → playwright.page.fill("input[name='field-keywords']", "shoes")
click → playwright.page.click("input[type='submit']")
5. 命令通过安全通道执行
- 这些浏览器自动化命令会通过 安全的 WebSocket 连接 发给 AgentCore Browser。
- AgentCore Browser 是运行在 隔离容器里的无头浏览器,它和本地机器隔离,保证安全。
6. 浏览器执行动作并返回反馈
- 浏览器会打开网站,执行搜索操作。
- 执行完后,它会返回:
- 状态反馈(例如页面加载完成、元素找到/没找到)
- 截图(用于让 AI 直观看页面变化)
- 页面内容快照(HTML 片段等,方便后续推理)
7. LLM 再次推理
- LLM 根据返回的信息继续推理:
- 如果搜索成功,就提取结果
- 如果失败(比如网络问题或按钮没找到),就调整动作再试
- 这个 循环会持续,直到完成原始任务(比如获取前 10 个鞋子搜索结果)。
8. 最终结果返回给用户
- 完成任务后,Agent 把整理好的结果返回给用户(比如鞋子列表)。
- 用户看到的就是 “已为你搜索到 10 双鞋子,以下是价格和链接。”
用户用自然语言提需求 → LLM 把需求转化为结构化动作 → 框架把动作转译成浏览器命令 → 命令在安全容器里执行 → 执行结果(页面反馈、截图)返回给 LLM → 循环推理直到任务完成 → 最终结果返回用户。
第三部分:测试场景
环境准备
pip install bedrock-agentcore
pip install playwright
pip install nova-act
在https://nova.amazon.com/act生成API密钥
场景示例
利用 Nova Act 的结构化数据提取能力,使用自然语言指令测试智能信息检索、校验与结构化输出
from nova_act import NovaAct
from pydantic import BaseModel
class BookInfo(BaseModel):
title: str
author: str
price: str
class searchList(BaseModel):
searchResult: list[BookInfo]
with NovaAct(starting_page="https://www.google.com") as nova:
nova.act("search for amazon nova act announcing news")
result = nova.act(
"Return search results with amazon nova act announcing news with their titles, date, and definition",
schema=searchList.model_json_schema()
)
if result.matches_schema:
search_list = searchList.model_validate(result.parsed_response)
for search in search_list.searchResult:
print(f"Title: {search.title}, Date: {search.date}, Definition: {search.definition}")
这段代码有如下步骤:
- 启动 Nova Act 浏览器会话,从 Google 首页开始。
- 第一次 act:执行搜索(输入关键词并获取结果页)。
- 第二次 act:让代理以结构化 JSON 返回“搜索结果”列表,并要求符合 Pydantic 模型的 JSON Schema。
- 如果返回结果能匹配 schema,就用 Pydantic 校验并打印每条结果。
后台 Thinking+Action 输出如下:
a17a> think("I am on the google search page. My task is to search for amazon nova act anouncing news. I see an empty search bar where I can type queries to search for information. I should type the query 'amazon nova act anouncing news' into the search bar.");
>> agentType("amazon nova act anouncing news", " <box>241,418,286,867</box> ");
...
a17a> think("I am on the same page and the text value 'amazon nova act anouncing news' is populated in the search field but I did not press the enter key to initiate the search, therefore my last action was a mistake. I need to complete the search, but since the search field is populated I don't want to type and press the enter key again, I see a magnifying glass search button that could complete the search. I should click the magnifying glass search button that should complete the search.");
>> agentClick(" <box>241,379,286,418</box> ");
...
a17a> think("I am on the same page and the search has not been initiated, meaning my earlier action was unsuccessful. I see a dropdown opened from the search bar and it shows suggested results. I can see the first suggested result as 'announcing newsletter'. I should click on the first suggested result to initiate my search.");
>> agentClick(" <box>289,537,327,759</box> ");
...
a17a> think("The page loaded and I can see the results, meaning my earlier action was successful. I can see the first result as the AI overview and the second result is the news article. I should click on the second result to open the news article.");
>> agentClick(" <box>633,170,662,690</box> ");
...
a17a> think("A new page loaded up with the title 'Accelerate AI agent development with the Nova Act IDE extension', meaning my earlier action was successful. I can see the article details and the definition of the Nova Act. I should return the title, date and the definition of the Nova Act. My task to search for the amazon nova act anouncing news is complete and I should return the title, date and the definition of the Nova Act.");
>> return "Title: Accelerate AI agent development with the Nova Act IDE extension, Date: 23 Sep 2025, Definition: The Nova Act extension is a tool that streamlines the path to build browser automation agents without leaving your IDE. The Nova Act extension integrates directly into IDEs like Visual Studio Code (VS Code), Kiro, and Cursor, helping you to create web-based automation agents using natural language with the Nova Act model.";
a17a> ** View your act run here: /var/folders/dt/dgrd5y515c73hssfz9ky14n40000gr/T/tmp2k7tky83_nova_act_logs/a17a5a8f-9eab-4c69-8f4b-ae8fd30b088e/act_f12d490e-5ce9-477e-846d-a3b045f878fe_from_nova_act_import_NovaAct_f.html
end session:
Nova Act流程拆解:
- 感知:识别自己位于 Google 搜索页,发现搜索框为空并可输入查询词。
- 计划:先输入查询词,再触发搜索;首选点击放大镜按钮而非重复输入+回车,避免重复触发不确定性;最后利用下拉建议项发起搜索。
- 执行:出现搜索结果页后,识别首条为 AI overview、次条为新闻,优先点击新闻条目;随后点击正确的新闻结果进入文章页。
- 抽取:在文章页读取标题、日期与“definition/定义”(实为摘要/功能概述),构造结构化结果并返回。字段与任务描述一致,完成度高。
- 返回:返回格式化字段:
- Title: Introducing Amazon Nova Act,
- Date: Mar 31, 2025,
- Definition: Today, we’re excited to introduce Amazon Nova Act, a new AI model trained to perform actions within a web browser.
利用 Nova Act 的自动化执行能力,使用自然语言指令测试电商平台的自动化比价购物典型流程
from nova_act import NovaAct
with NovaAct(starting_page="https://www.amazon.com") as nova:
nova.act("search for a wireless earbuds")
nova.act("select the Top Rated item")
nova.act("check the price")
nova.act("if price is less than $500, scroll down or up until you see 'add to list' and then click 'add to list'")
- 第一次 act:搜索
- act(“search for a automatic coffee maker”) 让代理在亚马逊上找到“wireless earbuds”的搜索框、输入并提交,进入搜索结果页。
- 第二次 act:选择“Best Seller”
- act(“select the Top Rated item”) 让代理在结果页寻找带有“Top Rated”徽标或“Top Rated”筛选项的条目并点击。进入商品详情页。
- 第三次 act:查看价格
- act(“check the price”) 在商品详情页定位价格区域(可能含多种价格:促销价、原价、订阅价),并将其读出到返回文本中,或为后续判断保留上下文。
- 第四次 act:条件动作
- 若价格小于 500 美元,则滚动直到看见“add to list”并点击。否则什么也不做。
后台 Thinking+Action 输出如下:
036e> think("I am on the Amazon homepage. The task is to search for a wireless earbuds, select the top rated item, check the price, and if the price is less than $500, add the item to the list. I see a search bar at the top of the page. I should type 'wireless earbuds' into the search bar and press enter to initiate the search.");
>> agentType("wireless earbuds", " <box>11,294,48,991</box> ", {"pressEnter": true});
...
036e> think("I am now on a search results page for wireless earbuds, so my last action was successful. I see the top rated item is the second result. I should click the second result to open it.");
>> agentClick(" <box>482,588,531,1398</box> ");
...
036e> think("I am now on the product page for the top rated item, so my last action was successful. I see the price is EUR 21.51. I should scroll down to see if the price is less than $500.");
>> agentScroll("down", " <box>0,0,712,1439</box> ");
...
036e> think("The page has scrolled down, so my last action was successful. I see the price is still EUR 21.51. I should click the 'Add to List' button to add the item to the list.");
>> agentClick(" <box>192,1179,223,1421</box> ");
...
036e> think("I am now on a sign in page, so my last action was successful. I should pass control to the user to complete the task.");
>> return;
036e> ** View your act run here: /var/folders/dt/dgrd5y515c73hssfz9ky14n40000gr/T/tmpohltmz_7_nova_act_logs/036eb82d-0cca-4491-b002-9d5571748f0f/act_896f4912-57fa-42de-8fd3-9f9caade8a15_from_nova_act_import_NovaAct__.html
end session
Nova Act流程拆解
- 进入亚马逊主页:成功识别到页面为可交互首页,确认搜索栏可用,作为任务的正确起点。
- 搜索发起:检测到顶部搜索框为空,在输入框内输入“wireless earbuds”,执行回车触发搜索,页面顺利跳转至搜索结果页。
- 结果选择:在结果页识别到第二条商品为“Top Rated”标签项,依据策略“优先选择评分最高产品”,点击第二条进入详情页
- 价格判断:在商品详情页读取到价格“EUR 21.51”,系统将其与阈值 500 美元比较,判断条件满足“低于 500 则加清单”的分支,准备执行下一操作。
- 加入清单:点击识别出的“Add to List”按钮,触发展示登录页,系统确认需凭证后中止自动化流程。
相比之前的 Google 搜索流程,这个亚马逊加购流程更复杂,主要因为它不仅要检索信息,还要做业务规则判断与状态性操作(选择“Top Rated”、读取价格、满足阈值再“Add to List”),从信息查找升级为交易前置步骤的多阶段动作。
测试 Nova Act 与 Playwright 在同一 CDP 会话中的协同操作及本地文件上传能力
首先请下载 https://github.com/awslabs/amazon-bedrock-agentcore-samples/blob/main/01-tutorials/05-AgentCore-tools/02-Agent-Core-browser-tool/interactive_tools/browser_viewer.py 这一文件,用于 Browser Tool 的实时查看器,支持适当的显示尺寸调整和 DCV 调试。
import time
import base64
import threading
from datetime import datetime
from playwright.sync_api import sync_playwright
from bedrock_agentcore.tools.browser_client import browser_session
from nova_act import NovaAct
from rich.console import Console
from browser_viewer import BrowserViewerServer
# 执行代码之前,请替换文件中的 YOUR_NOVA_ACT_API_KEY 和 YOUR JEPG FILE 部分
NOVA_ACT_API_KEY = "YOUR_NOVA_ACT_API_KEY"
console = Console()
def capture_cdp_screenshot(context, page, filename_prefix="screenshot", image_format="jpeg"):
"""Capture a screenshot using the CDP API and save to file."""
cdp_client = context.new_cdp_session(page)
screenshot_data = cdp_client.send("Page.captureScreenshot", {
"format": image_format,
"quality": 80,
"captureBeyondViewport": True
})
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{filename_prefix}_{timestamp}.{image_format}"
image_bytes = base64.b64decode(screenshot_data['data'])
with open(filename, "wb") as f:
f.write(image_bytes)
console.print(f"✅ Screenshot saved: {filename}")
return filename
def run_nova_act(ws_url, headers):
"""Run Nova Act in a separate thread to avoid asyncio conflicts"""
with NovaAct(
cdp_endpoint_url=ws_url,
cdp_headers=headers,
nova_act_api_key=NOVA_ACT_API_KEY,
starting_page="https://www.amazon.com"
) as nova_act:
result = nova_act.act("Search for wireless earbuds and get the details of the lowest priced one on the first page")
console.print(f"[bold green]Nova Act Result:[/bold green] {result}")
def main():
try:
with browser_session('us-west-2') as client:
console.print("✅ Browser ready!")
ws_url, headers = client.generate_ws_headers()
# Start viewer server
console.print("[cyan]Starting viewer server...[/cyan]")
viewer = BrowserViewerServer(client, port=8005)
viewer_url = viewer.start(open_browser=True)
console.print(f"🌐 Browser viewer running at [bold blue]{viewer_url}[/bold blue]")
# Run Nova Act in a separate thread
nova_thread = threading.Thread(target=run_nova_act, args=(ws_url, headers))
nova_thread.start()
nova_thread.join()
# After Nova Act completes, use Playwright on the same CDP session
with sync_playwright() as playwright:
browser = playwright.chromium.connect_over_cdp(ws_url, headers=headers)
try:
context = browser.contexts[0] if browser.contexts else browser.new_context()
page = context.pages[0] if context.pages else context.new_page()
console.print(f"[cyan]Page title:[/cyan] {page.title()}")
# Step 1: Navigate to adobe
console.print("🌐 Navigating to Adobe Express JPG->PNG...")
page.goto("https://www.adobe.com/express/feature/image/convert/jpg-to-png", wait_until="domcontentloaded")
time.sleep(10)
capture_cdp_screenshot(context, page, "adobe_home")
# Step 2: Upload image
console.print("🔎 Uploading image...")
file_path = "Your JEPG File"
page.locator('input[type="file"]').set_input_files(file_path)
time.sleep(30)
capture_cdp_screenshot(context, page, "file_uploaded")
# Step 3: Keep session alive for visualization
console.print("🕒 Keeping session alive. Press Ctrl+C to exit...")
while True:
time.sleep(120)
finally:
console.print("[yellow]Closing browser...[/yellow]")
if not page.is_closed():
page.close()
browser.close()
except KeyboardInterrupt:
console.print("\n[yellow]Shutting down...[/yellow]")
except Exception as e:
console.print(f"[red]Error: {e}[/red]")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()
Nova Act流程拆解
启动 AWS Bedrock 远程浏览器会话(us-west-2 区域)
获取 CDP(Chrome DevTools Protocol)连接信息(WebSocket URL 和 headers)
启动浏览器查看器服务器(端口 8005),提供可视化界面
在独立线程中运行 Nova Act
访问 Amazon 网站
执行任务:搜索无线耳塞并获取首页最低价商品详情
等待任务完成
通过 CDP 连接到同一个浏览器会话
复用现有的浏览器上下文和页面
导航到 Adobe 图片转换页面(JPG→PNG)
截图保存(adobe_home)
上传本地图片文件
截图保存(file_uploaded)
进入无限循环,每 120 秒休眠一次
保持浏览器会话活跃,便于通过查看器观察操作结果
按 Ctrl+C 退出
捕获键盘中断或异常
关闭页面和浏览器连接
打印错误信息(如有)
核心特点:先用 AI 代理自动化操作 Amazon,再用 Playwright 手动控制浏览器完成 Adobe 图片上传任务,全程可通过 Web 界面可视化观察。
后台 Thinking+Action 输出如下:
39de> act("Search for wireless earbuds and get the details of the lowest priced one on the first page")
...
39de> think("I am on the amazon website and I see a continue shopping button. I should complete my task by navigating to the homepage. I am now on the shopping page, not the homepage. I need to navigate to the homepage. I should click on the continue shopping button to go to the homepage.");
>> agentClick(" <box>202,582,231,873</box> ");
...
39de> think("The screenshot now shows that I am on the homepage. My prior action of clicking on the continue shopping button was successful. I am now on the homepage, which is what I need. I can see the search field, but it is empty. I should click on the search field and type 'wireless earbuds' to search for wireless earbuds.");
>> agentType("wireless earbuds", " <box>9,403,48,990</box> ", {"pressEnter": true});
...
39de> think("The screenshot now shows that the search has been initiated. My prior action of clicking on the search field and typing 'wireless earbuds' was successful. I am now on the wireless earbuds search results page, which is what I need. I can see the sort by dropdown, but it is not set to price: low to high. I should click on the sort by dropdown to reveal more options.");
>> agentClick(" <box>108,1300,131,1429</box> ");
...
39de> think("The screenshot now shows that the sort by dropdown is expanded. My prior action of clicking on the sort by dropdown was successful. The sort by dropdown is now expanded, which is what I need. I can see the price: low to high option, but it has not been selected yet. I should click on the price: low to high option to select it.");
>> agentClick(" <box>141,1264,167,1426</box> ");
...
39de> think("The screenshot now shows that the page is sorted by price: low to high. My prior action of clicking on the price: low to high option was successful. I am now on the sorted search results page, which is what I need. I can see the first result, but it has not been selected yet. I should click on the first result to open it.");
>> agentClick(" <box>558,590,605,1406</box> ");
...
39de> think("The screenshot now shows that I am on the product page for the first result. My prior action of clicking on the first result was successful. I am now on the product page, which is what I need. My task to search for wireless earbuds and get the details of the lowest priced one on the first page has been completed successfully. My task is complete, and no return information was asked of me. I should empty return.");
>> return;
39de> ** View your act run here: /var/folders/dt/dgrd5y515c73hssfz9ky14n40000gr/T/tmp1b01xzr8_nova_act_logs/39dea150-c1b1-46ec-b79b-ccc5fdfce615/act_7b5f97e6-967e-4212-8de1-2eff70627d8d_Search_for_wireless_earbuds_an.html
Nova Act Result: ActResult(
response = None
parsed_response = None
valid_json = None
matches_schema = None
metadata = ActMetadata(
session_id = 39dea150-c1b1-46ec-b79b-ccc5fdfce615
act_id = 7b5f97e6-967e-4212-8de1-2eff70627d8d
num_steps_executed = 6
start_time = 2025-10-15 15:23:14.857843 CST
end_time = 2025-10-15 15:24:49.528876 CST
step_server_times_s = ['5.272', '6.321', '6.220', '5.889', '6.030', '6.477']
prompt = 'Search for wireless earbuds and get the details of the lowest priced one on the first page'
)
)
end session
可以看出,在这个示例中工程化复杂度更高。之前的 Google 搜索/新闻抽取、Amazon 搜索加购:均是“单体 Nova Act 会话 + 顺序 act 指令”的教学小例子。在本段代码中:
- 引入远程浏览器托管(browser_session),把浏览器运行与本地脚本解耦。
- 通过 CDP 进行多客户端协作:同一浏览器会话先由 Nova Act 驱动,再由 Playwright 接手继续任务,体现“代理 + 传统自动化”的混合编排。
- 加入线程隔离,避免事件循环冲突。
- 增加可观测性:Viewer 可视化 + CDP 截图,使调试与验证更可控。
结语
智能体驱动测试已是现实,而非未来。早期采用者正在获得竞争优势:更快的发布周期、更低的维护成本、更高的测试覆盖率。随着软件系统日益复杂,传统测试方法难以为继,智能体驱动测试提供了可持续、可扩展的解决方案。在 AI 时代,测试的本质不再是执行预定义步骤,而是智能地探索、学习、适应和验证。
本文探讨了如何利用 AWS 生态的 Amazon Nova Act 和 AgentCore Browser Tool,实现开箱即用的一站式智能体测试解决方案。在后续文章中,我们将进一步探讨 Browser-Use 与 AgentCore Browser Tool 的结合,展示如何通过开源框架灵活选择 LLM 模型(如 Claude),并对比两种方案各自的优势与适用场景。
*前述特定亚马逊云科技生成式人工智能相关的服务目前在亚马逊云科技海外区域可用。亚马逊云科技中国区域相关云服务由西云数据和光环新网运营,具体信息以中国区域官网为准。
本篇作者