PowerShell and XML: SpecFlow bindings

In work this week, I needed to programmatically edit an app.config to include SpecFlow binding references. I thought that I could just turn to my other posts on XML editing, and that would cover it. Wrong. Those posts did not include how to add an element, then sibling elements within that element, with those sibling elements taking an attribute. As pictures, I wanted to get from this

SpecFlowXml01

to this:

SpecFlowXml02

So going back to our starting picture..

SpecFlowXml01

, we can select the <specFlow> node in PowerShell, and find it has nothing (do note that XML object notation is case-sensitive):

$xml = [xml] (Get-Content “C:\temp\app.config”)
$xml.SelectNodes(‘//configuration/specflow’)

SpecFlowXml03

If you then execute this, then this is what you see in the PowerShell ISE…

SpecFlowXml04 SpecFlowXml05

And this is the output I wanted, which PowerShell has done. The PowerShell for this is not as clean as the dot notation you can use when elements, attributes, values already exist, but it ain’t too bad:

SpecFlowXml06

There is however a Yeah-But in this: if you run that PowerShell a second time, you get this:

SpecFlowXml07

Rather than make things too noisy, in my use-case I know that the StepAssemblies and its children are either wholly there and well-formed, or fully absent. So testing for the existence of the stepAssemblies node is enough to decide whether to update the XML (code):

SpecFlowXml09

This will always give us back this, assuming our starting point above:

SpecFlowXml06

Advertisements

Visual Studio 2015: SpecFlow

Dunno why, but installing SpecFlow in Package Manager console works… but then I can’t find the items.

So going here, gives you this:

SFlowDownload01

Same how-tos here and page dedicated to 2015 here.

Install it…

SFlowDownload02

Then when you stop and start VS, you get this:

SFlowDownload03

I had already created a MSTest project inside a console application solution. Before I didn’t have the SpecFlow items, now I do, when I go to add an item:

SFlowDownload04

The first time of asking, we get this:

SFlowDownload05

SFlowDownload06

So easy enough to resolve the SpecFlow reference, now just the NUnit.

This is the app.config for the other project (sic):

SFlowDownload07

And I’ve seen comments saying that you need this line (first for context):

<specFlow>
      <unitTestProvider name="MsTest.2010" />

I dragged the updated app.config into the Unit Test Project, thus…

SFlowDownload08

SFlowDownload09

Also note that the generated file now contains MSTest references, not NUnit references, since we have the new entry in the app.config (the squiggles are because it can’t find a schema for SpecFlow:

SFlowDownload10

Perhaps now I can get to the error I was trying to demonstrate. Oh but first I have to give it some bindings:

SFlowDownload11

So now we have that (PasteBin here)…

SFlowDownload12

Then the test passes…

SFlowDownload13

And then I was playing with the tags trying to get it to break with this error re ScenarioContext and missing key… but I can’t now (get it to break).