Reputation: 65
I have a test project that holds all the Selenium scenarios that I want to test and I want to add a SpecFlow project to this solution that obviously will use some of the WebDriver methods. I don't want to duplicate my code but the SpecFlow is not working well with the Selenium (for example Selenium is using the [TestInitialize] attribute which is not allowed in SpecFlow). What is the best way to combine the two?
I want to do the same steps as in "SomeTestMethod" but with SpecFlow.
This is an example of the project:
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()
{
}
}
Upvotes: 2
Views: 513
Reputation: 41
You can use attributes aka hooks like:
[BeforeTestRun] [AfterTestRun]
[BeforeFeature] [AfterFeature]
[BeforeScenario] or [Before] [AfterScenario] or [After]
[BeforeScenarioBlock] [AfterScenarioBlock]
[BeforeStep] [AfterStep]
For detailed information about hooks, go here
Upvotes: 0
Reputation: 18783
Since SpecFlow steps are really just public methods on a class that inherits from System.Object
, just instantiate the step definition class and call the public methods from your Selenium test.
DataSteps.cs
[Binding]
public class DataSteps
{
[Given("Something exists in the database")]
public void GivenSomethingExistsInTheDatabase()
{
// ...
}
}
In your Selenium test class:
[TestClass]
public class SeleniumFeature : SeleniumBaseTest
{
private DataSteps dataSteps;
[TestInitialize]
public void SeleInitialize()
{
dataSteps = new DataSteps();
}
[TestMethod]
public void SomeTestMethod()
{
dataSteps.GivenSomethingExistsInTheDatabase();
}
}
The only real pain is when you need to use a TechTalk.SpecFlow.Table
object as a parameter to a step definition. To figure out that syntax look at the Designer-generated source for one of the .feature
files that uses the Gherkin table syntax, e.g.
Scenario: Testing something important
Given a Foo exists with the following attributes:
| Field | Name |
| Name | Foo |
| Fruit | Apple |
If it helps, you can keep the step definitions in their own assembly.
Upvotes: 1