c# - Having Multiple Step Files Opens Multiple Browsers
问题描述
The Problem:
If I have more than one Steps file, when I execute tests it seems that the WebDriver creation for each is being happening regardless of which test(s) I run.
I was seeing a seemingly random Chrome Browser open up whenever I ran my tests. In an attempt to see if there was some sort of incompatibility between SpecFlow and ChromeDriver (a long shot, I know), I changed the WebDriver for my search tests to Firefox and left the WebDriver for my login tests as Chrome. No matter what test(s) I ran, I always saw 2 browsers open; one Chrome and one Firefox.
When I moved all of the steps from my SearchTestSteps.cs file into the LoginTestSteps.cs file, the problem disappeared.
So, yeah, this solves the immediate issue, but it is sub-optimal to have all of my steps in a single file. That can quickly become unwieldy.
Since each set of steps needs to have its own WebDriver, I'm at a loss.
Might this have something to do with folder structure and where things are stored? Here is what mine looks like.
Root
|-Page Object Files
|- Page Components
|- Pages
|- Test Tools
|- Step Definitions
|- <*Steps.cs>
|- TESTS
|- BDD Tests
|-<*.feature>
|- *standard selenium test files*
The Code:
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);
}
}
}
解决方案
我想出了如何使用绑定范围来解决这个问题。
在每个 Steps 文件中,我可以执行以下操作:
[BeforeFeature(), Scope(Feature = "SearchTests")]
public static void Startup()
{
_driver = new ChromeDriver();
}
[AfterFeature()]
public static void ShutDown()
{
_driver?.Close();
}
这样做只会打开和关闭我想要的测试文件的驱动程序。如果我需要更细化,我还可以选择在每次测试之前将范围限定为标签。
推荐阅读
- php - 对 ebay-sdk-php 中的 shopping_get_item.php 和 ShoppingBaseService.php 进行哪些更改以使其与 X-EBAY-API-IAF-TOKEN 一起使用
- c# - 如何在 C# 代码中使用“go”发送 SQL 请求
- angular - 使用 Angular Material 拖放复制和附加
- weblogic - 使用 WLST 列出和使用应用程序角色 - listAppRoles()
- django - 如何在 Django 中将 json 转换为表单?
- html - 在 div 中添加边框
- python - 为什么 vs code ipynb 中的代码是灰色的,我该如何更改?
- javascript - 如何用集合字段序列化表单数据并在服务器端反序列化?
- android - VoskAPI:ExpectToken():io-funcs.cc:200) 无法读取令牌 [从文件位置 0 开始],预期
- kotlin - 多部分表单数据的 Kotlin (Ktor) 请求类