I ran into a problem with Coded UI Testing, and Word Add-Ins. Specifically, I couldn’t assert the code of a field in the document.. Hold on. Let me give you a more dramatic introduction:
-(losers that want to get straight to the point can skip to paragraph 7)
I was tasked with using the Coded UI Testing tools included in Visual Studio 2010 to test an Add-In in Microsoft Word 2010. It seemed easy enough, I mean I’ve got some buttons to press, some text to look at.. So? Big deal! HUGE DEAL!!! You don’t just hit the record button and start doing stuff in a Word Add-in! But how was I supposed to know that? Here I am, just starting my first week at my first internship—the only job I’ve ever had besides cashiering at a water park (don’t remind me). I’ve barely ever used Visual Studio, most of my programming experience is Java, Python and C++, the only UI work I’ve ever done was Visual Basic 6. I certainly haven’t worked with Word Add-ins (or even used them for that matter). I had no idea where to start!
Well, I’ll probably write something about getting started with Coded UI Testing, and stuff like that, I’d love to right now but my boss is on vacation today.. You’re right, I don’t really see how that excuse ties in either, but when they told me to blog, they said “extra points if you make fun of the superiors!”
So first of all, if you want to test anything in Word with Coded UI Tests, you’re going to have to make an extension. No, it’s not that hard; I’ll walk you through this line by line. You are most likely in the software development industry; in fact you are probably an intern just like me, contrary to the name of the blog. Yesterday your boss probably saw you starting a file from scratch, hopefully he (or she) didn’t throw you in a dark room for 3 days (mine almost did, he said “that’s what interns are for”) but hopefully s/he said something like “don’t re-invent the wheel; someone’s already done that for you!” And you thought “But in CS 160 they said…” Well the truth is you can find the extension to find Coded UI support for Word here
Now, you are recording actions in word, those actions make fields, you are asserting the text that represents the fields, life is just great! One more assertion method to make, we just need to get at the code in that field. Umm. Huh. We’ll take the method we used to assert the text, and modify it a little.. GetProperty(“Fields”) should work instead of GetProperty(“Text”) – Umm… Oh and we want to cast that as a Word.Fields object, good. Access the first member (interesting thing, Word starts it’s indices at 1 instead of 0.. sigh) so fields.Code.Text. Sweet!
Run the test.. works, works, everything is going great.. WHAT?! FAILED?! I hate my job
Okay what’s the exception? System.NotSupportedException? So you are saying I can’t access the fields?
That’s exactly what I’m saying! If you try to access the Fields member of a selection in Word, using the CUI Word Add-In, well you are just SOL. Sorry!
Oh, I’m here to help you with that actually. So open up the CuiWordAddin solution you got from http://vsarcodeduiword.codeplex.com/, open the UITestCommunicator.cs file from the WordAddIn project and find the GetSelectionProperty(WordSelectionInfo selectionInfo, string propertyName) method. At the bottom of that switch case, add another case:
case PropertyNames.Fields: return selection.Fields;
And then in WordPropertyProvider.cs from the WordExtension project, get into the InitializePropertiesMap() method and add the following line:
map.Add(PropertyNames.Fields, new UITestPropertyDescriptor(typeof(Word.Fields), Read));
And that’s it. Build (F6), install the new add-in and enjoy!