GitHub Actions中Selenium测试报告共享及Ubuntu环境报错修复咨询
问题解决方案:Selenium+Python测试用例与GitHub Actions工作流问题
一、Windows-latest环境下测试报告跨分支/仓库共享方案
方案1:通过GitHub Actions Artifact共享报告
修改工作流文件,在生成报告后添加上传Artifact步骤,报告可在Actions页面下载,也支持其他仓库工作流拉取:
name: Automate_testcases_py on: push: branches: [ "Automate_with_GitActions" ] pull_request: branches: [ "Automate_with_GitActions" ] permissions: contents: read actions: write # 新增:允许上传Artifact jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v3 - name: Set up Python 3.9 uses: actions/setup-python@v3 with: python-version: "3.9" - name: Install dependencies run: | python -m pip install --upgrade pip pip install pytest selenium pytest-html python-dotenv webdriver-manager chromedriver-autoinstaller - name: Run tests and generate report run: | py.test -v -s --html=report.html # 注意移除--html与report.html之间的空格 - name: Upload test report uses: actions/upload-artifact@v3 with: name: pytest-html-report path: report.html
方案2:推送报告到当前仓库独立分支
若需将报告持久化到仓库分支,修改权限并添加推送步骤:
permissions: contents: write # 改为write权限,允许推送代码 # 生成报告后添加以下步骤 - name: Push report to test-reports branch run: | git config --global user.name "GitHub Actions" git config --global user.email "actions@github.com" # 切换到test-reports分支,不存在则创建 git checkout test-reports 2>/dev/null || git checkout -b test-reports cp report.html . git add report.html git commit -m "Update test report: $(date +'%Y-%m-%d %H:%M:%S')" || echo "No changes to commit" git push origin test-reports --force
方案3:跨仓库推送报告
要同步报告到其他仓库,需创建个人访问令牌(PAT)并添加到当前仓库Secrets(命名为REPO_PAT),再添加推送步骤:
# 生成报告后添加以下步骤 - name: Push report to target repository run: | git config --global user.name "GitHub Actions" git config --global user.email "actions@github.com" # 克隆目标仓库 git clone https://${{ secrets.REPO_PAT }}@github.com/your-username/target-repo.git cp report.html target-repo/ cd target-repo git add report.html git commit -m "Update test report from AutomatedTests: $(date +'%Y-%m-%d %H:%M:%S')" || echo "No changes to commit" git push
二、Ubuntu-latest环境下AttributeError错误修复
错误原因
Ubuntu环境未配置浏览器无头模式、缺少系统依赖,或全局driver对象未正确初始化,导致截图时driver为None。
修复步骤
1. 修改conftest.py配置
添加无头模式参数,并增加driver非空检查:
import pytest from selenium import webdriver options = webdriver.ChromeOptions() options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option("useAutomationExtension", False) # 添加Ubuntu必需的无头模式参数 options.add_argument("--headless") options.add_argument("--disable-gpu") options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") global driver url="https://automatedtestcases" def pytest_addoption(parser): parser.addoption( "--browser", action="store", default="chrome" # 默认改为chrome,适配Ubuntu环境 ) @pytest.fixture() def setup(request): global driver browser = request.config.getoption("--browser") try: if browser == "chrome": from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) elif browser =="firefox": from selenium.webdriver.firefox.service import Service as FirefoxService from webdriver_manager.firefox import GeckoDriverManager firefox_options = webdriver.FirefoxOptions() firefox_options.add_argument("--headless") driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()), options=firefox_options) else: raise ValueError(f"Unsupported browser: {browser}") print("driver is open 1 ", driver) driver.get(url) driver.maximize_window() request.cls.driver = driver yield finally: if driver is not None: driver.quit() # 用quit代替close,确保资源完全释放 @pytest.mark.hookwrapper def pytest_runtest_makereport(item): pytest_html = item.config.pluginmanager.getplugin('html') outcome = yield report = outcome.get_result() extra = getattr(report, 'extra', []) if report.when == 'call' or report.when == "setup": xfail = hasattr(report, 'wasxfail') if (report.skipped and xfail) or (report.failed and not xfail): file_name = report.nodeid.replace("::", "_") + ".png" _capture_screenshot(file_name) if file_name: html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" onclick="window.open(this.src)" align="right"/></div>' % file_name extra.append(pytest_html.extras.html(html)) report.extra = extra def _capture_screenshot(name): # 增加driver非空检查,避免报错 if driver is not None: driver.get_screenshot_as_file(name)
2. 修改Ubuntu环境工作流文件
添加浏览器系统依赖安装步骤:
name: Automate_testcases_py on: push: branches: [ "Automate_with_GitActions" ] pull_request: branches: [ "Automate_with_GitActions" ] permissions: contents: read jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python 3.9 uses: actions/setup-python@v3 with: python-version: "3.9" - name: Install system dependencies run: | sudo apt-get update sudo apt-get install -y chromium-browser chromium-chromedriver firefox-esr - name: Install Python dependencies run: | python -m pip install --upgrade pip pip install pytest selenium pytest-html python-dotenv webdriver-manager - name: Run tests and generate report run: | py.test -v -s --html=report.html --browser=chrome
内容的提问来源于stack exchange,提问作者Venugopal
相关产品推荐
相关产品推荐

