【问题标题】:How do I implement parallel execution on Specflow/Selenium/C#/NUnit?如何在 Specflow/Selenium/C#/NUnit 上实现并行执行?
【发布时间】:2019-07-11 08:33:48
【问题描述】:

我有一个按顺序运行测试的现有项目,我正在尝试实现并行执行。

我最初将这些属性添加到 AssemblyInfo.cs

[assembly: Parallelizable(ParallelScope.Fixtures)]
[assembly: LevelOfParallelism(2)]

为了看到正在尝试并行执行,我必须创建两个功能,每个功能都有一个测试,然后在 Visual Studio 的测试资源管理器中运行它们。这试图同时运行每个功能中的每个测试。

我有一个测试设置为在 Chrome 中运行,另一个在 Firefox 中运行。这也是 webdriver 调用浏览器实例的顺序。

Chrome 先打开,然后再打开 Firefox - 但 Chrome 是孤立的,测试只能在 Firefox 中进行。

我相信这是因为我的网络驱动程序是静态的,所以 Firefox 正在劫持 Chrome 使用的线程。我读到我不能使用静态 webdriver 进行并行测试,所以我正在尝试使用非静态 webdriver。

看来我现在必须在方法之间传递驱动程序,以确保所有操作都在该特定实例上进行。

在非静态实现 webdriver 之后,我首先尝试确保运行单个测试,然后再尝试并行运行所有测试。

但我遇到了障碍。在以下测试中,驱动程序在第二(何时)步骤开始时被重置为空:

Scenario Outline: C214 Log in
    Given I launch the site for <profile> and <environment> and <parallelEnvironment>
    When I log in to the Normal account
    Then I see that I am logged in

        Examples:
        | profile | environment | parallelEnvironment |
        | single  | Chrome75    |                     |
        #| single  | Firefox67   |                     |

如何使非静态 webdriver 在步骤之间保持不变?

ThreadLocal 是答案吗?如果是这样,如果我想在 Windows 桌面、Android 和 iOS 设备上在 Selenium Grid 中使用这种并行执行,那么以后使用它会不会有问题?

这是我的设置:

SetUp.cs

using TechTalk.SpecFlow;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;

namespace OurAutomation
{
    [Binding]
    public class SetUp
    {
        public IWebDriver Driver;
        public string theEnvironment;

        public IWebDriver InitialiseDriver(string profile, string environment, string parallelEnvironment)
        {
            theEnvironment = environment;

            if (profile == "single")
            {
                if (environment.Contains("IE"))
                {
                    Driver = new InternetExplorerDriver();
                }
                else if (environment.Contains("Edge"))
                {
                    Driver = new EdgeDriver();
                }
                else if (environment.Contains("Chrome"))
                {
                    Driver = new ChromeDriver(@"C:\Automation Test Drivers\");
                }
                else if (environment.Contains("Firefox"))
                {
                    Driver = new FirefoxDriver(@"C:\Automation Test Drivers\");
                }
            }

            Driver.Manage().Window.Maximize();

            Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

            return Driver;
        }

        [AfterScenario]
        public void AfterScenario()
        {
            Driver.Quit();
        }
    }
}

BaseSteps.cs

using OpenQA.Selenium;

namespace OurAutomation.Steps
{
    public class BaseSteps : SetUp
    {
        public IWebDriver driver;

        public void DriverSetup(string profile, string environment, string parallelEnvironment)
        {
            driver = InitialiseDriver(profile, environment, parallelEnvironment);
        }
    }
}

LaunchTestSteps.cs

using OurAutomation.BaseMethods;
using OurAutomation.Pages;
using TechTalk.SpecFlow;

namespace OurAutomation.Steps
{
    [Binding]
    public class LaunchTestSteps : BaseSteps
    {
        [Given(@"I launch the site for (.*) and (.*) and (.*)")]
        public void ILaunchTheSite(string profile, string environment, string parallelEnvironment)
        {
            DriverSetup(profile, environment, parallelEnvironment);
            new Common().LaunchSite(driver);
            new Core().Wait(10, "seconds");
        }
    }
}

还有更多,但不确定是否需要完整的套件来解决这个问题。就我的致命错误而言,也许这很明显!

【问题讨论】:

    标签: c# selenium


    【解决方案1】:

    经过一番折腾,找到了https://github.com/minhhoangvn/AutomationFramework,它使我能够剥离必要的代码以使用 ThreadLocal 实现并行测试。

    【讨论】:

    • 嗨,你使用硒网格吗?您对使用 selenium 网格并行运行的移动测试有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2017-03-05
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多