Github Actions运行axe-core/cli失败:ChromeDriver与Chrome版本不匹配
解决GitHub Action中axe-core/cli的ChromeDriver与Chrome版本不匹配问题
问题根源是GitHub Actions的ubuntu-latest runner默认预装的Chrome版本(120.0.6099.224)和axe-core/cli自动安装的ChromeDriver版本(121)不兼容,导致会话创建失败。以下是两种可行的解决方法:
方案一:手动安装匹配版本的ChromeDriver
通过脚本获取当前Chrome的版本,下载对应版本的ChromeDriver,然后在运行axe时指定路径:
修改流水线的Run axe步骤前新增安装ChromeDriver的步骤,完整配置如下:
name: CI Tests on: pull_request: branches: [main] jobs: axe: runs-on: ubuntu-latest env: WORDPRESS_API_URL: XYZ GITHUB_API_SECRET: ABC steps: - uses: actions/checkout@v3 - name: Use Node.js 18.x uses: actions/setup-node@v1 with: node-version: 18.x - run: yarn install - run: yarn run build - run: yarn dev & npx wait-on http://localhost:3000 - name: Install matching ChromeDriver run: | # 获取当前Chrome版本号 CHROME_VERSION=$(google-chrome --version | grep -oP '\d+\.\d+\.\d+\.\d+') # 下载对应大版本的ChromeDriver最新版本号 wget -N https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${CHROME_VERSION%.*} CHROMEDRIVER_VERSION=$(cat LATEST_RELEASE_${CHROME_VERSION%.*}) # 下载并解压ChromeDriver wget -N https://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip unzip chromedriver_linux64.zip chmod +x chromedriver - name: Run axe run: | yarn add @axe-core/cli npx axe http://localhost:3000 --chromedriver-path ./chromedriver --exit
方案二:同步Chrome与ChromeDriver版本(推荐)
使用browser-actions/setup-chrome action安装与ChromeDriver匹配的Chrome版本,省去手动下载的步骤:
修改流水线,新增Setup Chrome步骤,完整配置如下:
name: CI Tests on: pull_request: branches: [main] jobs: axe: runs-on: ubuntu-latest env: WORDPRESS_API_URL: XYZ GITHUB_API_SECRET: ABC steps: - uses: actions/checkout@v3 - name: Use Node.js 18.x uses: actions/setup-node@v1 with: node-version: 18.x - name: Setup Chrome uses: browser-actions/setup-chrome@v1 with: chrome-version: '121' # 与axe-core/cli安装的ChromeDriver版本匹配 - run: yarn install - run: yarn run build - run: yarn dev & npx wait-on http://localhost:3000 - name: Run axe run: | yarn add @axe-core/cli npx axe http://localhost:3000 --exit
额外优化建议
可以将@axe-core/cli添加到项目的package.json开发依赖中,避免每次流水线都临时安装,提升执行效率:
# 本地执行后提交package.json yarn add -D @axe-core/cli
然后将流水线中Run axe步骤的yarn add @axe-core/cli删除,直接运行npx axe ...即可。
内容的提问来源于stack exchange,提问作者jamesthemullet
相关产品推荐
相关产品推荐

