当阿拉伯语文字溢出时,如何自动截图高亮?

解读

国内电商、金融、政务类 Web 系统出海时,阿拉伯语(RTL 语言) 经常因容器宽度不足导致单词断裂或溢出,引发 UI 评审不通过。面试官问“自动截图高亮”,实质考察三点:

  1. 能否在无人工值守的 CI 流水线里精准判定溢出;
  2. 能否在多语言混杂页面中只高亮阿语元素;
  3. 能否生成国内项目可复用的审计凭据(带高亮截图+日志)。
    若只答“截全屏”或“用 JavaScript 滚动”,会被追问“如何定位阿语”“如何高亮”“如何断言”,因此必须给出可工程落地的 RTL 溢出检测方案。

知识点

  • WebDriver W3C 规范中 getBoundingClientRect() 与 scrollWidth/offsetWidth 差值判定溢出
  • Arabic Unicode Range:[\u0600-\u06FF\u0750-\u077F],用于正则过滤阿语节点
  • RTL 溢出特征:direction:rtl 且 scrollWidth > clientWidth + 1px(Chrome 亚像素误差)
  • 高亮注入:arguments[0].style.cssText += 'box-shadow:0 0 0 3px #F5222D !important;' 符合国内审计红线风格
  • 截图策略:AShot(Java)或 Pillow(Python)裁剪元素可视区,避免全屏导致个人信息泄露合规风险
  • CI 集成:Allure 附件 Allure.addAttachment("arabic-overflow", new ByteArrayInputStream(pngBytes), "image/png"),与国内主流禅道、飞书OKR打通

答案

以 Python + Selenium 4.11 + Pillow 为例,完整代码可直接放入 Pytest 用例,无需额外阿拉伯语字体环境:

import re, base64, io
from PIL import Image, ImageDraw
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement

ARABIC_RANGE = re.compile(r'[\u0600-\u06FF\u0750-\u077F]')

def highlight_arabic_overflow(driver, save_path):
    # 1. 过滤出包含阿语文字的可见元素
    els = driver.execute_script(
        "return [...document.querySelectorAll('body *:not(script):not(style)')]"
        ".filter(el => el.offsetWidth && el.offsetHeight && "
        "           window.getComputedStyle(el).visibility !== 'hidden' && "
        "           /[\\u0600-\\u06FF\\u0750-\\u077F]/.test(el.textContent))"
    )
    overflow_list = []
    for el in els:
        # 2. 精确溢出判定
        is_overflow, rect = driver.execute_script("""
            const el = arguments[0];
            const dir = window.getComputedStyle(el).direction;
            const st = el.scrollWidth, cw = el.clientWidth;
            return [dir === 'rtl' && st > cw + 1, el.getBoundingClientRect()];
        """, el)
        if is_overflow:
            overflow_list.append((el, rect))

    if not overflow_list:
        return None

    # 3. 全屏截图
    png = driver.get_screenshot_as_png()
    img = Image.open(io.BytesIO(png)).convert('RGBA')
    draw = ImageDraw.Draw(img)

    # 4. 高亮所有溢出阿语元素
    for _, rect in overflow_list:
        box = [rect['left'], rect['top'], rect['right'], rect['bottom']]
        draw.rectangle(box, outline='#F5222D', width=3)

    img.save(save_path, format='PNG')
    return save_path

调用断言:

def test_arabic_no_overflow(driver):
    driver.get("https://example.com/ar")
    pic = highlight_arabic_overflow(driver, "arabic_overflow.png")
    assert pic is None, f"发现阿语溢出,已截图:{pic}"

CI 侧把 arabic_overflow.png 作为 Allure 附件上传,钉钉群机器人自动推送,符合国内交付流程。

拓展思考

  1. 混合语言断句:当阿语与英语混排时,仅用 scrollWidth 会误报,可叠加 Canvas 2D 测量 ctx.measureText() 拿到真实文字宽度,误差降到 0.5 px。
  2. 移动端适配:在 Android WebView 90+ 上,visualViewport 缩放会导致 getBoundingClientRect 漂移,需 execute_script("return window.visualViewport.scale") 做比例修正。
  3. 合规脱敏:国内金融项目截图需遮挡卡号、姓名,可在高亮后二次遍历含敏感关键字的元素,用 Pillow 画黑色遮罩,再通过 OCR 二次校验确保未泄露。
  4. 性能优化:万级 SKU 列表页一次性遍历 DOM 会卡 3s,可改用 Chrome DevTools Protocol DOM.getDocument + DOM.querySelector 批量拉取节点,耗时降至 300 ms。