具有多个步骤文件会打开多个浏览器 [英] Having Multiple Step Files Opens Multiple Browsers
问题描述
如果我有多个步骤"文件,则在执行测试时,无论我运行哪个测试,似乎正在为每个创建WebDriver.
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.
每次运行测试时,我都会看到一个看似随机的Chrome浏览器打开.为了查看SpecFlow和ChromeDriver之间是否存在某种不兼容(我知道这是一个很长的路要走),我将用于搜索测试的WebDriver更改为Firefox,而将要用于登录测试的WebDriver更改为Chrome.不管我运行了什么测试,我总是看到两个浏览器都打开了.一台Chrome浏览器和一台Firefox.
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.
当我将所有步骤从SearchTestSteps.cs文件移至LoginTestSteps.cs文件时,问题消失了.
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.
由于每组步骤都需要具有自己的WebDriver,所以我很茫然.
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*
代码:
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);
}
}
}
推荐答案
我想出了如何使用绑定范围"解决此问题的方法.
I figured out how to fix this issue using Binding Scopes.
在每个步骤"文件中,我可以执行以下操作:
In each of the Steps files, I can do the following:
[BeforeFeature(), Scope(Feature = "SearchTests")]
public static void Startup()
{
_driver = new ChromeDriver();
}
[AfterFeature()]
public static void ShutDown()
{
_driver?.Close();
}
执行此操作将仅为我想要的测试文件打开和关闭驱动程序. 如果需要更详细的说明,我还可以选择在每次测试之前将范围限定在标签上.
Doing this opens and closes the Driver for only the Test file that I want it to. I can also choose to scope to the tag before each test if I need to get more granular.
这篇关于具有多个步骤文件会打开多个浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!