66 lines
2.5 KiB
Python
66 lines
2.5 KiB
Python
from playwright.sync_api import sync_playwright
|
|
import time
|
|
import os
|
|
|
|
def dismiss_vite_error(page):
|
|
try:
|
|
page.evaluate("document.querySelector('vite-error-overlay')?.remove()")
|
|
time.sleep(0.3)
|
|
except:
|
|
pass
|
|
|
|
def test_empty_center():
|
|
os.makedirs('/tmp/frontend_test', exist_ok=True)
|
|
|
|
with sync_playwright() as p:
|
|
browser = p.chromium.launch(headless=True)
|
|
context = browser.new_context(viewport={"width": 1400, "height": 900})
|
|
page = context.new_page()
|
|
|
|
print("=" * 60)
|
|
print("测试空状态div居中")
|
|
print("=" * 60)
|
|
|
|
# 访问工具页面(当没有数据时会显示空状态)
|
|
page.goto('http://localhost:5173/tools', wait_until='load', timeout=15000)
|
|
time.sleep(2)
|
|
dismiss_vite_error(page)
|
|
page.screenshot(path='/tmp/frontend_test/empty_center.png', full_page=True)
|
|
print(" ✓ 空状态截图已保存")
|
|
|
|
# 检查空状态容器是否居中
|
|
empty_wrapper = page.locator('.tools-empty-wrapper')
|
|
if empty_wrapper.count() > 0:
|
|
# 获取容器的位置和尺寸
|
|
bbox = empty_wrapper.first.bounding_box()
|
|
if bbox:
|
|
print(f" 容器位置: x={bbox['x']}, y={bbox['y']}")
|
|
print(f" 容器尺寸: width={bbox['width']}, height={bbox['height']}")
|
|
|
|
# 检查是否居中(水平居中)
|
|
page_width = page.viewport_size['width']
|
|
expected_center_x = page_width / 2
|
|
actual_center_x = bbox['x'] + bbox['width'] / 2
|
|
print(f" 页面中心X: {expected_center_x}, 容器中心X: {actual_center_x}")
|
|
|
|
if abs(expected_center_x - actual_center_x) < 10:
|
|
print(" ✓ 容器水平居中")
|
|
else:
|
|
print(" ⚠ 容器可能未完全居中")
|
|
|
|
# 检查子元素是否居中对齐
|
|
container = page.locator('.ant-empty')
|
|
if container.count() > 0:
|
|
styles = container.first.evaluate("el => { const s = window.getComputedStyle(el); return { display: s.display, justifyContent: s.justifyContent, alignItems: s.alignItems, margin: s.margin }; }")
|
|
print(f" 子元素样式: {styles}")
|
|
else:
|
|
print(" 未找到空状态容器(可能当前有数据)")
|
|
|
|
browser.close()
|
|
print("\n" + "=" * 60)
|
|
print("测试完成!")
|
|
print("=" * 60)
|
|
|
|
if __name__ == "__main__":
|
|
test_empty_center()
|