Recently a customer asked whether it is possible to combine the power of Spec Explorer and VS2010 UI test features to apply MBT in UI automation. The short answer is yes. And in my opinion, the most challenge part is not how to model system under test, but to build an adapter layer with UI automation framework. Today, I’d like to share a very simple example that just shows the possibility of use Spec Explorer to apply MBT with VS2010 Coded UI Test. You can download the example code at here.
First, let’s create a test project and add a new coded UI test. You can find detailed steps at http://msdn.microsoft.com/en-us/library/dd286681.aspx.
Launch calculator and start to record actions. Please make sure all number buttons, button “C”, “+”, and “=” are pressed during recording. After you record those actions, you can press “Generate Code” button to generate test method.
Then let’s add assertion by drag the crosshair over the calculator edit box and drop it when the edit box is highlighted in blue. Select the “DisplayText” property and add assertion by pressing “Add Assertion” button. Press “Generate Code” button to generate assertion method.
You should be able to find a test class identified by [CodedUITest] attribute and UIMap.uitest associated with UIMap.cs and UIMap.Designer.cs. The generated method is simple and straightforward, it just creates and presses buttons as we recorded. That should be enough clue to write your own adapter method now.
Let’s add own adapter method to UIMap.cs. I have a constructor to initialize a map from number to its button, and two adapter method “Add” and “ReadAndReset” just like what we have in project template of Spec Explorer.
Then let’s model the behavior “Add” and “ReadAndReset” of system under test.
And add a Cord config file to define configuration and machines. Note that value of switch “TestClassAttribute” should be specified to CodedUITestAttribute.
Finally let’s generate test case from machine “TestSuite”, launch your calculator and run the generated test cases.