Tuesday, July 14, 2015

Creating a Data Driven Coded UI Testing with different data sources

After creating a coded UI test, you might want to run your coded UI test multiple times with different sets of data to test different scenarios. This lab will show you how to do this by adding parameter values from a data source to create a data-driven coded UI test. Each row in the data source is an iteration of the coded UI test. The overall result of the test will be based on the outcome for all the iterations All changes to your test to enable it to use a data source for the parameters will be made to your test's source code file (i.e. CodedUITest1.cs). You cannot modify the code in the UIMap.Designer.cs file.

//This is a recorded method for addition and validating the sum

[TestMethod] 
 public void CodedUITestMethod1() 
       {           
           this.UIMap.Addition();   
         this.UIMap.validation(); 
       }

We will only add data for the calculator test method that adds two numbers and verifies that they are added together correctly for this test to pass.The data source can be a csv, XML, Excel, Data Base and Test Case that contains the following data
 Num1   Num2  Sum    

   1          2        3    
   2          4        6

Different Data sources

 //Data source for Test Case "Test Case from Team Foundation Server"       [DataSource("Microsoft.VisualStudio.TestTools.DataSource.TestCase", "http://vsalm:8080/tfs/fabrikamfibercollection;Calculator", "248", DataAccessMethod.Sequential)]


 //Data source for XML 

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\CalcData.xml", "DataContextRow", DataAccessMethod.Sequential), DeploymentItem("CodedUITestProject2\\CalcData.xml")]
  
//Data source for CSV 
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("CodedUITestProject2\\data.csv")]

//Data source for Excel

 [DataSource("System.Data.Odbc", "Dsn=Excel Files;dbq=|DataDirectory|\\data.xlsx;defaultdir=C:\\TestData;driverid=1046;maxbuffersize=2048;pagetimeout=5", "Sheet1$", DataAccessMethod.Sequential), DeploymentItem("CodedUITestProject2\\data.xlsx")]

//Data source for SQL

[DataSource("System.Data.SqlClient", "Data Source=.\\sqlexpress;Initial Catalog=Tfs_FabrikamFiberCollection;Integrated Security=True", "CodedUI_Test", DataAccessMethod.Sequential)]

Here i am using csv file, must be copied to the output directory, or the test can’t run. Use the Properties window to copy it.
Deploy the .CSV file
To find the search property open the UIMap.uitest file for solution explorer.
Open the Coded UI Test Editor
Click on UI Action "Click '1' button " then it will highlight in UI  Control Map  
"UIItem1Button"

Use the Coded UI Test Editor to assist with code
right click on control "UIItem1Button" choose properties there you will see the Search Properties  click on it Name properties which equals to 1.

For every run we will replace this value.

by seeing this UIMap.uitest we can wright the code to over right the values like

  this.UIMap.UICalculatorWindow.UIItemWindow.UIItem1Button.SearchProperties[WinButton.PropertyNames.Name] = TestContext.DataRow["Num1"].ToString();

Use the search properties to assist in coding
Add using Microsoft.VisualStudio.TestTools.UITesting.WinControls to the top of the CodedUITest.cs file

 [TestMethod]

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("CodedUITestProject1\\data.csv")]

public void CodedUITestMethod1() 
       {
           this.UIMap.UICalculatorWindow.UIItemWindow.UIItem1Button.SearchProperties[WinButton.PropertyNames.Name] = TestContext.DataRow["Num1"].ToString(); 
           this.UIMap.UICalculatorWindow.UIItemWindow2.UIItem2Button.SearchProperties[WinButton.PropertyNames.Name] = TestContext.DataRow["Num2"].ToString();

  this.UIMap.Addition(); 

 this.UIMap.validationExpectedValues.UIItem12TextDisplayText = TestContext.DataRow["Sum"].ToString(); 


this.UIMap.validation();

        }


0 comments:

Post a Comment