DotNetCurry Logo

Coded UI Test – Re-use and Customize the code generated with CUIT Builder using Visual Studio 2012

Posted by: Gouri Sohoni , on 2/7/2013, in Category Visual Studio, VSTS & TFS
Views: 56465
Abstract: In this article, we will discuss how to customize and re-use the code which gets automatically generated with CUIT Builder using Visual Studio 2012.

In previous articles, we have seen various aspects of working with Coded UI Test (CUIT). We have discussed starting from preliminary creation of CUIT to variations and best practices of it. We also have discussed how data driven Coded UI test can be created. In this article, we will discuss how to customize and re-use the code which gets automatically generated with CUIT Builder using Visual Studio 2012.

We will also customize the code created to suit our needs and explore how the code from the designer (UIMap.Designer.cs) file can be copied in the a partial class (UIMap.cs) and customized.

 

We will start by creating a Coded UI Test to start a browser (Internet Explorer), enter a url, provide search criteria and add assertion for the number of results found.

1. Create a Coded UI Test project using Visual Studio 2012

cuit-01

2. Rename the default name from “CodedUITest1.cs” to “CUITCustomize.cs” as follows

cuit-02

3. Change the method name

cuit-03

4. Now that we have changed the method name as well as the class name, we will add code by using Coded UI Test Builder.

cuit-04

5. Start Coded UI Test Builder

cuit-05

6. We will record actions. Start browser Internet Explorer and enter URL http://www.dotnetcurry.com

cuit-06

7. Provide search criteria for finding out how many articles are available for “Coded UI Test”.

cuit-07

8. Add assertion for the InnerText property.

cuit-08

9. Change the assertion criteria from Equal to to Contains

cuit-09

10. Generate separate method names for different actions and finally for assertion

cuit-10

11. The test method created looks as below

cuit-14

12. Stop the Coded UI Test Builder.

13. Start Test Explorer window if it is not already shown, and execute the test

cuit-11

14. The test execution is successful and we get following result

cuit-13

15. Now we will convert this normal Coded UI Test to Data Driven CUIT. Add a .csv file as a data file which will have search criteria and number expected for articles. Add the attributes to the method and the code and the data file looks as below

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV”,|DataDirectory|Searches.csv",
            "Searches#csv",DataAccessMethod.Sequential),]
        [DeploymentItem("Searches.csv")]
        [TestMethod]
public void CodedUITestMethod_CustomizeCode()
{
           this.UIMap.SearchStringParams.UICtl00qEditText = TestContext.DataRow["SearchString"].ToString();
this.UIMap.AssertNumberofSearchesExpectedValues.UIAbout44results016secPaneInnerText =
               TestContext.DataRow["Number"].ToString();
this.UIMap.EnterURL();
this.UIMap.SearchString();
this.UIMap.AssertNumberofSearches();
}

image

16. Observe the change in the code where the parameter for search criteria is taken from data source. Similarly there is a change in the assert expected values.

17. Now we need to change the code for assertion where instead of searching for string value, we want to check the minimum number of articles by converting to integer.

18. We cannot change the method for assertion in the UIMap.Designer.cs. If you change this file, the changes to the file will be overwritten. This file gets re-generated from UIMap.uitest. If we want to change the method AssertNumberofSearches, we will go ahead with the following approach

Copy the complete code for the method and put as a part of other partial class file. We cannot have the same method name in the same class so change the name of the method and customize the code as required. The original method looks as below

public partial class UIMap
{
public void AssertNumberofMinSearches()
{
    #region Variable Declarations
    HtmlDiv uIAbout44results016secPane =
        this.UIDotnetcurrycomSearchWindow.UIGoogleSearchFrameFrame.UIGoogleCustomSearchDocument.UIAbout44results016secPane;
    #endregion

    // Verify that the 'InnerText' property of 'About 44 results (0.16 seconds)' pane contains '44'
    StringAssert.Contains(uIAbout44results016secPane.InnerText, this.AssertNumberofSearchesExpectedValues.UIAbout44results016secPaneInnerText,
        "number of results not matching");
}

19. The Customized code looks as follows

public partial class UIMap
{
public void AssertNumberofMinSearches()
{
    #region Variable Declarations
    HtmlDiv uIAbout44results016secPane =
        this.UIDotnetcurrycomSearchWindow.UIGoogleSearchFrameFrame.UIGoogleCustomSearchDocument.UIAbout44results016secPane;
    #endregion
    string str = uIAbout44results016secPane.InnerText;
    string[] words = str.Split(' ');
    if (Convert.ToInt16(words[1]) < Convert.ToInt16(this.AssertNumberofSearchesExpectedValues.UIAbout44results016secPaneInnerText))
    {
        Assert.AreEqual(true, true);
    }

    else
    {
        Assert.AreEqual(true, false);
    }
   
}

}

20. Now when we use this method in the calling code, the complete code looks as below

cuit-15

While executing the CUIT, it will be iterated as many number of times as we have records in the data file

In this article, we discussed how we can re-use the code generated by using Coded UI Test Builder in the designer file. We also discussed how the code can be customized. It is not possible to change the code in the designer file as it gets overwritten every time. We need to add the code in the other partial class file provided. We can only copy those methods from UIMap.Designer.cs to UIMap.cs which needs to be modified keeping the remaining code intact.

Was this article worth reading? Share it with fellow developers too. Thanks!
Share on Google+
Further Reading - Articles You May Like!
Author
Gouri Sohoni is a Trainer and Consultant for over two decades. She specializes in Visual Studio - Application Lifecycle Management (ALM) and Team Foundation Server (TFS). She is a Microsoft MVP in VS ALM, MCSD (VS ALM) and has conducted several corporate trainings and consulting assignments. She has also created various products that extend the capability of Team Foundation Server.


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by bharat on Friday, May 31, 2013 12:19 AM
good
Comment posted by Burdette Lamar on Tuesday, May 20, 2014 1:26 PM
I'm building hand-code CUITs, which I write about in my blog:  http://burdettelamar.wordpress.com
Comment posted by chinna on Thursday, July 3, 2014 10:38 AM
Hi,

I am new user to coded UI and coding as well. I entered into automation and need to work on Coded UI using specflow. Please let me know whether is there any possibility to help me.

Thanks,
Chinna.
Comment posted by Oscar R (Bogota-Colombia) on Tuesday, August 12, 2014 11:08 AM
Hello,
My name is Oscar, I don´t speak english but I will try to explain me.

I need a couple of methods
the first one must be login in aplication  and after the second one must be a cycle of interactions (number of iteractions defined by CSV file)and
finaly another method that must be a logout.

is clear??

can you help me with some ideas??

Thanks in advance.