This article describes how to use the Ranorex Studio IDE and Ranorex API for test automation in behavior-driven development (BDD).
To parse feature files from natural language into executable code, SpecFlow is used as the BDD interpreter, and NUnit is the test provider that manages and executes the tests.
Prepare Ranorex Studio for BDD
The SpecFlow add-in provides file templates for feature, step definition, and event definition files. It also translates features written in Gherkin syntax into C# code.
Start by installing the SpecFlow add-in to Ranorex Studio:
- Download or clone the bdd-specflow repository .
-
Copy the Ranorex SpecFlow AddIn folder to:
<Ranorex installation folder>\Bin\Addins\Misc - Copy the NUnit.ConsoleRunner.3.15.2 folder to the location from which you want to run tests. For example, copy it to the debug folder of the solution where the
.rxtstfile resides.
Make your Ranorex solution BDD-ready
- In Ranorex Studio, open the Package Management Console (View > Tools > Package Management Console).
-
Run the following command:
Install-Package SpecFlow.NUnit -Version 3.9.74 -Source "https://www.nuget.org/api/v2/" - In the project properties, set the Output type to Class Library.
- Copy all Ranorex runtime DLLs to the output folder:
- In Solution Explorer, locate the references.
- Set Local copy to True for each reference.
-
To ensure that your BDD solution integrates correctly with SpecFlow and NUnit, update the AssemblyInfo.cs and app.config files.
-
Double-click AssemblyInfo.cs in the Projects view and add: [assembly: NUnit.Framework.Apartment(System.Threading.ApartmentState.STA)]
#region Using directives using System; using System.Reflection; using System.Runtime.InteropServices; #endregion // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle("BDOCalc")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("BDOCalc")] [assembly: AssemblyCopyright("Copyright 2019")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: NUnit.Framework.Apartment(System.Threading.ApartmentState.STA)] // This sets the default COM visibility of types in the assembly to invisible. // If you need to expose a type to COM, use [ComVisible(true)] on that type. [assembly: ComVisible(false)] // The assembly version has the following format: // // Major.Minor.Build.Revision // // You can specify all the values, or you can use the default revision and // build numbers by using the '*' as shown below: [assembly: AssemblyVersion("1.0.*")]
In app.config, add the following section:
<assemblyBinding> <probing privatePath="Runtime" /> </assemblyBinding>Example - C#
<?xml version="1.0" encoding="utf-8"?> <configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> <supportedRuntime version="v2.0.50727" /> </startup> <runtime> <enforceFIPSPolicy enabled="false" /> <assemblyBinding> <probing privatePath="Runtime" /> </assemblyBinding> </runtime> </configuration> -
Get started with BDD
Once Ranorex Studio is BDD-ready, you can define features in SpecFlow or import existing feature files.
Step 1: Define a feature from the business side
Option A: Create it in Ranorex Studio. In SpecFlow, right-click the project and select Add > New Item > Category: SpecFlow > SpecFlow Feature.
Option B: Import an existing feature file. Right-click the project, select Add > Existing Item..., and then select the feature file.
Verify that the feature file has been added successfully.
For both options, Ranorex Studio creates a .feature.cs file containing the automatically generated code that executes test scenarios using the configured test provider.
Build the solution to create and populate that C# file.
Step 2: Create step definitions
- Right-click the project and select Add > New Item > Category: SpecFlow > SpecFlow Step Definition.
- Add methods for each Given, When, and Then action described in the feature file. Follow the samples provided by the template from which the new step definition file was generated.
[Binding]
public class StepDefinition1
{
private readonly ScenarioContext _scenarioContext;
public StepDefinition1(ScenarioContext scenarioContext)
{
_scenarioContext = scenarioContext;
}
[Given("I have entered (.*) into the calculator")]
public void GivenIHaveEnteredSomethingIntoTheCalculator(int number)
{
// TODO: implement arrange logic
// For storing and retrieving scenario-specific data,
// fields of the private member _scenarioContext can be used.
// To use multiline text or the table argument of the scenario,
// additional string/Table parameters can be defined
// on the step definition method.
_scenarioContext.Pending();
}
}- If you are transitioning from traditional scripting to BDD, Ranorex Studio includes additional features that can help:
- Reuse code from existing recorded steps by viewing and copying it from the Actions table.
- Your methods can be set up to take advantage of the repository and its ability to propagate object updates:
public static {Your project repository name} repo = {Your project repository name}.Instance;Example - C#
[Binding]
public class Sqrt
{
[Given("I have entered (.*) into the calculator")]
public void GivenIHaveEnteredSomethingIntoTheCalculator(int number)
{
ClickNumButton.repo.numButton = number.ToString();
ClickNumButton.Start();
}
[When("I press the square root button")]
public void WhenIPressSqrt()
{
PressSqrt.Start();
}
[Then("the result should be (.*) on the screen")]
public void ThenTheResultShouldBe(int result)
{
ValidateResult.Instance.result = result.ToString();
ValidateResult.Start();
}
}You can create additional steps to be executed before and after a scenario, such as setup and teardown tasks, including starting and closing the system under test.
Example - C#
[BeforeScenario]
public void BeforeScenario()
{
OpenBrowser.Start();
}
[AfterScenario]
public void AfterScenario()
{
CloseBrowser.Start();
}Save your changes and build the solution to apply them.
Step 3: Execute a BDD test using NUnit
To run tests, you need:
- The
.featurefile - The generated
.feature.csfile - The step definitions file
Since this workflow uses NUnit as the test provider, run NUnit Console Runner to execute the tests:
{path to the Console Runner}\NUnit.ConsoleRunner.3.15.2\nunit3-console.exe "{path to the test solution debug folder}\<solution>.dll"By default, NUnit creates an XML report. To transform the results into JUnit format, use the following command:
--result=junit-results.xml;transform=nunit3-junit.xsltThe results folder, which is the directory from which you run the command, will also contain a report in the standard Ranorex format.
Troubleshooting
If your assembly is not found, build your project as a library.
- Open the project properties and set the output type to Class Library.
- Copy all Ranorex runtime DLLs to the output folder by setting Local copy to True.