使用 Selenium、.NET 和 Azure DevOps 进行自动化测试

作者 : 慕源网 本文共4867个字,预计阅读时间需要13分钟 发布时间: 2021-10-28 共531人阅读

Selenium + .NET 是为 Web 和桌面应用程序创建自动化测试的良好组合。

要为 Web 应用程序创建我们的第一个自动化项目,我们只需要使用以下命令(或使用 Visual Studio)创建一个 dotnet 库,

dotnet new classlib

之后,您必须添加一个用于单元测试的库,您可以使用自己喜欢的库。我建议使用 xUnit,因为它非常简单且易于实现。您可以使用以下命令(或 Visual Studio 中的 NuGet 包管理器)。

dotnet add package xunit

然后我们需要使用以下命令添加 Selenium 库,

dotnet add package Selenium.WebDriver

此外,对于网络应用程序,您需要安装 Chrome 网络驱动程序。但是,也有其他浏览器的驱动程序,但 chrome 驱动程序始终更新(推荐)。

你可以从这个 URL 获取它:ChromeDriver – WebDriver for Chrome (chromium.org)

并且您需要使用以下命令在您的项目中安装 Selenium Chrome 网络驱动程序,

dotnet add package Selenium.WebDriver.ChromeDriver

注意
ChromeDriver 必须根据 Chrome 版本进行更新。这非常重要,因为如果您使用的是旧版本,测试将不会开始。ChromeDriver 版本取决于 Chrome 浏览器版本。

此时,您可以开始您的第一个测试。

对于此演示,我们将对该站点进行测试 –  https://mango-flower-00e2aa010.azurestaticapps.net/

这是一个简单的 React.js 应用程序,我们可以在其中选择颜色,其想法是测试 div 容器是否根据所选颜色更改背景。

为此,我们只需要创建一个 chromedriver 会话,导航到该站点,然后使用 FindElement 方法就可以与该站点进行交互。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using Xunit;

namespace dotnetseleniumdemo
{    public class ChangeColorTest
    {
        public IWebDriver driver;

        public readonly string site;

        public ChangeColorTest()
        {
            driver = new ChromeDriver();
            driver.Manage().Window.Maximize();
            site = "https://mango-flower-00e2aa010.azurestaticapps.net/";
        }

        [Fact]
        public void ChangeToBlueColor()
        {
            driver.Navigate().GoToUrl(site);
            System.Threading.Thread.Sleep(2000);
            var blueBox =  driver.FindElement(By.ClassName("color-blue"));
            blueBox.Click();
            System.Threading.Thread.Sleep(2000);
            var container =  driver.FindElement(By.ClassName("color-container"));

            Assert.Equal("color-container color-blue", container.GetAttribute("class"));
            driver.Close();
            driver.Quit();
            driver.Dispose();
        }
     }
}

在示例中,我们使用类名通过 FindElement(By.ClassName()) 方法获取元素,但您可以获取元素 ById、XPath 等。

Sleeps 方法可用于查看有一些延迟的进程并查看 selenium 如何在浏览器中导航,

此外,使用断言来检查我们是否得到了预期的结果非常重要。请记住,您正在创建一个测试,即使整个工作流程都有效,结果也可能是错误的。

在这种情况下,我们使用 xUnit 和 Assert.Equal 方法来验证值。方法 GetAttribute(“class”) 返回 div 容器的当前 CSS 类,“color-container color-blue”是预期结果,

Assert.Equal("color-container color-blue", container.GetAttribute("class"));

这是红色的另一个示例,但这次使用 XPath 获取元素,

[Fact]
public void ChangeToRedColor() {
    driver.Navigate().GoToUrl(site);
    System.Threading.Thread.Sleep(1000);
    var redBox = driver.FindElement(By.XPath("//*[@id=\"root\"]//button[3]"));
    redBox.Click();
    System.Threading.Thread.Sleep(1000);
    var container = driver.FindElement(By.ClassName("color-container"));
    Assert.Equal("color-container color-red", container.GetAttribute("class"));
    driver.Close();
    driver.Quit();
    driver.Dispose();
}

查看代码

注意
要执行测试,请将 chrome 驱动程序库更新到最新版本。

Include="Selenium.WebDriver.ChromeDriver" Version="94.0.4606.81"

Azure 开发运营

现在让我们在 Azure DevOps 中执行我们的测试。Azure DevOps 是一个了不起的 DevOps 套件,具有适用于整个开发过程的功能和工具。

要在 Azure DevOps 中自动部署和执行自动化测试,我们需要使用管道模块和发布。

对于这个项目,我们必须创建一个简单的管道,其中包含 .NET 应用程序的常规命令(恢复、构建、发布),最后,我们必须发布工件以在下一步中使用它。

YAML 代码

pool:
  name: Azure Pipelines

steps:
- task: DotNetCoreCLI@2
  displayName: Restore
  inputs:
    command: restore
    projects: '$(Parameters.RestoreBuildProjects)'

- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    projects: '$(Parameters.RestoreBuildProjects)'
    arguments: '--configuration $(BuildConfiguration)'

- task: DotNetCoreCLI@2
  displayName: Publish
  inputs:
    command: publish
    publishWebProjects: false
    projects: '$(Parameters.RestoreBuildProjects)'
    arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)'
    zipAfterPublish: false
    modifyOutputPath: false

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact'
  inputs:
    PathtoPublish: '$(build.artifactstagingdirectory)'
  condition: succeededOrFailed()

然后,导航到发布部分并创建一个新版本,

我们的新发布必须选择通过管道创建的工件,然后在发布管道中执行它,

第 1 阶段将使用“dotnet test”命令获取工件并执行测试,但首先,我们需要在代理中安装 ChromeDriver,因为默认情况下未安装它,我们需要选择我们想要使用的版本。

在下一页上,您可以找到所有可用版本并选择要安装在代理中的zip 文件。

这个 PowerShell 脚本负责获取 ChromeDriver 并将其安装在代理中,

Invoke-WebRequest -Uri "https://chromedriver.storage.googleapis.com/92.0.4515.43/chromedriver_win32.zip" -OutFile $(System.DefaultWorkingDirectory)/chromedriver_win32.zip;
Expand-Archive $(System.DefaultWorkingDirectory)/chromedriver_win32.zip -DestinationPath $(System.DefaultWorkingDirectory)/chromedriver_win32;
Copy-Item "$(System.DefaultWorkingDirectory)/chromedriver_win32/chromedriver.exe" -Destination "$(System.DefaultWorkingDirectory)/"

YAML文件

steps:
- powershell: |
   Invoke-WebRequest -Uri "https://chromedriver.storage.googleapis.com/92.0.4515.43/chromedriver_win32.zip" -OutFile $(System.DefaultWorkingDirectory)/chromedriver_win32.zip;
   Expand-Archive $(System.DefaultWorkingDirectory)/chromedriver_win32.zip -DestinationPath $(System.DefaultWorkingDirectory)/chromedriver_win32;
   Copy-Item "$(System.DefaultWorkingDirectory)/chromedriver_win32/chromedriver.exe" -Destination "$(System.DefaultWorkingDirectory)/"
  displayName: 'PowerShell Script'

然后,在下一步中,我们需要使用 dotnet CLI 执行测试,

YAML文件

steps:
- task: DotNetCoreCLI@2
  displayName: 'running selenium'
  inputs:
    command: test
    projects: '**/dotnetseleniumdemo.dll'
    arguments: '--no-build'
    workingDirectory: '$(System.DefaultWorkingDirectory)/_ReactDemoColors-ASP.NET Core-CI/drop'

按照这些步骤,我们将在 Azure DevOps 中运行我们的测试,然后通过检查日志获得结果,

最后,如果您有可用的测试部分,您可以转到“运行”部分并查看更多详细信息的结果,


慕源网 » 使用 Selenium、.NET 和 Azure DevOps 进行自动化测试

常见问题FAQ

程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!

发表评论

开通VIP 享更多特权,建议使用QQ登录