运行 UI 在 GitLab CI/CD 管道中使用 Chrome 驱动程序进行测试

Running UI tests with Chrome Driver in GitLab CI/CD pipeline

我 运行 在尝试对使用 ChromeDriver 的网站进行 运行 单元测试时遇到问题。这是来自作业日志的堆栈跟踪:

OpenQA.Selenium.WebDriverException : Cannot start the driver service on http://localhost:37329/
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
StackTrace:    at OpenQA.Selenium.DriverService.Start()
   at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.WebDriver.StartSession(ICapabilities desiredCapabilities)
   at OpenQA.Selenium.WebDriver..ctor(ICommandExecutor executor, ICapabilities capabilities)
   at OpenQA.Selenium.Chromium.ChromiumDriver..ctor(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options, TimeSpan commandTimeout)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory)
   at ScamTeamWebsiteTests.Tests.Setup() in /builds/kostyabek/ScamTeamWebsiteTests/ScamTeamWebsiteTests/WebSiteTests.cs:line 19

.yml 文件内容

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

stages:
  - build
  - test

variables:
  projFolder: "ScamTeamWebsiteTests"
  projName: "ScamTeamWebsiteTests.csproj"

before_script:
  - "apt-get update -qy"
  - "apt-get -y install zip unzip"
  - "cd ${projFolder}"
  - "wget https://chromedriver.storage.googleapis.com/102.0.5005.27/chromedriver_linux64.zip"
  - "unzip chromedriver_linux64.zip -d '/home/kostyabek'"

build:
  stage: build
  script:
    - "dotnet build"

unit-test:
  stage: test
  script:
    "dotnet test ${projName}"

基本上,我不知道该怎么办。我对 CI/CD 管道和 Selenium 都比较陌生。

您需要 运行 selenium chrome 实例与您的 unit-test 阶段。可能不需要下载 chrome 驱动程序。

unit-test:
  stage: test
  services:
    - name: selenium/standalone-chrome:latest 
  script:
    - dotnet test ${projName}

当 运行 在 CI 测试环境中时,您需要配置测试套件以连接到 http://selenium__standalone-chrome:4444/wd/hub 的远程 Web 驱动程序。