首页 > 解决方案 > 如何使用 selenium 网格和 c# 在多个节点上同时/并行执行测试?

问题描述

我需要在具有不同配置的多个节点上使用 selenium 网格执行 selenium UI 测试。目前,我只能在给定时间使用 RemoteWebDriver 和所需功能在一台机器上执行测试。我的代码如下:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;

namespace ClassLibrary1.Test
{
[TestFixture]

public class Test1 : BaseTestClass
{
    public RemoteWebDriver driver = null;
    [SetUp] 
    public void setup()
    {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.SetCapability(CapabilityType.BrowserName, "chrome");
        capabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows));

        driver = new RemoteWebDriver(new Uri(" http://152.17.100.217:5454/wd/hub"), capabilities);
        driver.Manage().Window.Maximize();
    }
    [TearDown] 
    public void teardown()
    {
        driver.Quit();
    }
    [Test]
    //[Parallelizable]
    public void test1()
    {


        driver.Navigate().GoToUrl("https://www.google.com");
        Thread.Sleep(5000);
        IWebElement query = driver.FindElement(By.Name("q"));
        query.SendKeys("test");
        query.Submit();
        Thread.Sleep(15000);

    }
    [Test]
    //[Parallelizable]
    public void test2()
    {

        driver.Navigate().GoToUrl("https://www.google.com");
        Thread.Sleep(5000);
        IWebElement query = driver.FindElement(By.Name("q"));
        query.SendKeys("selenium ");
        query.Submit();
        Thread.Sleep(15000);

    }

}
  }

我需要通过 VSTS 在服务器上执行大约 250 多个测试,还需要为不同的配置实现 selenium 网格。有了这个,我一次只能在一台服务器上执行这些测试,然后我需要手动更改功能以在另一台服务器上执行它。我该怎么做才能让它同时在多台机器上运行。我知道 Java 可以使用 xml 文件并为机器配置和测试文件添加节点,但不能使用 c# 来做到这一点。之后,我打算使用 VSTS 构建来触发它。有什么办法可以实现吗??

标签: c#seleniumselenium-webdriverazure-devopsselenium-grid

解决方案


如果您想在每台服务器上执行不同的功能,您可能需要为此使用 C# 构建配置和 XML 转换。

您的设置将按如下方式工作:

  • 多个 .config 文件——每个配置文件包含每个服务器的功能—— server1.configserver2.config等等。

  • 针对所需的 .config 文件运行测试

这样,您可以指定要在每台服务器上执行的功能。

您将需要用于 XML 转换的SlowCheetah包扩展。

您需要使用此处ConfigurationManager.AppSettings指定的 .configs 读取。

这是一个开放式问题的通用答案。许多这项工作和研究需要您自己完成。如果您遇到特定问题或错误消息,可以在此处打开一个新问题以进行更有针对性的故障排除。


推荐阅读