在单元测试的GitHub操作中运行SelensWebDriver [英] Running Selenium Webdriver in GitHub Actions for unit tests

查看:25
本文介绍了在单元测试的GitHub操作中运行SelensWebDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在GitHub Actions中运行单元测试,然后再部署我的AWS Lambda,在该AWS Lambda中,我正在运行Selenium WebDriver和一个正常运行的硒-铬lambda层(https://github.com/vittorio-nardone/selenium-chromium-lambda)。我在Python3.6中运行我的环境,并使用ChromeDriver版本95.0.4638.54

我在运行单元测试时不断遇到此错误:

selenium.common.exceptions.WebDriverException: 
Message: unknown error: Chrome failed to start: exited abnormally. 
(chrome not reachable)

我使用的Chrome选项与我运行我的AWS Lambda时相同(工作正常):

lambda_options = [
    '--autoplay-policy=user-gesture-required',
    '--disable-background-networking',
    '--disable-background-timer-throttling',
    '--disable-backgrounding-occluded-windows',
    '--disable-breakpad',
    '--disable-client-side-phishing-detection',
    '--disable-component-update',
    '--disable-default-apps',
    '--disable-dev-shm-usage',
    '--disable-domain-reliability',
    '--disable-extensions',
    '--disable-features=AudioServiceOutOfProcess',
    '--disable-hang-monitor',
    '--disable-ipc-flooding-protection',
    '--disable-notifications',
    '--disable-offer-store-unmasked-wallet-cards',
    '--disable-popup-blocking',
    '--disable-print-preview',
    '--disable-prompt-on-repost',
    '--disable-renderer-backgrounding',
    '--disable-setuid-sandbox',
    '--disable-speech-api',
    '--disable-sync',
    '--disk-cache-size=33554432',
    '--hide-scrollbars',
    '--ignore-gpu-blacklist',
    '--ignore-certificate-errors',
    '--metrics-recording-only',
    '--mute-audio',
    '--no-default-browser-check',
    '--no-first-run',
    '--no-pings',
    '--no-sandbox',
    '--no-zygote',
    '--password-store=basic',
    '--use-gl=swiftshader',
    '--use-mock-keychain',
    '--single-process',
    '--headless']

for argument in lambda_options:
    chrome_options.add_argument(argument)

使用一些特定于Lambda的其他选项:

chrome_options.add_argument('--user-data-dir={}'.format(tmp_folder + '/user-data'))
chrome_options.add_argument('--data-path={}'.format(tmp_folder + '/data-path'))
chrome_options.add_argument('--homedir={}'.format(tmp_folder))
chrome_options.add_argument('--disk-cache-dir={}'.format(tmp_folder + '/cache-dir'))

chrome_options.binary_location = "/opt/bin/chromium"

tmp_folder是由AWS提供的文件系统,其中tMP_Folders=/tmp

最后,我正在启动驱动程序:

driver = webdriver.Chrome(options=chrome_options)

这在Lambda中运行得很好,但每次尝试在GitHub操作中运行单元测试时都会失败。

这是在GitHub操作中运行作业时的配置:

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.7
        uses: actions/setup-python@v2
        with:
          python-version: 3.7
      - name: Install dependencies
        run: |
        .......

      - name: Unit test
        run: |

          wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
          echo "deb http://dl.google.com/linux/chrome/deb/ stable main" | sudo tee -a /etc/apt/sources.list.d/google-chrome.list
          sudo apt-get update -qqy
          sudo apt-get -qqy install google-chrome-stable
          CHROME_VERSION=$(google-chrome-stable --version)
          CHROME_FULL_VERSION=${CHROME_VERSION%%.*}
          CHROME_MAJOR_VERSION=${CHROME_FULL_VERSION//[!0-9]}
          sudo rm /etc/apt/sources.list.d/google-chrome.list
          export CHROMEDRIVER_VERSION=`curl -s https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${CHROME_MAJOR_VERSION%%.*}`
          curl -L -O "https://chromedriver.storage.googleapis.com/${CHROMEDRIVER_VERSION}/chromedriver_linux64.zip"
          unzip chromedriver_linux64.zip && chmod +x chromedriver && sudo mv chromedriver /usr/local/bin
          export CHROMEDRIVER_VERSION=`curl -s https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${CHROME_MAJOR_VERSION%%.*}`
          curl -L -O "https://chromedriver.storage.googleapis.com/${CHROMEDRIVER_VERSION}/chromedriver_linux64.zip"
          unzip chromedriver_linux64.zip && chmod +x chromedriver && sudo mv chromedriver /usr/local/bin
          chromedriver -version
          which chromedriver
          which google-chrome

该配置完全取自Selify自己(https://github.com/SeleniumHQ/selenium/blob/selenium-4.0.0-beta-3/.github/actions/setup-chrome/action.yml)在GibHub操作中的Chrome设置,非常有效。

此GitHub Actions配置使用的是最新的ChromeDriver,所以我同时尝试了旧版本(前面提到的95.0.4638.54)和最新版本(96.0.4664.45),但我仍然收到相同的错误:

selenium.common.exceptions.WebDriverException: 
Message: unknown error: Chrome failed to start: exited abnormally. 
(chrome not reachable)

推荐答案

以下是我需要添加到Chrome选项以使其工作的内容。

首先添加一个远程调试端口

chrome_options.add_argument('--remote-debugging-port=9222')

然后我还将二进制位置更改为以下位置,binary_location选项用于Google Chrome:

chrome_options.binary_location = "/usr/bin/google-chrome"

最后,如本答案(https://stackoverflow.com/a/60092331/6697714)所述,我还向Chrome驱动程序添加了可执行路径

executable_path="/usr/local/bin/chromedriver"

lambda配置保持不变:

lambda_options = [
    '--autoplay-policy=user-gesture-required',
    '--disable-background-networking',
    '--disable-background-timer-throttling',
    '--disable-backgrounding-occluded-windows',
    '--disable-breakpad',
    '--disable-client-side-phishing-detection',
    '--disable-component-update',
    '--disable-default-apps',
    '--disable-dev-shm-usage',
    '--disable-domain-reliability',
    '--disable-extensions',
    '--disable-features=AudioServiceOutOfProcess',
    '--disable-hang-monitor',
    '--disable-ipc-flooding-protection',
    '--disable-notifications',
    '--disable-offer-store-unmasked-wallet-cards',
    '--disable-popup-blocking',
    '--disable-print-preview',
    '--disable-prompt-on-repost',
    '--disable-renderer-backgrounding',
    '--disable-setuid-sandbox',
    '--disable-speech-api',
    '--disable-sync',
    '--disk-cache-size=33554432',
    '--hide-scrollbars',
    '--ignore-gpu-blacklist',
    '--ignore-certificate-errors',
    '--metrics-recording-only',
    '--mute-audio',
    '--no-default-browser-check',
    '--no-first-run',
    '--no-pings',
    '--no-sandbox',
    '--no-zygote',
    '--password-store=basic',
    '--use-gl=swiftshader',
    '--use-mock-keychain',
    '--single-process',
    '--headless',
]

for argument in lambda_options:
    chrome_options.add_argument(argument)

最终配置如下:

chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--user-data-dir={}'.format(tmp_folder + '/user-data'))
chrome_options.add_argument('--data-path={}'.format(tmp_folder + '/data-path'))
chrome_options.add_argument('--homedir={}'.format(tmp_folder))
chrome_options.add_argument('--disk-cache-dir={}'.format(tmp_folder + '/cache-dir'))
chrome_options.add_argument('--remote-debugging-port=9222')

chrome_options.binary_location = "/usr/bin/google-chrome"
driver = webdriver.Chrome(options=chrome_options, executable_path="/usr/local/bin/chromedriver")

这篇关于在单元测试的GitHub操作中运行SelensWebDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆