如何在GitHub Actions中运行Selenium测试

4

我正在尝试找到在 GitHub Actions 中运行 Selenium 测试的等效命令。在 Azure DevOps 中,我会使用以下 YAML 文件来运行“Visual Studio Test”:

- task: VSTest@2
  displayName: 'Run functional smoke tests on website and web service'
  inputs:
    searchFolder: '$(build.artifactstagingdirectory)'
    testAssemblyVer2: |
      **\FeatureFlags.FunctionalTests\FeatureFlags.FunctionalTests.dll
    uiTests: true
    runSettingsFile: '$(build.artifactstagingdirectory)/drop/FunctionalTests/FeatureFlags.FunctionalTests/test.runsettings'
    overrideTestrunParameters: |
     -ServiceUrl "https://$(WebServiceName)-staging.azurewebsites.net/" 
     -WebsiteUrl "https://$(WebsiteName)-staging.azurewebsites.net/"  

在GitHub Actions中,等价的任务是什么?似乎GitHub runner/agent中不存在VsTest.Console.exe,所以答案可能需要安装Visual Studio Test Platform installer - 但显然我想避免这样做,因为这会严重拖慢每个构建。

1
看起来跑步者已经拥有了Visual Studio Enterprise,请查看此链接:https://help.github.com/en/actions/automating-your-workflow-with-github-actions/software-installed-on-github-hosted-runners#windows-server-2019 - Eldar
谢谢,看起来我在C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\TestPlatform\vstest.console.exe找到了它。我会尝试一下看看是否有效。 - Sam
顺便说一句,我是通过搜索代理找到它的(最后我会清理这个)- name: 搜索Visual Studio测试运行程序 run: | $var1 = Get-Childitem -Path "C:\Program Files (x86)\Microsoft Visual Studio" -Filter "vstest.console.exe" -Recurse | select -ExpandProperty FullName Write-Host "VS测试运行程序:$var1" shell: powershell - Sam
2个回答

2

感谢 @Eldar 帮助我找到了正确的方向。基本答案是(在Windows运行器中运行):

- name: Functional Tests
  run: |
    $vsTestConsoleExe = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\Common7\\IDE\\Extensions\\TestPlatform\\vstest.console.exe"
    $targetTestDll = "functionaltests\FeatureFlags.FunctionalTests.dll"
    $testRunSettings = "/Settings:`"functionaltests\test.runsettings`" "
    #Note that the `" is an escape character to quote strings, and the `& is needed to start the command
    $command = "`& `"$vsTestConsoleExe`" `"$targetTestDll`" $testRunSettings " 
    Write-Host "$command"
    Invoke-Expression $command
  shell: powershell

使用Invoke-Expression似乎有助于解决我最初遇到的一些错误。完整的yaml内容及其上下文可以在GitHub上查看:
请注意,为了找到vstest.console.exe文件,我使用了这个yaml来搜索GitHub Actions runner。有一些被锁定的文件夹,因此如果在根目录C:文件夹中搜索,则会出现错误。通过上面提供的链接,我能够确定Visual Studio的根文件夹并在其中搜索vstest.console.exe。
- name: search for visual studio test runner
  run: |
    $var1 = Get-Childitem -Path "C:\Program Files (x86)\Microsoft Visual Studio" -Filter "vstest.console.exe" -Recurse | select -ExpandProperty FullName
    Write-Host "VS test runner: $var1"
  shell: powershell

1

由于市场上提供了Setup VSTeset.console.exe操作,现在这变得更加简单。

仍然需要在Windows代理上运行,但YAML行数显着减少。

以下代码片段假定一个名为“functional-tests”的工件包含已编译的资产(DLL、chromedriver.exe等)用于您的Selenium测试已被上传,然后下载到“./functional-tests/”中。

- name: Add VSTest.console.exe to the PATH
  uses: darenm/Setup-VSTest@v1

- name: Run functional tests
  run: vstest.console.exe "functional-tests\<functional-tests-project-dll-name-here>.dll"

我的经验是,如果你在作业级别设置环境变量,那么vstest.console.exe会自动捕获它们。这对于在步骤或工作流级别设置它们也可能是正确的,但我没有测试过。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接