You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Linux环境的GitLab流水线中用Selenium测试WebKit浏览器(适配Safari)

可行方案推荐

方案1:Playwright + WebKit

Playwright官方维护的WebKit基于Apple官方WebKit源码编译,在Linux环境下可直接运行,对Mobile Safari的渲染兼容性接近原生,且支持Selenium测试适配(或直接使用Playwright原生API)。

  • 优势:无需额外配置驱动,自带WebKit二进制文件,可精准模拟Mobile Safari的设备参数(视口、User-Agent、触摸事件等)
  • 实操步骤:
    1. 安装Playwright依赖:pip install playwright(以Python为例,其他语言逻辑一致)
    2. 安装WebKit浏览器:playwright install webkit
    3. 编写测试时指定WebKit并配置Mobile Safari参数:
      from playwright.sync_api import sync_playwright
      
      with sync_playwright() as p:
          browser = p.webkit.launch()
          # 模拟iPhone 14的Mobile Safari环境
          context = browser.new_context(
              viewport={'width': 390, 'height': 844},
              user_agent='Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1'
          )
          page = context.new_page()
          page.goto("你的测试地址")
          # 执行测试逻辑
          browser.close()
      
    4. GitLab CI中只需确保Runner安装了Playwright依赖和WebKit,无需额外内核配置

方案2:配置macOS类型的GitLab Runner

如果团队有条件,直接使用macOS环境的GitLab Runner搭配原生Safari + SafariDriver测试,这是最准确匹配Mobile Safari渲染效果的方案:

  • 优势:完全使用Apple官方Safari浏览器,渲染逻辑和用户实际使用的Mobile Safari完全一致
  • 实操步骤:
    1. 在GitLab项目中配置macOS Runner(可选用GitLab托管的共享Runner,或自行搭建本地macOS Runner)
    2. 启用Safari的远程自动化:打开Safari「开发」菜单,勾选「允许远程自动化」
    3. 编写标准Selenium测试并指定Safari驱动:
      from selenium import webdriver
      from selenium.webdriver.safari.options import Options
      
      options = Options()
      # 模拟Mobile Safari标识
      options.add_argument("--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1")
      options.add_argument("--window-size=390,844")
      
      driver = webdriver.Safari(options=options)
      driver.get("你的测试地址")
      # 执行测试逻辑
      driver.quit()
      

方案3:使用WebKit容器化镜像

直接采用预构建的WebKit容器镜像(如基于Playwright的官方镜像),在Linux Runner中拉取镜像运行测试,无需手动安装依赖:

  • 优势:环境隔离性好,无需在Runner中配置复杂的WebKit依赖
  • GitLab CI配置示例:
    test:webkit:
      image: mcr.microsoft.com/playwright:v1.40.0-focal
      script:
        - pip install pytest-playwright
        - pytest tests/ --browser webkit
    

内容的提问来源于stack exchange,提问作者kroe761

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 13:25:57