多步文件打开多个浏览器

问题描述 投票:2回答:3

The Problem:

如果我有多个Steps文件,那么当我执行测试时,无论我运行哪个测试,似乎都会为每个测试创建WebDriver。

每当我运行测试时,我都会看到一个看似随机的Chrome浏览器。为了看看SpecFlow和ChromeDriver之间是否存在某种不兼容性(我知道这很长),我将我的搜索测试的WebDriver更改为Firefox,并将WebDriver作为Chrome进行登录测试。无论我跑了什么样的测试,我总是看到2个浏览器打开;一个Chrome和一个Firefox。

当我将SearchTestSteps.cs文件中的所有步骤移动到LoginTestSteps.cs文件中时,问题就消失了。

所以,是的,这解决了眼前的问题,但将所有步骤放在一个文件中并不是最佳的。这很快就会变得笨拙。

由于每组步骤都需要有自己的WebDriver,所以我很茫然。

这可能与文件夹结构和存储内容有关吗?这是我的样子。

Root
 |-Page Object Files
      |- Page Components
      |- Pages
      |- Test Tools  
 |- Step Definitions
      |- <*Steps.cs>  
 |- TESTS
      |- BDD Tests
          |-<*.feature>
      |- *standard selenium test files*

代码:

Login.feature
Feature: Login
    In order to be able to use Laserfiche
    As a legitimate user
    I want to be able to log into the repository

@SmokeTest
Scenario: Login with correct credentials
    Given I am on the Login page 
    And I have a good username/password combination
    And I select a repository
    When I fill out the form and submit
    Then I am taken to the repo page

---------------
LoginSteps.cs (I also have a SearchTestSteps.cs that looks very similar)
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using Selenium_C_Sharp_POC.Page_Object_Files.Pages;
using Selenium_C_Sharp_POC.Page_Object_Files.Test_Tools;
using TechTalk.SpecFlow;

namespace Selenium_C_Sharp_POC.StepDefinitions
{
    [Binding]
    public class LoginSteps
    {
        private static readonly IWebDriver Driver = new ChromeDriver();

        private static LoginPage _loginPage;
        private static string _username;
        private static string _password;
        private static string _repo;

        [AfterTestRun]
        public static void ShutDown()
        {
            Driver?.Close();
        }

        [Given(@"I am on the Login page")]
        public void GivenIAmOnTheLoginPage()
        {
            _loginPage = new LoginPage(Driver);
        }

        [Given(@"I have a good username/password combination")]
        public void GivenIHaveAGoodUsernamePasswordCombination()
        {
            _username = Nomenclature.WebClientPersonalUsername;
            _password = Nomenclature.WebClientPersonalPassword;
        }
        [Given(@"I select a repository")]
        public void GivenISelectARepository()
        {
            _repo = Nomenclature.RepoUnderTest;
        }

        [When(@"I fill out the form and submit")]
        public void WhenIFillOutTheFormAndSubmit()
        {
            _loginPage.Login(
                username: _username, 
                password: _password, 
                repo: _repo);
        }

        [Then(@"I am taken to the repo page")]
        public void ThenIAmTakenToTheRepoPage()
        {
            Assert.AreEqual(
                expected: _repo,
                actual: Driver.Title);

            HelperMethods.Logout(Driver);
        }
    }
}
c# selenium selenium-webdriver bdd specflow
3个回答
1
投票

我想出了如何使用Binding Scopes来解决这个问题。

在每个步骤文件中,我可以执行以下操作:

  [BeforeFeature(), Scope(Feature = "SearchTests")]
  public static void Startup()
  {
      _driver = new ChromeDriver();
  }

  [AfterFeature()]
  public static void ShutDown()
  {
      _driver?.Close();
  }

这样做只会打开和关闭我想要的测试文件的驱动程序。如果我需要更细粒度,我还可以在每次测试之前选择范围到标记。


0
投票

您可能已在每个.cs文件中创建了驱动程序实例。例如:在LoginSteps.cs中,您将在下面的loc中创建chrome驱动程序。

private static readonly IWebDriver Driver = new ChromeDriver();

您应该在xStep.cs文件之外创建驱动程序,并将其传递给基于框架的类/方法。


0
投票

最终,这是因为在步骤定义类中将Web驱动程序创建为静态字段。你需要集中这个逻辑。

您希望在该功能之前创建Web驱动程序,将其注册到SpecFlow依赖项注入容器,然后将该IWebDriver对象传递给您的步骤定义。

我发现实现一个“懒惰”的Web驱动程序是一个好主意,因此当您的C#代码实际需要与它进行交互时,浏览器窗口才会生成。这个LazyWebDriver类实现了IWebDriver接口,是真正的Web驱动程序的包装器。

LazyWebDriver.cs

public sealed class LazyWebDriver : IWebDriver
{
    private readonly Lazy<IWebDriver> driver;

    public string Title => driver.Value.Title;

    // Other properties defined in IWebDriver just pass through to driver.Value.Property

    public LazyWebDriver(Func<IWebDriver> driverFactory)
    {
        driver = new Lazy<IWebDriver>(driverFactory);
    }

    public IWebElement FindElement(By by)
    {
        return driver.Value.FindElement(by);
    }

    public void Close()
    {
        driver.Value.Close();
    }

    // other methods defined in IWebDriver just pass through to driver.Value.Method(...)
}

然后使用SpecFlow挂钩(这对于SpecFlow中的“事件”只是花哨的谈话),您可以创建真正的Web驱动程序和惰性Web驱动程序并将其注册到SpecFlow框架:

SeleniumHooks.cs

[Binding]
public sealed class SeleniumHooks
{
    private readonly IObjectContainer objectContainer;

    public SeleniumHooks(IObjectContainer objectContainer)
    {
        this.objectContainer = objectContainer;
    }

    [BeforeFeature]
    public void RegisterWebDriver()
    {
        objectContainer.RegisterInstanceAs<IWebDriver>(new LazyWebDriver(CreateWebDriver));
    }

    private IWebDriver CreateWebDriver()
    {
        return new ChromeDriver();
    }

    [AfterFeature]
    public void DestroyWebDriver()
    {
        objectContainer.Resolve<IWebDriver>()?.Close();
    }
}

最后,对LoginSteps.cs文件进行一些修改:

[Binding]
public class LoginSteps
{
    private readonly IWebDriver Driver;
    private LoginPage _loginPage;

    private static string _username;
    private static string _password;
    private static string _repo;

    public LoginSteps(IWebDriver driver)
    {
        Driver = driver;
    }

    [Given(@"I am on the Login page")]
    public void GivenIAmOnTheLoginPage()
    {
        _loginPage = new LoginPage(Driver);
    }

    [Given(@"I have a good username/password combination")]
    public void GivenIHaveAGoodUsernamePasswordCombination()
    {
        _username = Nomenclature.WebClientPersonalUsername;
        _password = Nomenclature.WebClientPersonalPassword;
    }
    [Given(@"I select a repository")]
    public void GivenISelectARepository()
    {
        _repo = Nomenclature.RepoUnderTest;
    }

    [When(@"I fill out the form and submit")]
    public void WhenIFillOutTheFormAndSubmit()
    {
        _loginPage.Login(
            username: _username, 
            password: _password, 
            repo: _repo);
    }

    [Then(@"I am taken to the repo page")]
    public void ThenIAmTakenToTheRepoPage()
    {
        Assert.AreEqual(
            expected: _repo,
            actual: Driver.Title);

        HelperMethods.Logout(Driver);
    }
}

请注意,IWebDriver对象作为构造函数参数传递给LoginSteps。 SpecFlow附带了一个依赖注入框架,该框架非常智能,可以将您在SeleniumHooks中注册的LazyWebDriver作为LoginSteps构造函数的IWebDriver参数传递。

一定要使_loginPage字段成为实例字段而不是静态字段。

© www.soinside.com 2019 - 2024. All rights reserved.