浏览器自动化测试系统

一个基于Playwright的测试自动化系统,用于验证和提取亚马逊巴西网站上PlayStation 5产品的信息。

  • by AmomDeAbreu
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 AmomDeAbreu/MCP

使用说明

MCP Playstation项目的自述文件

概述

MCP Playstation项目是一个基于剧作家的测试自动化系统,用于验证和提取亚马逊巴西网站上的产品信息 1 该系统通过自动浏览器测试专门针对PlayStation 5主机列表 2 .

目的

该项目有两个主要功能:

  1. 功能测试:验证亚马逊巴西的搜索功能和页面导航 3
  2. 数据提取:删除PlayStation 5产品定价信息 4

项目结构

MCP/
├── main.py              # Core test automation logic
├── app.py               # Aplication responsible to host a flesk server an support the localhost website
├── graphic.py           # Code that convert all the data into a ploted graphic
├── preco.json           # File that contains the main data used in the graphic
├── __pycache__/         # Python bytecode cache
└── README.md            # Project documentation

所有测试逻辑都位于一个文件中(main.py)包含两个测试功能 5 .

测试功能

test_has_title(page: Page)

通过检查页面标题中的“亚马逊”来验证亚马逊巴西主页可访问性的烟雾测试 6 .

test_search_and_get(page: Page)

主要自动化工作流程包括:

  • 导航到亚马逊巴西 7
  • 搜索“playstation 5” 8
  • 查找“控制台PlayStation 5-数字版”产品 9
  • 提取并打印产品价格 4

依赖项

  • 剧作家:浏览器自动化框架 10
  • pytest:测试执行框架(通过测试函数命名隐式) 11
  • 关于:标题验证的正则表达式支持 12

按键选择器

该系统使用特定的CSS选择器与亚马逊的界面进行交互:

  • #twotabsearchtextbox:搜索输入字段 13
  • #nav-search-submit-text:搜索提交按钮 14
  • div[data-cy="asin-faceout-container"]:产品容器 9
  • span.a-offscreen:价格文本元素 15

执行

使用pytest的测试发现机制执行测试,该机制自动查找并运行前缀为的函数 test_ 16 The page 夹具由 pytest-playwright 插件.

备注

代码库包含注释掉的代码(第26-41行),这些代码似乎是替代元素选择策略的实验性或弃用的测试逻辑 17 目前的实现使用了一种更简单的方法,专注于数字版PlayStation 5主机.

您可能想探索的Wiki页面:

引用

文件: main.py(L1-1)

import re

文件: main.py(L2-2)

from playwright.sync_api import Page, expect

文件: main.py(L4-25)

def test_has_title(page: Page):
    page.goto("https://www.amazon.com.br/")

    # Expect a title "to contain" a substring.
    expect(page).to_have_title(re.compile("Amazon"))

def test_search_and_get(page: Page):
    page.goto("https://www.amazon.com.br/")

    # Espera o link "Get started" estar visível
    page.locator("#twotabsearchtextbox").fill("playstation 5");
    element1 = page.locator("#nav-search-submit-text");
    element1.click()
    expect(page).to_have_title(re.compile("playstation"))
    # Encontra o produto pelo texto do título
    produto = page.locator('div[data-cy="asin-faceout-container"]:has-text("Console PlayStation 5 - Digital Edition")').first

    # Dentro desse produto, pega o preço
    preco = produto.locator("span.a-offscreen").first
    preco.wait_for(state="visible", timeout=5000)
    texto_preco = preco.inner_text()
    print(texto_preco)

文件: main.py(L26-41)

    # element = page.locator('a[href*="B09FGCKBPK"]:has-text("Console PlayStation 5 - Digital Edition")')
    # div.wait_for(state="visible", timeout=5000)
    # print("HTML do link Playstation 5:", element.inner_html())

    # element = page.get_by_role("link", name="Get started")
    # element.wait_for(state="visible", timeout=5000)
    # print("HTML do link Get started:", element.inner_html())

    # # Click the get started link.
    # element.click()

    # # Expects page to have a heading with the name of Installation.
    # expect(page.get_by_role("heading", name="Installation")).to_be_visible()
    # title = page.get_by_role("heading", name="Installation")
    # title.wait_for(state="visible", timeout=5000)
    # print("HTML do título Installation:", title.inner_html())