Nov 8, 2010

How to Use CodedUI Tests, Watin and MTM Together

A customer I’m working with has placed a heavy investment in Watin testing over the years and with a recent move to TFS2010 they also wanted to take advantage of the new Microsoft Test Manager (MTM) feature and the ability to associate automated tests to test cases in MTM.  Here’s a quick how-to for those of you wanting to do the same thing.

Create A Test Case

First up, let’s create a test case:

image

Pretty simple – open browser and check the url in the address bar.  What I have also done with this test is use parameters to supply the data allowing testers to decide what values they want to test with.  This is better than the devs doing this, plus it makes for a much nicer UI for data driven tests than excel or CSV files do.

Create The CodedUI Watin Test

Next we need to create the automation for this test.

Add a Coded UI test class to a test project but don’t go create any tests via the wizard that appears.  Just press cancel and then go to your test code and add a normal Watin test as per usual.  Oh, if you’re a person who normally deletes all that TestContext stuff, then you’ll need to leave it in place this time around – don’t delete it.

Here’s an example:

using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WatiN.Core;

namespace CodedUITesting
{
[CodedUITest]
public class CodedUITest1
{
...
[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.TestCase",
"http://tfs2008-vm:8080/tfs/TemplateTrials;Scrum v5",
"101", DataAccessMethod.Sequential)]
public void RunWatinTestParameterized()
{
var url = TestContext.DataRow["url"].ToString();
var result = TestContext.DataRow["result"].ToString();
using (IE ie = new IE(url))
{
Assert.AreEqual(result, ie.Url);
}
}
}

The things to note in this test are that the data source attribute is used to pull data from the TFS Test Case work item – specifically the URL of the team project is included, as it the test case id. 

In the test itself we pull data from the parameters in the test case using the TestContext.DataRow[“parameter'NameHere”].ToString() calls, and the rest is just normal Watin code.

If you have any issues with missing references make sure you reference Watin and Interop.ShDocVw and that both get copied to the output folder as part of the build.

Attach the Automation

The only thing we need to do once we have this test is to link it to the test case as it’s automation method.  Open the test case in Visual Studio, navigate to the Associated Automation tab and click the […] button to select the method.  You should see something like this once it’s done:

image

Run The Test

Assuming you have a lab environment with test agents installed you should then be able to trigger a new build and when it’s complete start an automated test run for that build and see everything working as you would expect.

Here’s a result from a run on my local TFS server:

image

I hope this helps!

1 comment:

  1. Can you expand on the "add a normal Watin test" we are trying to use codedui testing with google chrome and we are finding that codedui does not work with google chrome and are wondering how to wrap Watin code to do the google chrome testing.

    ReplyDelete