This is part 5 in a series of posts on creating and maintaining Coded UI tests with Multiple UI maps. My intent here is to give a little more detail to help those new to Visual Studio.
Part 1: Creating a new Coded UI solution
Part 2: Adding a New UI Map
Part 3: Modifying an Existing Coded UI Map
Part 4: Adding a UI Map to the TestRunUtility
Part 5: Assembling the test
Part 6: Changing Recorded Methods
Part 7: Deleting actions when recording with the Coded UI Test Builder
Part 8: Modifying Generated Code
This technique is an extension of the technique found in the following links by Anu , a Program Manager in the Visual Studio ALM Test Tools group and the MDSN documentation. http://blogs.msdn.com/b/anutthara/archive/2010/02/08/scaling-up-your-cuit-ui-automation-for-real-world-projects.aspx
http://blogs.msdn.com/b/anutthara/archive/2010/02/10/walkthrough-using-multiple-coded-ui-maps-in-test-automation.aspx
http://msdn.microsoft.com/en-us/library/ff398056.aspx
Assembling the Test
Time to ?assemble our test scenario?. Best practice dictates we rename CodedUITest1.cs so right click on file in solution explorer -> rename. Enter TailspinToysTest.cs (It is important to keep the .cs extension and click yes if prompted).
Now double click on the file name. In the editor pane you should see the following:

Change the highlighted method to something meaningful like AddOneCoffeFlyerToCartTest

Now add a using to the top of the TailspinToysTest class referencing the namespace in the TestRunUtility class.

Now we are ready to assemble the test.
In the AddOneCoffeeFlyerToCartTest instantiate an instance of the TestRunUtility.
[TestMethod]
public void AddOneCoffeFlyerToCartTest()
{
TestRunUtility trUtility = new TestRunUtility();
}
The first thing our test should do is launch IE and navigate to the home page.
Enter the following code to accomplish this
[TestMethod]
public void AddOneCoffeFlyerToCartTest()
{
TestRunUtility trUtility = new TestRunUtility();
trUtility.HomePage.NavigateToTailspinToysSite();
}
Now run the test and see if we get a new instance of IE launched and it navigates to TailspinToys correctly.
You can run or debug a single test by placing your mouse cursor in the test method and clicking the Run or Debug tests in current context buttons highlighted below:

Now that we are successfully launching the browser and navigating to TailspinToys we need to navigate through our pages until we get to the shopping cart. Implement the following code. The lines beginning with ?//? and are colored green are comments. You do not need to enter comments.
[TestMethod]
public void AddOneCoffeFlyerToCartTest()
{
TestRunUtility trUtility = new TestRunUtility();
trUtility.HomePage.NavigateToTailspinToysSite();
//On the home page click the Model Airplanes button
trUtility.HomePage.NavigateToModelAirplanes();
//From model airplane page, click the view fourth coffee flyer button
trUtility.ModelAirplanePage.ViewFourthCoffeeFlyer();
//From the fourth coffee Flyer details page, click add to cart
trUtility.FourthCopyFlyerDetailsPage.AddFourthCoffeeFlyerToCart();
//We should be at the shopping cart now
}
Run the test method again and validate that the browser is launched and we navigate all the way to the shopping cart.
We only have 3 steps left, adding the validation, cleaning out the cart, and closing the browser. The code for that is as follows
[TestMethod]
public void AddOneCoffeFlyerToCartTest()
{
TestRunUtility trUtility = new TestRunUtility();
trUtility.HomePage.NavigateToTailspinToysSite();
//On the home page click the Model Airplanes button
trUtility.HomePage.NavigateToModelAirplanes();
//From model airplane page, click the view fourth coffee flyer button
trUtility.ModelAirplanePage.ViewFourthCoffeeFlyer();
//From the fourth coffee Flyer details page, click add to cart
trUtility.FourthCopyFlyerDetailsPage.AddFourthCoffeeFlyerToCart();
//We should be at the shopping cart now
//Validate the quantity
trUtility.ShoppingCartPage.AssertQuantity();
//Clean out the cart
trUtility.ShoppingCartPage.RemoveItemFromCart();
//close the browser
trUtility.HomePage.CloseBrowser();
}
When the run is complete you should see the following in your test result window. The Green icon indicates our test passed.

One final improvement, if we are running a test and the test fails, the browser will be left open. We could either wrap the code in a using statement or put in a try-Finally. The code looks like this:
[TestMethod]
public void AddOneCoffeFlyerToCartTest()
{
TestRunUtility trUtility = new TestRunUtility();
try
{
trUtility.HomePage.NavigateToTailspinToysSite();
//On the home page click the Model Airplanes button
trUtility.HomePage.NavigateToModelAirplanes();
//From model airplane page, click the view fourth coffee flyer button
trUtility.ModelAirplanePage.ViewFourthCoffeeFlyer();
//From the fourth coffee Flyer details page, click add to cart
trUtility.FourthCopyFlyerDetailsPage.AddFourthCoffeeFlyerToCart();
//We should be at the shopping cart now
//Validate the quantity
trUtility.ShoppingCartPage.AssertQuantity();
//Clean out the cart
trUtility.ShoppingCartPage.RemoveItemFromCart();
}
finally
{
//close the browser
trUtility.HomePage.CloseBrowser();
}
}
Now if our test fails we will still close the browser window.
That is it! We have used the multiple-ui map technique to create a robust maintainable coded UI test. In our next few posts we will learns some techniques to help us maintain and enhance these tests.