首页 > 解决方案 > 在 Gitlab CI 中将 Blazor 的 E2E 测试与 Selenium 集成时修复“无法在 http://localhost 上启动驱动程序服务:...已在使用中”

问题描述

我对 CI 比较陌生,我正在尝试将 selenium 集成到 gitlab ci 中,以使用 c# 进行 dotnet blazor 项目。使用 selenium 进行的 e2e 测试正在我的机器上运行。我必须为 Firefox 和 chrome 编写它们。我曾经在 nunit 中Program.CreateHostBuilder(new string[0]).Build().RunAsync();启动应用程序进行 e2e 测试。[SetUp]

由于不同的环境,我正在读取不同的连接字符串

 if (String.CompareOrdinal(Environment.MachineName, "runner") == 0)
            {
                return configuration.GetConnectionString("Pipeline");
            }
 return configuration.GetConnectionString("Local");

这是我.gitlab-ci.yml目前的样子:

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

stages:
  - build
  - test


before_script:
    - "cd Project/Team01"
    - "dotnet restore"

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

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

  variables:
    ACCEPT_EULA: Y
    SA_PASSWORD: SomeStrongPW
    MSSQL_DB: db
    MSSQL_USER: Dev
    MSSQL_PASSWORD: DatabasPW

  script:
    - "dotnet test"

当我运行管道时,所有 e2e 都失败并显示以下错误消息:

OpenQA.Selenium.WebDriverException : Cannot start the driver service on http://localhost:43471/
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
     at OpenQA.Selenium.DriverService.Start()
   at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
...

当我向下滚动一点时,我发现以下内容:

Unable to start Kestrel.
       System.IO.IOException: Failed to bind to address http://127.0.0.1:5000: address already in use.
        ---> Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use
        ---> System.Net.Sockets.SocketException (98): Address already in use
          at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)

当我查看我在这里这里找到的第一条错误消息时,虽然遗憾的是没有连接到 CI。安装正确的 webdriver 或其他一些未正确设置的配置似乎可能是错误的。我也想知道为什么地址应该已经在使用,因为我看不到任何正在使用它的东西。

对于设置配置,我主要发现这篇文章很有用,将这个这个作为参考。

我觉得有点卡住了,因为我在非常具体的情况下只看到了一半的答案,而且我不确定 blazor 和 c# 的最佳实践是什么,因为大多数答案都是指其他编程语言。

如果您需要更多详细信息,请告诉我。提前致谢!任何提示或解决方案表示赞赏。

更新 1

我仍然遇到同样的错误,我改为

  - name: selenium/standalone-chrome:latest
  - name: selenium/standalone-firefox:latest

在我的 yml 中。我想知道如何传递有关端口的不同参数。

更新 2

我尝试将以下内容添加到gitlab-ci.yml

install:
  image: docker:stable
  script:
    - docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-firefox:4.0.0-beta-4-prerelease-20210527
    - docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:4.0.0-beta-4-prerelease-20210527
  artifacts:
    paths:
      - test

我从这个文档中得到的,也将 dotnet 的图像移到了其他阶段。

标签: c#seleniumgitlab-cie2e-testingblazor-server-side

解决方案


推荐阅读