BDD UI Automation with Specflow and Coded UI Tests

As a tester in a Scrum development environment when I heard about Specflow and Behaviour Driven Development I started to wonder if we could use the Specflow Gerkin format to specify our acceptance criteria for a User Story and then be able to wire this up to a coded UI Test to prove that we have actually added this feature and to ensure that it will not break in the future. After a bit of research and a good understanding of Coded UI test and Specflow I found it actually wasn’t that hard to do. So why would I do this?

  • Specflow tests are normally aimed around the ViewModel layer; this is fine if you’re using a good architecture but what about older legacy products.
  • Normal Specflow tests do not test exactly what the user will experience.
  • Unfortunately Coded UI Tests have been known to be brittle and this is defiantly something you need to be aware of. With older technology Coded UI Tests uses MSAA (Microsoft Active Accessibility) to find property values of a control. From my experience this can be a bit flaky however the .net version should be a lot better. If worst comes to worst you can write a technology plugin for Coded UI Tests to help it out. A strict project structure can help to limit how much work needs to be done when the UI Changes. For example record actions in small chunks, this makes them flexible and means that you won’t have to rerecord much it the UI Changes.

So there are both pros and cons about using UI Tests for Acceptance Tests but the one good thing is what you test at the UI Level will be what the user sees.

How to create an Automated UI Specflow Feature

This sample assumes you have a good understanding of Specflow and Coded UI Tests

  1. Create a Specflow Feature file
  2. Record your Coded UI test methods
  3. Wire up the Specflow step definitions with a Coded UI test methods

Source code for this series can be found here

SpecFlow 1.6

SpecFlow 1.7

  1. #1 by Urs Müller on June 16, 2011 - 2:44 pm

    Hi Ryan

    Thanks for this interesting post. The last couple of months, I have worked with MS Coded UI Test. We recently heard about SpecFlow. But I simply have some “silly” problem. According to I should place the custom Attribute assembly to some path, which is? I don’t want to do that with GAC. So what would this path look like?

    — Urs

    • #2 by RBurnham on June 16, 2011 - 10:30 pm

      So the Specflow article walks you through creating a custom Specflow generator for coded ui test. I originally got Specflow and coded ui tests working without doing this by call the Playback.Initialize() in the BeforeScenario. But you wouldn’t want to initialise the coded ui test playback engine every time you run a scenario, so it makes more sense to call it in the Before Test Run. Doing this though cased a few issues which the generator works around. Using the generator i didn’t have to call the Playback.Initialize() method at all.

      So back to your question the path you place the SpecFlow generator in is inside your install folder for SpecFlow. This is where you find the SpecFlow.exe normally %ProgramFiles(x86)%\TechTalk\SpecFlow

      • #3 by Urs Müller on June 17, 2011 - 6:47 am

        Hi Ryan

        That’s what I had already tried. Nevertheless, that helped me to find the solution: The assembly SpecflowCodedUIGenerator needs to be built for .NET framework version 3.5, I had all solutions on version 4.0. Now, everything works fine. Thanks!


  2. #4 by RBurnham on June 20, 2011 - 6:34 am

    Ah this makes sense, i updated the SpecFlow wiki article so it reflects your findings

  3. #5 by Lars on September 20, 2011 - 4:29 pm

    Hi Ryan, thank you for these useful articles. The recipe on seems to have broken because of recent refactorings of SpecFlow, particularly rooted in

    I tried some quick fixes, but with limited knowledge of .NET in general and SpecFlow source in particular, I’m afraid I’ve stranded. Would you have a look?


    • #6 by RBurnham on September 21, 2011 - 3:11 pm

      No problem, i’ll try to take a look at them soon and get back to you

    • #7 by RBurnham on October 5, 2011 - 5:02 am

      Hi Lars,
      I’ve finally figured out the change with some help from Gáspár Nagy. I’ve updated the Specflow Wiki article to include the 1.7 steps and also uploaded a 1.7 sample to go with this series

  4. #8 by Michael on March 13, 2012 - 11:05 am

    Hello! Reference to the source code no longer works. Could you update it?

  5. #9 by RBurnham on March 13, 2012 - 12:10 pm

    Hi Michael,
    Thanks for the notification. I’ve updated the links

  6. #10 by Hugh Lacey on October 9, 2012 - 11:24 am

    Has anybody got this working with SpecFlow 1.9?
    I started with the examples for 1.7 and then changed it to reflect the 1.9 example code and used NUGet to update to SpecFlow 1.9. But now the custom tool for feature files fails – it can’t find the custom CodedUIGenerator.

  7. #12 by Paul Shippy on December 4, 2012 - 10:46 pm

    I am having trouble getting this to work in 1.9 with Visual Studio 2012. I am getting the following error in the .feature.cs file:
    #error Generation error: Could not load file or assembly ‘SpecFlowCodedUI’ or one of its dependencies. The system cannot find the file specified.

    I have tried copying SpecFlowCodedUI.dll (my assembly with the 1.9 code from the wiki) to both of the following folders to no avail:
    C:\Users\{my username}\AppData\Local\Microsoft\VisualStudio\11.0\Extensions\z1xsju3a.4sm
    C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE

    I have also tried referencing it from my test project, but that didn’t help much because it won’t build without the generated .feature.cs file.
    I have tried setting SpecFlowCodedUI.dll to target.NET 3.5, 4.0, and 4.5 (my project is on 4.5).


    • #13 by Paul Shippy on December 4, 2012 - 11:05 pm

      I was able to get it to work by adding the following line within the tag in app.config:

      I assume the random letters at the end of this path will be different on others’ machines.

      • #14 by Paul Shippy on December 4, 2012 - 11:06 pm

        <generator path=”C:\Users\pshippy\AppData\Local\Microsoft\VisualStudio\11.0\Extensions\gskggsmv.c1h” />

      • #15 by RBurnham on December 5, 2012 - 8:25 am

        Thanks for letting us know. not sure what that file could be, how did you figure out it was looking for that file?

      • #16 by Paul Shippy on December 11, 2012 - 8:22 pm

        I searched my hard drive for the assemblies and found them there. And I saw the generator tag on the documentation. Here is the line again, hopefully escaped this time:
        <generator path=”C:\Users\[username here]\AppData\Local\Microsoft\VisualStudio\11.0\Extensions\gskggsmv.c1h” />

    • #17 by Paul Shippy on December 18, 2012 - 12:43 am

      Found it –
      1. Use NuGet to add SpecFlow to your project.
      2. Drop the custom assembly into [Solution Folder]\packages\Specflow.1.9.0\tools
      That should do it.

  8. #18 by Vincent on March 22, 2013 - 3:02 pm

    Nice article! It works great for SpecFlow 1.7. However I encounter some difficulties when using 1.9. I suspect that it is better then to create a custom generator as a plugin:
    But I can’t figure out how to build and use a custom generator plugin. Do you perhaps already have used this new feature?

    • #19 by RBurnham on April 14, 2013 - 9:13 am

      Sorry i haven’t looked into Speflow 1.9 much or the Generator Plugins

  1. Create a Specflow Feature file « Ryan Burnham's Blog
  2. Record your Coded UI test methods « Ryan Burnham's Blog
  3. Wire up the Specflow step definitions with a Coded UI test methods « Ryan Burnham's Blog
  4. BDD Silverlight Testing « Hornet Dear Bernard

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: