问题:
如果我有多个 Steps 文件,当我执行测试时,似乎无论我运行哪个测试,都会为每个 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);
}
}
}