如何在Github actions中使用Python运行Selenium测试?

10

我有些困难在Github Actions中运行我的Python Selenium。

过去一年,我一直在使用Circle CI,但最近开始迁移到Github Actions。

为了让Circle CI在Chrome浏览器中运行Selenium,我在config.yml文件中添加了以下几行:

docker:
    # includes chrome browser for selenium testing
  - image: circleci/python:3.7.4-browsers

而且似乎不需要安装chromedriver。

我在我的github action .yml文件中使用以下内容:

jobs:
  build:
    runs-on: ubuntu-latest
    services:
      selenium:
        image: selenium/standalone-chrome
    steps:
    - uses: actions/checkout@v1
    - name: Set up Python 3.7
      uses: actions/setup-python@v1
      with:
        python-version: 3.7
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install pipenv
        pipenv install
    - name: Prepare Selenium
      # https://github.com/marketplace/actions/setup-chromedriver
      uses: nanasess/setup-chromedriver@master
    - name: Launch browser
      run: |
        google-chrome --version
        export DISPLAY=:99
        chromedriver --url-base=/wd/hub &
        sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional, disables headless mode
    - name: Run tests
      run: pipenv run python manage.py test functional_tests.tests.test_selenium.test_exams -v 2

但是当我在Python代码中尝试运行时,会出现以下错误:
from selenium import webdriver
driver = webdriver.Chrome()

  File "/home/runner/.local/share/virtualenvs/lang-EMCZ4oUT/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/home/runner/.local/share/virtualenvs/lang-EMCZ4oUT/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/runner/.local/share/virtualenvs/lang-EMCZ4oUT/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/runner/.local/share/virtualenvs/lang-EMCZ4oUT/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/runner/.local/share/virtualenvs/lang-EMCZ4oUT/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

根据我在网上的阅读,我只需要使用 uses: nanasess/setup-chromedriver@master,不需要使用 image: selenium/standalone-chrome,但无论是将其切换进入还是退出,Python测试仍然找不到Chrome浏览器。
我是否应该设置一个监听端口?
3个回答

9
我将首先回答你的问题,然后提供一种替代方法。我将使用统一差异格式来突出显示我对你的工作流程所做的更改。如果您不熟悉这种格式,请忽略前三行,然后想象我正在删除以“-”开头的行,并添加以“+”开头的行。“ ”开头的行保留不变。
当您发布问题时,动作nanasess/setup-chromedriver下载了Chrome浏览器和chromedriver(版本为v1.0.1)。截至本文撰写时,它仍然执行相同操作(v1.0.5)。因此,您不需要额外的服务容器来运行Chrome浏览器和chromedriver-它们已经在主要容器中。
--- original.yml    2020-06-13 20:42:25 +0000
+++ step1.yml   2021-04-23 00:01:00 +0000
@@ -1,9 +1,6 @@
 jobs:
   build:
     runs-on: ubuntu-latest
-    services:
-      selenium:
-        image: selenium/standalone-chrome
     steps:
     - uses: actions/checkout@v1
     - name: Set up Python 3.7

你不需要“启动浏览器”这一步骤。Selenium库将为您执行此操作。默认情况下,它会执行本地的chromedriver二进制文件,该文件默认情况下会执行Chrome浏览器二进制文件。如果您不想使用无头模式,则仍然必须启动虚拟帧缓冲区(但不需要其他任何内容):
--- step1.yml   2021-04-23 00:01:00 +0000
+++ step2.yml   2021-04-23 00:02:00 +0000
@@ -15,11 +15,10 @@
     - name: Prepare Selenium
       # https://github.com/marketplace/actions/setup-chromedriver
       uses: nanasess/setup-chromedriver@master
-    - name: Launch browser
+    - name: Start XVFB
       run: |
-        google-chrome --version
-        export DISPLAY=:99
-        chromedriver --url-base=/wd/hub &
         sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional, disables headless mode
     - name: Run tests
       run: pipenv run python manage.py test functional_tests.tests.test_selenium.test_exams -v 2
+      env:
+        DISPLAY: :99

这样就可以了。注意添加了与启动XVFB时相同的DISPLAY端口的env

我猜想你分享的错误是由chromedriver和你启动和控制的Google Chrome以及你的测试套件试图启动和控制的浏览器之间的冲突引起的。

我的替代方法是什么?个人而言,我在引入第三方依赖时有些谨慎。特别是对于只需几行代码的事情。当寻找灵感时,我尽可能地靠近源头。那么,Selenium是如何测试他们的代码的呢?

有趣的是,Selenium项目正在使用GitHub Actions来测试他们自己的库,他们有相当广泛的集成测试套件需要运行浏览器。他们不使用第三方操作来设置浏览器环境。他们的测试非常复杂,但你可以参考一下,根据你的需求采取个别操作和步骤,并应用它们。

重要的部分是设置Chrome和chromedriver的操作。与您使用的Kentaro Ohkouchi(又名nanasess)的操作相比,这个操作很直接且基本相同。
接下来重要的部分是启动虚拟帧缓冲器。如果您使用无头模式,则不需要它。他们的启动方式与您或Ohkouchi的示例相比非常简单:
--- step2.yml   2021-04-23 00:02:00 +0000
+++ step3.yml   2021-04-23 00:03:00 +0000
@@ -18,3 +18,3 @@
     - name: Start XVFB
       run: |
-        sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional, disables headless mode
+        Xvfb :99 &

没有sudo,没有额外的参数,只需要DISPLAY端口。
附注:当使用第三方操作时,请使用标签。你永远不知道什么样的变化会挤入别人的代码中。与其盲目地相信有人会为你做出正确的改变,不如让测试在CI期间失败,调查发生了什么变化,然后提升版本。
--- step3.yml   2021-04-23 00:03:00 +0000
+++ step4.yml   2021-04-23 00:04:00 +0000
@@ -15,3 +15,3 @@
     - name: Prepare Selenium
       # https://github.com/marketplace/actions/setup-chromedriver
-      uses: nanasess/setup-chromedriver@master
+      uses: nanasess/setup-chromedriver@v1.0.5

0

请查看chromedriver-autoinstaller Python软件包。

在你的脚本中加入以下代码,即可自动检查并下载最新版本的chromedriver,并将其添加到路径中。

chromedriver_autoinstaller.install()

你也应该看看这个模板


-1

使用无头Chromedriver

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)

如果您需要带有图形用户界面的Chrome - 您可以使用macOSwindows而不是Ubuntu。

MacOS

jobs:
  build:
    runs-on: macos-latest
    ...

Windows

jobs:
  build:
    runs-on: windows-latest
    ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接