首页 > 解决方案 > 如何在 gitlab ci 中将 chrome 和 Firefox 的 selenium 驱动程序与 dotnet blazor 服务器集成以进行 e2e 测试

问题描述

我想在 blazor 服务器项目中运行我的 e2e 测试,其中用户 geckodriver 和 chromedriver 使用 gitlab ci 并使用 apt 命令在 blazor 服务器项目中进行 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"

标签: .netseleniumselenium-webdrivergitlab-cie2e-testing

解决方案


只是为了我的理解。正如我所见,您使用独立服务器但还手动安装了浏览器/驱动程序?独立图像已经包含这两个项目。

  • 附加服务 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)

推荐阅读