【问题标题】:How can I get Selenium tests to run in python with Github actions?如何使用 Github 操作让 Selenium 测试在 python 中运行?
【发布时间】:2020-10-03 11:27:14
【问题描述】:

我无法让我的 python Selenium 在 github 操作中运行。

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

为了让 Circle CI 在 chrome 浏览器中运行 selenium,我的 config.yml 中有以下几行:

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

而且似乎不需要安装 chromedriver。

我在我的 github 操作 .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 浏览器。

我应该设置一个端口来监听吗?

【问题讨论】:

    标签: python selenium selenium-chromedriver github-actions


    【解决方案1】:

    我将首先回答您的问题,然后我将提供另一种方法。我将使用unified diff format 来强调我将对您的工作流程所做的更改。如果您不熟悉格式,请忽略前三行,然后想象我正在从您的工作流程中删除以“-”开头的行并添加以“+”开头的行。以“ ”开头的行保持原样。

    当您发布问题时,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
    

    这应该可以解决问题。注意添加 env 与启动 XVFB 时传递的相同 DISPLAY 端口。

    我的猜测是,您在此处分享的错误是由于您启动的 chromedriver 和 Google Chrome 与您的测试套件试图启动和控制的 Chromedriver 之间的冲突而发生的。

    我的替代方法是什么?在引入第三方依赖时,我个人还是比较谨慎的。特别是对于应该只是几行代码的事情。而在寻找灵感时,我会尽量靠近源头。那么,Selenium 是如何测试他们的代码的呢?

    有趣的是,Selenium 项目正在使用 GitHub Actions 来测试库本身,并且它们具有相当广泛的集成测试套件,需要运行浏览器。他们不使用第三方操作来设置浏览器环境。他们的测试相当复杂,但您可以得到提示,采取个人行动和步骤,并根据您的需要应用它们。

    重要的部分是action to setup Chrome and chromedriver。与您正在使用的大口健太郎(又名 nanases)的动作相比,直截了当且几乎相同。

    下一个重要部分是启动虚拟帧缓冲区。同样,如果您使用无头模式,则不需要它。与您或 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 端口。

    P.S.:使用第三方操作时,请使用标签。你永远不知道什么样的变化会挤进别人的代码中。与其在 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
    

    【讨论】:

      【解决方案2】:

      使用无头 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)
      

      如果您需要带有 GUI 的 Chrome - 您可以使用 ma​​coswindows 代替 ubuntu

      MacOS

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

      Windows

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

      【讨论】:

        猜你喜欢
        • 2021-06-26
        • 2014-03-12
        • 1970-01-01
        • 2021-05-16
        • 1970-01-01
        • 1970-01-01
        • 2021-12-30
        • 1970-01-01
        • 2020-10-11
        相关资源
        最近更新 更多