如何正确管理和访问Webdriver实例以避免并行执行测试的问题? [英] How to properly manage and access webdriver instances to avoid problems with parallel execution of tests?

查看:56
本文介绍了如何正确管理和访问Webdriver实例以避免并行执行测试的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Specflow示例中,我已经看到了几种实例化Web驱动程序的方法。

I've seen several approaches to instantiate web driver in Specflow examples.


  1. 在步骤定义类中创建它并将其放置在处理类的方法

  1. Creating it in the steps definition class and disposing it in Dispose method for the class

为什么这么恐怖?原因1场景不等于1步骤定义类,因为某些步骤仅在功能之间共享,并且实例化了多个Web驱动程序。示例: https://www.softwaretestinghelp.com/specflow-and-selenium/

Why is it horrible? Cause 1 scenario doesn't equal 1 steps definition class as some steps are just shared between features and there will be more than 1 web driver instantiated. Example: https://www.softwaretestinghelp.com/specflow-and-selenium/


  1. 在挂钩中创建它 [BeforeScenario] 并销毁int in [AfterScenario]

  1. Creating it in hooks [BeforeScenario] and destroying int in [AfterScenario]

它不适用于并行执行(根据作者)。 https://github.com/AutomateThePlanet/AutomateThePlanet- Learning-Series / tree / master / Specflow-Series / ExtendTestExecutionWorkflowUsingHooks

It won't work with parallel execution(according to author). https://github.com/AutomateThePlanet/AutomateThePlanet-Learning-Series/tree/master/Specflow-Series/ExtendTestExecutionWorkflowUsingHooks

问题:如何管理 Specflow UI中的WebDriver 实例是否使用NUnit测试解决方案?

Question: How to manage WebDriver instances in Specflow UI tests solution with NUnit? Where and when initialize it, where and when destroy and how to access it in page object models and steps definition classes?

推荐答案

您在何处,何时初始化,何时何地销毁以及如何在页面对象模型和步骤定义类中对其进行访问?需要使用SpecFlow随附的依赖项注入框架。选择#2,在 [BeforeScenario] 中创建它并在 [AfterScenario] 中销毁它是正确的方法这样做,但是您需要向依赖注入框架注册 IWebDriver 对象:

You need to use the dependency injection framework that comes with SpecFlow. Option #2 where you create it in the [BeforeScenario] and destroy it in the [AfterScenario] is the right way to do, but then you need to register the IWebDriver object with the dependency injection framework:

[Binding]
public class WebDriverHooks
{
    private readonly IObjectContainer container;

    public WebDriverHooks(IObjectContainer container)
    {
        this.container = container;
    }

    [BeforeScenario]
    public void CreateWebDriver()
    {
        FirefoxDriver driver = new FirefoxDriver();

        // Make 'driver' available for DI
        container.RegisterInstanceAs<IWebDriver>(driver);
    }

    [AfterScenario]
    public void DestroyWebDriver()
    {
        var driver = container.Resolve<IWebDriver>();

        if (driver != null)
        {
            driver.Quit();
            driver.Dispose();
        }
    }
}

您的步骤定义需要接受IWebDriver对象作为构造函数参数。您甚至可以使用DI框架注册页面对象。

Your step definitions need to accept an IWebDriver object as a constructor argument. You could even register your page objects with the DI framework too.

[Binding]
public class LoginSteps
{
    private readonly IWebDriver driver;
    private readonly LoginPage loginPage;

    public LoginSteps(IWebDriver driver)
    {
        // Assign 'driver' to private field or use it to initialize a page object
        this.driver = driver;

        // Initialize Selenium page object
        this.loginPage = new LoginPage(driver);
    }

    [When(@"I go to the login page")]
    public void WhenIGoToTheLoginPage()
    {
        // Use 'driver' in step definition
        driver.FindElement(By.LinkText("Sign In")).Click();
    }

    [When(@"I log in")]
    public void WhenILogIn()
    {
        // Use Selenium page object in step definition
        loginPage.LogIn("testUser", "testPassword");
    }
}

在您引用的GitHub项目中,Web驱动程序对象初始化为静态属性。这就是为什么该代码示例不能用于并行测试的原因。听起来所有正在执行的方案都使用相同的AppDomain,因此它们共享静态类状态,这意味着每种方案都尝试使用相同的浏览器实例。

In the GitHub project you referenced, the web driver object is initialized in as a static property. This is the reason why that code example cannot be used for parallel tests. It sounds like all executing scenarios are using the same AppDomain, so they share static class state, which means each scenario is attempting to use the same browser instance.

这篇关于如何正确管理和访问Webdriver实例以避免并行执行测试的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆