Coded UI with CSV file

Data Driven Testing with CSV file

Add new item on Project

Select a Text file and named it as “data.csv”

Now select the file on Solution Explorer section

And we can see the Properties are –

We need to change “Copy to Output Directory” from “Do not copy” to “Copy always” and it will looks like –

Now provide the following sample data on “data.csv” file –

Fname,Lname,Address
Tanmay,Sarkar,MyHome

Now open this data.csv file from NotePad++ and we can see the encoding of this file is UTF-8-BOM

Change it to “ANSI” and save it.
A popup will appear on visual studio for this file change activity from outside the IDE

Click Yes.
Change the attribute from

[TestMethod]
To 
[TestMethod, DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv","data#csv",DataAccessMethod.Sequential), DeploymentItem("data.csv")]
And also change 
FirstName.Text = “Tanmay” to 
FirstName.Text = TestContext.DataRow["Fname"].ToString();
The complete code is – 

[TestMethod, DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv","data#csv",DataAccessMethod.Sequential), DeploymentItem("data.csv")]
        public void CodedUITestMethod1()
        {
            BrowserWindow Browser = new BrowserWindow();
            Browser.SearchProperties[UITestControl.PropertyNames.Name] = "Automation Sample 1:: Tanmay Sarkar";

            HtmlEdit FirstName = new HtmlEdit(Browser);
            FirstName.SearchProperties[UITestControl.PropertyNames.Name] = "ts_first_name";
            FirstName.Text = TestContext.DataRow["Fname"].ToString();

            HtmlEdit LastName = new HtmlEdit(Browser);
            LastName.SearchProperties[UITestControl.PropertyNames.Name] = "ts_last_name";
            LastName.Text = TestContext.DataRow["Lname"].ToString();

            HtmlTextArea Address = new HtmlTextArea(Browser);
            Address.SearchProperties[UITestControl.PropertyNames.Name] = "ts_address";
            Address.Text = TestContext.DataRow["Address"].ToString();
                        

        }

        
        [TestInitialize()]
        public void MyTestInitialize()
        {
            BrowserWindow.Launch("http://demo.tanmaysarkar.com/sample_01.html");
        }

And the output will be –

And the result will looks like –

We can also add multiple data row in our csv file.

Fname,Lname,Address
Tanmay,Sarkar,MyHome
Sukumar,Roy,SweetHome

But make sure the data file still is in “ANSI” format.
Now when we execute the test runner we will get the following output for our both data row

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *