Coded UI: Assembling the test

   Posted by: Tim Star

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

Credits

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:

image

Change the highlighted method to something meaningful like AddOneCoffeFlyerToCartTest

image

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

image

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:

image

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.

image

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.


Comments (2)

Ecommerce Designer 1/5/2012 3:53 AM

Great post with lots of imptorant stuff.

solar power systems 1/18/2012 3:33 AM

really amazing!! i read out your entire blog and found it very much interesting and informative thanks a lot for sharing this wonderful post keep posting !!

Add a Comment

*

*

Loading

Find Us
Contact Us 651-288-7000 1-800-866-9884
Home | Training | Curriculum | Course Finder | Schedule | Enroll | Twin Cities Java User Group | Consulting | Foundation | Jobs | About Us | Our Story | Press Room | Instructors | President | Map & Directions | Sitemap

Java Training | JSF / Struts / Spring / Hibernate Training | Java Power Tools Training | .NET 4.0 & Visual Studio 2010 Training | Microsoft Web Development Training | Prism / MVVM / MEF Training | .NET 3.5 and Visual Studio 2008 Training | .NET 2.0 and Visual Studio 2003 Training | Cloud Computing Training | Ajax / Web Services / XML Training | Groovy and Grails Training | SQL Server 2012 Training | SQL Server 2008 Training | SQL Server 2005 Training | Mobile Development Training | SharePoint 2010 Training | SharePoint 2007 Training | Agile, Process, Analysis & Design Training | Arch/Design Patterns Training | Microsoft Official Curriculum Training | Web Development Training | Ruby Training | Rational Application Developer (RAD) Training | WebSphere Application Server Training | WebSphere Portal Training | WebLogic Training | Boot Camp Training | Project Management Training | C / C++ Training | Metro / WinRT / Windows 8 Development Training | Retired

Intertech delivers training on-site and virtually serving cities including Phoenix, AZ | San Francisco, CA | Los Angeles, CA | San Diego, CA | San Jose, CA | Washington, DC | Chicago, IL | Orlando, FL | Boston, MA | Duluth, MN | Minneapolis St. Paul, MN | Rochester, MN | Raleigh-Durham, NC | New York, NY | Philadelphia, PA | Austin, TX | Dallas, TX | Houston, TX | Seattle, WA.