Data Driven Approach Coded UI
Data driven approach is one of the
essential aspect of test automation. Simply, the aim is to execute particular scenarios in
multiple times with different sets of input and response data. This approach
can be used to run single test to verify numerous test cases by driving the
test with input and output values from an external data source (CSV, SQL DB,
Excel, XML, etc…) instead of using the same hard-coded values each time the
test runs. By considering these aspects, Visual Studio – Coded UI includes
specific features that let us easily access data stored in an Excel sheet, CSV
file, XML file or in a database table.
Sample Code For Login (Data Driven
Concept)
For Using Excel file
[DataSource("System.Data.Odbc", "Dsn=Excel Files;Driver={Microsoft
Excel Driver (*.xls)};dbq=|DataDirectory|\\Data
Source\\Demo.xls;defaultdir=.;driverid=790;maxbuffersize=2048;pagetimeout=5;readonly=true",
"Login$", DataAccessMethod.Sequential), TestMethod]
public void Login()
{
#region Login Variable Declarations
BrowserWindow Brows = new
BrowserWindow();
HtmlEdit EmpID = new HtmlEdit(Brows);
HtmlEdit Pwd = new HtmlEdit(Brows);
HtmlInputButton LoginButton
= new HtmlInputButton(Brows);
#endregion
#region Search
Properties
Brows.SearchProperties[BrowserWindow.PropertyNames.Name]
= "Login";
EmpID.SearchProperties[HtmlEdit.PropertyNames.Name]
= "txtEmployeeID";
Pwd.SearchProperties[HtmlEdit.PropertyNames.Name]
= "txtPassword";
LoginButton.SearchProperties[HtmlInputButton.PropertyNames.Name]
= "btnLogin";
#endregion
//Enter Emp Id and
Password
EmpID.Text =
TestContext.DataRow["EmployeeId"].ToString();
Pwd.Text =
TestContext.DataRow["Password"].ToString();
//Click Login Button
Mouse.Click(LoginButton);
}
For Using CSV file
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
"|DataDirectory|\\data.csv", "data#csv",
DataAccessMethod.Sequential), DeploymentItem("data.csv"), TestMethod]
For Using XML file
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
"|DataDirectory|\\data.xml", "Iterations",
DataAccessMethod.Sequential), DeploymentItem("data.xml"), TestMethod]
For Test
case in Team Foundation Server
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.TestCase",
"http://vlm13261329:8080/tfs/DefaultCollection;Agile",
"30", DataAccessMethod.Sequential), TestMethod]
For SQL
Express
[DataSource("System.Data.SqlClient",
"Data Source=.\\sqlexpress;Initial Catalog=tempdb;Integrated
Security=True", "Data", DataAccessMethod.Sequential),
TestMethod]
Data Source Type
|
Data Source attribute
|
CSV
|
[DataSource(“Microsoft.VisualStudio.TestTools.DataSource.CSV”,
“|DataDirectory|\\data.csv”, “data#csv”,
DataAccessMethod.Sequential), DeploymentItem(“data.csv”), TestMethod]
|
Excel
|
DataSource(“System.Data.Odbc”, “Dsn=Excel
Files;Driver={Microsoft Excel Driver (*.xls)};dbq=|DataDirectory|\\Data.xls;defaultdir=.;driverid=790;maxbuffersize=2048;pagetimeout=5;readonly=true”,
“Sheet1$”,
DataAccessMethod.Sequential), TestMethod]
|
Test Case in TFS
|
[DataSource(“Microsoft.VisualStudio.TestTools.DataSource.TestCase”,
“http://vsalm:8080/tfs/DefaultCollection;FabrikumFiber”, “30”,
DataAccessMethod.Sequential), TestMethod]
|
XML
|
[DataSource(“Microsoft.VisualStudio.TestTools.DataSource.XML”,
“|DataDirectory|\\data.xml”,
“Iterations”, DataAccessMethod.Sequential), DeploymentItem(“data.xml”), TestMethod]
|
SQL Express
|
[DataSource(“System.Data.SqlClient”, “Data
Source=.\\sqlexpress;Initial Catalog=tempdb;Integrated Security=True”,
“Data”, DataAccessMethod.Sequential), TestMethod]
|
TestContext.DataRow[“<Field
Title>”].ToString());
·
You need
to right click your Excel file or XML file or CSV file, choose
"Properties".
Change the "Copy to Output
Directory" build action to
"Copy Always".

No comments:
Post a Comment