Selenium和SpecFlow之间的共享方法
本文关键字:共享 方法 之间 SpecFlow Selenium | 更新日期: 2023-09-27 18:35:03
我有一个测试项目,其中包含我要测试的所有Selenium场景,我想向此解决方案添加一个SpecFlow项目,该项目显然将使用一些WebDriver方法。我不想复制我的代码,但 SpecFlow 不能很好地与 Selenium 配合使用(例如,Selenium 正在使用 [TestInitialize] 属性,这在 SpecFlow 中是不允许的(。将两者结合起来的最佳方法是什么?
我想执行与"SomeTestMethod"中相同的步骤,但使用SpecFlow。
这是该项目的一个示例:
public class SeleniumBaseTest : BaseTest
{
[AssemblyInitialize]
public static void Initialize(TestContext testContext)
{
}
Public SomeMethod()
{
}
}
[TestClass]
public class SeleniumFeature : SeleniumBaseTest
{
[TestInitialize]
public void SeleInitialize()
{
}
[TestMethod]
public void SomeTestMethod()
{
}
}
由于 SpecFlow 步骤实际上只是继承自 System.Object
的类上的公共方法,因此只需实例化步骤定义类并从 Selenium 测试中调用公共方法即可。
数据步骤.cs
[Binding]
public class DataSteps
{
[Given("Something exists in the database")]
public void GivenSomethingExistsInTheDatabase()
{
// ...
}
}
在您的硒测试课上:
[TestClass]
public class SeleniumFeature : SeleniumBaseTest
{
private DataSteps dataSteps;
[TestInitialize]
public void SeleInitialize()
{
dataSteps = new DataSteps();
}
[TestMethod]
public void SomeTestMethod()
{
dataSteps.GivenSomethingExistsInTheDatabase();
}
}
唯一真正的痛苦是当您需要使用TechTalk.SpecFlow.Table
对象作为步骤定义的参数时。要弄清楚该语法,请查看设计器生成的源代码,以获取使用 Gherkin 表语法的.feature
文件之一,例如
Scenario: Testing something important
Given a Foo exists with the following attributes:
| Field | Name |
| Name | Foo |
| Fruit | Apple |
如果有帮助,您可以将步骤定义保留在其自己的程序集中。
您可以使用属性(又名钩子(,例如:
[测试运行前][测试后运行]
[之前功能][后记]
[之前方案] 或 [之前][之后场景] 或 [之后]
[之前场景块][后场景块]
[上一步][后步]
有关钩子的详细信息,请转到此处