2014-12-22 101 views
2

我有一个测试项目,其中包含我想测试的所有Selenium方案,并且我想向此解决方案添加一个SpecFlow项目,显然将使用一些WebDriver方法。 我不想复制我的代码,但是SpecFlow与Selenium无法正常工作(例如Selenium正在使用SpecFlow中不允许的[TestInitialize]属性)。 将两者结合的最佳方式是什么?Selenium和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() 
    {    
    } 
} 

回答

1

由于SpecFlow步骤是在从System.Object继承的类真的只是公共方法,只是实例化步骤定义类并调用从您的Selenium测试的公共方法。

DataSteps.cs

[Binding] 
public class DataSteps 
{ 
    [Given("Something exists in the database")] 
    public void GivenSomethingExistsInTheDatabase() 
    { 
     // ... 
    } 
} 

在你的Selenium测试类:

[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 | 

如果有帮助,您可以将步骤定义保留在它们自己的程序集中。

+0

这是一个可能的解决方案,但我的硒的项目中使用一些复杂的模式,以便它不帮助我。 我需要维护当前的设计并添加SpecFlow。 但这是一个很好的解决方案,谢谢。 – Udiy

+0

我明白了。有两个不同的项目可能会更好。在SpecFlow中创建场景时,请删除Selenium项目中的冗余覆盖。 –

0

您可以使用属性又名象钩:

[BeforeTestRun] [AfterTestRun]

[BeforeFeature] [AfterFeature]

[BeforeScenario]或[前] [AfterScenario]或[后]

[BeforeScenarioBlock] [AfterScenarioBlock]

[BeforeStep] [AfterStep的信息]

有关挂钩详细信息,请here

+0

@udiy,有没有更新? –

+0

@udiy,请让我们知道您的更新。 –