This is the final part of our 3 part blog on integrating Ranorex with SpecFlow in Visual Studio and creating a Page Object style framework

In this last part we look at bringing in BDD using SpecFlow.

BDD with SpecFlow

OK, now we have our own Ranorex POM framework sorted, lets extend this into BDD using SpecFlow.

Folder Structure

First let’s create a structure to store our classes & features, add the folders features and steps to our project.

Create a .feature file

Right-click on the features folder and select add new item -> SpecFlow Feature File, call it Navigation.feature and add a scenario:

Now right-click in the feature file and choose Generate Step Definitions…

Select the default class name, and create the class under the steps folder you created earlier.

So we should have:

Write the Code Bindings

Open the NavigationSteps.cs file and write our code:

Note – we are inheriting our BaseTest class just like we did in the simple test

Our BaseTest class had our StartBrowser which we use here, and then we instantiate our Page Object Classes to find Web Elements and perform our actions against them

Execute Features

Build the project and in the Test Explorer (Test->Windows->Test Explorer) you can see your Scenarios and run them!

Running from the command line:

We can now execute our feature files from the command line using the nunit console runner.

Navigate to your project folder and in the \packages\NUnit.ConsoleRunner.3.8.0\tools folder you will find the nunit3-console.exe application, use this to execute the project by passing in the project output dll. Do this in a command window. For example:

nunit3-console.exe “c:\<my project>\com.edgewords.ranorex.pom\bin\x86\Debug\<my project>.dll”

This will create a results file in nunit format called TestResult.xml in the same folder as the nunit3-console.exe

You can then use a results viewer like PicklesUI to convert the results to html, Word, Excel etc.

SpecFlow Hooks

In our BaseTest class, in the StartBrowser Method, we have a line of code that checks & resolves any Ranorex reference libraries – BUT we don’t want this to run for every test, so let’s use SpecFlow’s hook [BeforeTestRun] to ensure this only runs once. To do this, select our helpers folder, right-click and add New item… SepcFlow Hooks (event bindings) file and add the following code:

You can now remove the Ranorex.Core.Resolver statement from the StartBrowser Method.

The [BeforeTestRun] Method will be invoked just once before any features get executed when you click the Run All.. in the Test Explorer.



If you get an error that it cannot find a reference (I got this with the Ranorex.Plugin.Sap DLL)

Then in your solution explorer, under references, find the reference that the error was about, and ensure that Copy Local is set to True. Do this for all the Ranorex references). If copy to local is set to false and greyed out (you cannot change it), then set its Embed Interop Types to False, then change the Copy Local to True.

If you get any errors about mismatches with the target platform and the Ranorex dll references, you may need to set the VS project platform to x86.

In Visual Studio menu choose Build->Configuration Manager then from the Active solution platform drop-down choose x86 and then rebuild your project and try again.



Download Link for the project files shown in this blog