ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-feature-settings: normal;font-variation-settings: normal;font-size: 12.6px;text-align: left;line-height: 1.75;color: rgb(221, 17, 68);background: rgba(27, 31, 35, 0.05);padding: 3px 5px;border-radius: 4px;">DeepSeek这样的ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-feature-settings: normal;font-variation-settings: normal;font-size: 12.6px;text-align: left;line-height: 1.75;color: rgb(221, 17, 68);background: rgba(27, 31, 35, 0.05);padding: 3px 5px;border-radius: 4px;">AI模型如何“看懂”动态网站的内容?网站不像静态文本,输入一个ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-feature-settings: normal;font-variation-settings: normal;font-size: 12.6px;text-align: left;line-height: 1.75;color: rgb(221, 17, 68);background: rgba(27, 31, 35, 0.05);padding: 3px 5px;border-radius: 4px;">URL并不能让AI直接获取页面信息,尤其是那些需要交互(如点击按钮)才能加载的模块。很多测试工程师可能也有类似困惑:AI能否从URL跃升到理解页面逻辑,甚至生成自动化脚本?在之前的文章
Playwright + DeepSeek实战:手把手教你用ai实现xmind用例生成到自动化测试(以电商为例)
我曾通过人工描述网站功能,让DeepSeek生成测试用例和Playwright脚本。这次,我们将更进一步,围绕读者的提问,探索DeepSeek理解网站内容的可行方法,并以saucedemo.com为实战案例,提供Python实现的详细解析。
DeepSeek的“理解”边界在哪里?让我们先拆解问题,明确挑战所在:
saucedemo.com是一个电商网站,包含登录、商品列表、购物车和结算等功能。部分内容(例如商品详情或购物车列表)需要用户点击后才会加载。单纯给DeepSeek一个URL,它无法直接访问或解析这些动态内容。
AI的能力限制当前的DeepSeek等大语言模型主要依赖文本输入生成输出,没有内置浏览器或爬虫功能。它无法像人类一样打开网页、点击按钮、观察页面变化。
测试工程师可能希望DeepSeek能“自动”理解网站内容并生成测试用例或脚本,而非依赖人工输入。那么,这是否可行?如果不可行,有哪些替代方案?
基于这些分析,让DeepSeek尽可能理解saucedemo.om的页面内容,并生成有用的自动化测试代码,都有哪些可能的方法,将是本文探讨的目标。
既然DeepSeek无法直接访问网站,我们需要为它提供“眼睛”和“语言”。以下是三种解决方案,涵盖从实用到创新的思路:
这是目前最直接、高效的方法。通过人工提供网站的结构和功能描述,DeepSeek可以基于文本生成测试用例和脚本。
saucedemo.com假设我们要测试saucedemo.com的购物车功能,可以这样描述:
网站URL:https://www.saucedemo.com
功能描述:
1. 登录页面:输入用户名"standard_user"和密码"secret_sauce",点击"Login"按钮进入商品页。
2. 商品页面:展示多个商品,每个商品有"Add to cart"按钮,点击后购物车图标显示数量。
3. 购物车页面:点击右上角购物车图标进入,显示已添加的商品,包含"Checkout"按钮。
4. 结算页面:填写姓名、邮编等信息,点击"Continue"提交。
交互逻辑:
- 商品页面点击"Add to cart"后,按钮变为"Remove",购物车数量+1。
- 结算页面若邮编为空,提示错误"Error: Postal Code is required"。
输出要求:
- 为saucedemo.com的购物车功能编写Python Playwright自动化测试脚本
- 要求使用POM模式
- 覆盖正常添加商品和空邮编异常场景DeepSeek生成代码# pages/login_page.py
from playwright.sync_api import Page
class LoginPage:
def __init__(self, page: Page):
self.page = page
self.username = page.locator("#user-name")
self.password = page.locator("#password")
self.login_btn = page.locator("#login-button")
def login(self, username: str, password: str):
self.username.fill(username)
self.password.fill(password)
self.login_btn.click()
# pages/product_page.py
class ProductPage:
def __init__(self, page: Page):
self.page = page
self.add_to_cart_btn = page.locator("button:has-text('Add to cart')").first
self.cart_icon = page.locator(".shopping_cart_badge")
def add_to_cart(self):
self.add_to_cart_btn.click()
def get_cart_count(self) -> int:
return int(self.cart_icon.inner_text()) if self.cart_icon.is_visible() else 0
# pages/checkout_page.py
class CheckoutPage:
def __init__(self, page: Page):
self.page = page
self.postal_code = page.locator("#postal-code")
self.continue_btn = page.locator("text=Continue")
self.error_msg = page.locator(".error-message-container")
def fill_shipping_info(self, postal_code: str = ""):
self.postal_code.fill(postal_code)
self.continue_btn.click()
def get_error_message(self) -> str:
return self.error_msg.inner_text()
# tests/test_cart.py
import pytest
from playwright.sync_api import expect
from pages.login_page import LoginPage
from pages.product_page import ProductPage
from pages.checkout_page import CheckoutPage
def test_add_to_cart(page):
login_page = LoginPage(page)
product_page = ProductPage(page)
page.goto("https://www.saucedemo.com")
login_page.login("standard_user", "secret_sauce")
product_page.add_to_cart()
assert product_page.get_cart_count() == 1
def test_empty_postal_code(page):
login_page = LoginPage(page)
checkout_page = CheckoutPage(page)
page.goto("https://www.saucedemo.com")
login_page.login("standard_user", "secret_sauce")
page.goto("https://www.saucedemo.com/checkout-step-one.html")
checkout_page.fill_shipping_info("")
assert "Postal Code is required" in checkout_page.get_error_message()DeepSeek(进阶方案)Playwright获取saucedemo.com商品页面的源码如果想减少人工描述,可以用工具抓取页面HTML源码,让DeepSeek分析。例如:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://www.saucedemo.com/inventory.html")
html_content = page.content()
print(html_content)
browser.close()将HTML输入DeepSeek:请分析以下HTML内容,提取saucedemo.com商品页面的关键元素(如商品列表、添加按钮),并生成Playwright自动化脚本。
DeepSeek生成代码# pages/product_page.py
class ProductPage:
def __init__(self, page: Page):
self.page = page
self.add_to_cart_btn = page.locator("[data-test^='add-to-cart-']")
self.cart_icon = page.locator(".shopping_cart_badge")
def add_to_cart(self):
self.add_to_cart_btn.first.click()page.locator("button:has-text('Add to cart')").first.click()
new_html = page.content()将new_html输入DeepSeek,让它分析交互后的变化。
HTML可能冗长,包含无关信息,影响分析效果;动态交互仍需人工设计抓取逻辑。AI优化(创新方案)Playwright负责录制脚本,AI负责代码优化扩展为了进一步提升效率,我们可以利用Playwright的脚本录制功能,让AI基于录制的脚本进行优化和扩展。
playwright codegen https://www.saucedemo.com,手动执行操作(如登录、添加商品),生成Playwright脚本。I:将脚本提供给DeepSeek,提示优化代码结构、改进元素定位或扩展测试用例。AI优化与扩展:DeepSeek生成模块化代码并补充异常场景测试。from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://www.saucedemo.com/")
page.fill("#user-name", "standard_user")
page.fill("#password", "secret_sauce")
page.click("#login-button")
browser.close()请优化以下Playwright脚本,使其更模块化,并扩展一个异常测试用例(如登录失败场景)。
DeepSeek回复# pages/login_page.py
class LoginPage:
def __init__(self, page):
self.page = page
self.username = page.locator("#user-name")
self.password = page.locator("#password")
self.login_btn = page.locator("#login-button")
self.error_msg = page.locator(".error-message-container")
def login(self, username, password):
self.username.fill(username)
self.password.fill(password)
self.login_btn.click()
def get_error_message(self):
return self.error_msg.inner_text()
# tests/test_login.py
from playwright.sync_api import expect
def test_successful_login(page):
login_page = LoginPage(page)
page.goto("https://www.saucedemo.com/")
login_page.login("standard_user", "secret_sauce")
expect(page).to_have_url("https://www.saucedemo.com/inventory.html")
def test_failed_login(page):
login_page = LoginPage(page)
page.goto("https://www.saucedemo.com/")
login_page.login("invalid_user", "wrong_password")
expect(login_page.get_error_message()).to_contain("Username and password do not match")playwright codegen录制简单高效,为AI提供具体输入,能显著提升代码质量。AI可能需人工补充上下文。四、结语
综合来看:
DeepSeek的语言能力,确保结果准确。HTML):适合希望自动化提取页面信息的场景,但需工具支持,且效果受HTML质量影响。AI优化):创新性方案,适合简单操作流,能快速生成高质量代码。以saucedemo.com为例,推荐流程:
AI优化。AI生成完善代码。AI技术进步,我们或许能直接给DeepSeek一个URL,让它“自己看懂”页面。| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |