在单元测试的GitHub操作中运行SelensWebDriver

编程入门 行业动态 更新时间:2024-10-27 04:34:58
本文介绍了在单元测试的GitHub操作中运行SelensWebDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

seleniummon.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 - dl-ssl.google/linux/linux_signing_key.pub | sudo apt-key add - echo "deb dl.google/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 chromedriver.storage.googleapis/LATEST_RELEASE_${CHROME_MAJOR_VERSION%%.*}` curl -L -O "chromedriver.storage.googleapis/${CHROMEDRIVER_VERSION}/chromedriver_linux64.zip" unzip chromedriver_linux64.zip && chmod +x chromedriver && sudo mv chromedriver /usr/local/bin export CHROMEDRIVER_VERSION=`curl -s chromedriver.storage.googleapis/LATEST_RELEASE_${CHROME_MAJOR_VERSION%%.*}` curl -L -O "chromedriver.storage.googleapis/${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自己(github/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),但我仍然收到相同的错误:

seleniummon.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"

最后,如本答案(stackoverflow/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

本文发布于:2023-11-24 05:06:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624069.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   操作   GitHub   SelensWebDriver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!