如何在 gitlab ci 中将 chrome 和 Firefox 的 selenium 驱动程序与 dotnet blazor 服务器集成以进行端到端测试

how to integrate selenium driver for chrome and Firefox in gitlab ci with dotnet blazor server for e2e tests

我想 运行 我的 e2e 测试哪个用户 geckodriver 和 chrome 使用 gitlab ci 的驱动程序,并在 blazor 服务器项目中使用 apt 命令安装之前进行 e2e 测试。

我在我的代码中使用了以下内容:

_driverChrome = new ChromeDriver(chromeOptions);
_driverFirefox = new FirefoxDriver(firefoxOptions);

我尝试使用 gitlab-selenium-server 来实现:

image: mcr.microsoft.com/dotnet/sdk:5.0

stages:
  - build
  - test

before_script:
    - "cd Project/Team"

build:
  tags:
    - pro
  stage: build
  script:
    - "dotnet build"

test:
  tags:
    - pro
    - shared
  stage: test
  services:
  - name: mcmoe/mssqldocker:v2017.CU24.0
    alias: mssql

  variables:
    ACCEPT_EULA: Y
    SA_PASSWORD: test
    MSSQL_DB: test
    MSSQL_USER: test
    MSSQL_PASSWORD: test
    SELENIUM_REMOTE_URL: http://localhost:4545/wd/hub
    GITLAB_TARGET_SELENIUM_REMOTE_URL: http://localhost:4444/wd/hub   
    SELENIUM_REMOTE_URL: http://localhost:4545/wd/hub
    GITLAB_TARGET_SELENIUM_REMOTE_URL: http://localhost:4444/wd/hub

  script:
    # Dependencies for chromedriver, https://gist.github.com/mikesmullin/2636776#gistcomment-1742414
    # Otherwise we get this error: "error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory"
    - apt-get update -q -y
    - apt-get --yes install libnss3
    - apt-get --yes install libgconf-2-4

    #install npm
    - curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
    - apt-get install nodejs

    #add gnupg
    - apt-get update && apt-get install -y gnupg2


    # Install chrome
    # Based off of
    # - https://gitlab.com/gitlab-org/gitlab-build-images/blob/9dadb28021f15913a49897126a0cd6ab0149e44f/scripts/install-chrome
    # - https://askubuntu.com/a/510186/196148
    #
    # Add key
    - curl -sS -L https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
    # Add repo
    - echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list
    - apt-get update -q -y
    - apt-get install -y google-chrome-stable

    - npm install chromedriver -g
    - npm install https://gitlab.com/gitlab-org/gitlab-selenium-server.git -g
    # The `&` at the end causes it to run in the background and not block the following commands
    - nohup chromedriver --port=4444 --url-base=wd/hub &
    - nohup gitlab-selenium-server &

    # Run your tests
    - "dotnet test"

    # Show the logs for the GitLab Selenium Server service
    - mkdir -p selenium/ && curl -s http://localhost:4545/logs.tar.gz | tar -xvzf - -C selenium/
    - mkdir -p selenium/ && curl http://localhost:4545/server-log --output selenium/server-log.txt
  artifacts:
    when: always
    paths:
      - selenium/

我必须集成 gnup 并使用 apt 安装 npm。 现在这是失败的:

Uploading artifacts...
WARNING: selenium/: no matching files 

我的方法是否正确,如果是,我该如何进行这项工作,如果不是,那么获得 gitlab ci 运行 dotnet 的最佳方式是什么?

更新

我也尝试过使用 selenium/standalone-firefox 和 chrome 的服务:

  services:
    - name: selenium/standalone-chrome:latest
    - name: selenium/standalone-firefox:latest
    - name: mcmoe/mssqldocker:v2017.CU24.0
      alias: mssql

  variables:
    ACCEPT_EULA: Y
    SA_PASSWORD: test
    MSSQL_DB: test
    MSSQL_USER: test
    MSSQL_PASSWORD: test
    p: 5000:5000
 

  
  before_script:
    - apt-get update && apt-get install -y gnupg2
    - apt-get install gnupg

    # Install Chrome
    - wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
    - curl -sS -o - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
    - apt install -y ./google-chrome-stable_current_amd64.deb
    # Install Firefox
    - apt-get install -y firefox-esr
    - apt install chromium-chromedriver


    - "cd Project/Team"
  script: 
    - "dotnet test"

终于搞定了 运行 使用以下配置:

image: mcr.microsoft.com/dotnet/sdk:5.0

stages:
  - build
  - test

before_script:
    - "cd Project/Team"

build:
  tags:
    - pro
  stage: build
  script:
    - "dotnet build"

test:
  tags:
    - pro
  stage: test
  services:
    - selenium/standalone-firefox:latest
    - selenium/standalone-chrome:latest
    - name: mcmoe/mssqldocker:v2017.CU24.0
      alias: mssql

  variables:
    ACCEPT_EULA: Y
    SA_PASSWORD: te!n
    MSSQL_DB: test
    MSSQL_USER: test
    MSSQL_PASSWORD: test
 
test:
  tags:
    - sopro
  stage: test
  services:
    - selenium/standalone-firefox:latest
    - selenium/standalone-chrome:latest
    - name: mcmoe/mssqldocker:v2017.CU24.0
      alias: mssql

  variables:
    ACCEPT_EULA: Y
    SA_PASSWORD: S0Pr04dm!n
    MSSQL_DB: soprodb
    MSSQL_USER: SoProDev
    MSSQL_PASSWORD: SoPro2021
 

  
  before_script:
    - apt-get update && apt-get install -y gnupg2
    - . install_chrome_and_chromedriver.sh
    - . install_firefox_and_geckodriver.sh
  

    - "cd Project/Team"
  script:
    - *install_firefox_geckodriver
    - "dotnet test"

install_chrome_and_chromedriver.sh:

 # Install Chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
curl -sS -o - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
apt install -y ./google-chrome-stable_current_amd64.deb
# Install Chromedriver
apt-get install -yqq unzip curl
wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/

install_firefox_and_geckodriver.sh:

apt-get update && apt-get upgrade --assume-yes
apt-get install gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils --assume-yes
 wget -nv -O ~/FirefoxSetup.tar.bz2 "https://download.mozilla.org/?product=firefox-latest&os=linux64"
tar xjf ~/FirefoxSetup.tar.bz2 -C /opt/
ln -s /opt/firefox/firefox /usr/lib/firefox
export PATH=$PATH:/opt/firefox/
wget -nv -O ~/geckodriver.tar.gz "https://github.com/mozilla/geckodriver/releases/download/v0.28.0/geckodriver-v0.28.0-linux64.tar.gz"
tar -zxvf ~/geckodriver.tar.gz -C /opt/
export PATH=$PATH:/opt/

仅供理解。如我所见,您 steel 使用 server-standalone 但还手动安装 browser/driver 吗? 独立图像已包含这两项。

  • attach服务selenium/standalone-chrome:gitlab最新-ci.yml

  • 添加别名,例如 chrome

    name: selenium/standalone-chrome:latest
    alias: chrome
    
  • 在您的源代码中使用别名初始化远程驱动程序 (python 示例):

remote_url = "http://your_alias:4444/wd/hub"
wd = webdriver.Remote(remote_url, desired_capabilities=cap,options=opt)