簡體   English   中英

如何使用 Github 操作讓 Selenium 測試在 python 中運行?

[英]How can I get Selenium tests to run in python with Github actions?

我無法讓我的 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。

我在我的 githubs 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瀏覽器。

我應該設置一個端口來監聽嗎?

我將首先回答您的問題,然后我將提供另一種方法。 我將使用統一的差異格式來突出我將對您的工作流程所做的更改。 如果您不熟悉格式,請忽略前三行,然后想象我正在從您的工作流程中刪除以“ - ”開頭的行並添加以“ + ”開頭的行。 以“開頭的行 ”保持原樣。

當您發布問題時,操作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 操作來測試庫本身,並且它們具有相當廣泛的集成測試套件,需要運行瀏覽器。 他們不使用第三方操作來設置瀏覽器環境。 他們的測試非常復雜,但您可以采取提示,采取個人行動和步驟,並根據您的需要應用它們。

重要的部分是設置 Chrome 和 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 ,沒有額外的 arguments,只有DISPLAY端口。

PS:使用第三方動作時,使用標簽。 你永遠不知道什么樣的變化會擠進別人的代碼中。 與其在 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

使用無頭 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 - 您可以使用macoswindows而不是 ubuntu

蘋果系統

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

Windows

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM