In this tutorial for Learning to create a test automation framework with C#, Selenium 3 and Nunit , we’ll be adding data driven support to the test automation framework we’ve built and will read data from CSV file in C#.
Tutorial Overview:
Class 1 – Creating an Automated Test Using Selenium WebDriver 3 and C#
- Setting Up the Development Environment for Your Selenium Automation Framework
- How to Inspect Web Elements & Methods to Locate Them with Chrome Devtools
- How to Find Web Elements with Selenium WebDriver
Class 2 – How to Create a Test Automation Framework Architecture with Selenium WebDriver 3
- How to Create a Cross Browser Compatible Testing Framework
- Steps to Develop a Report Module in a Testing Framework
- Implementing a Report Module in an Automated Framework
- How to Write a Functional Test with a Basic Selenium Automation Framework
Class 3 – Utilizing Test Automation Framework with Advanced Capabilities
- Page Object Pattern: Advantages & Implementation
- You’re here→Read Data From CSV File in C#
- How to Create a Test Suite in Selenium WebDriver and C#
- How to Use Data Driven with Selenium Test Suite
Data driven is external to your functional tests, it is loaded and used to extend your automated test cases. One of the best examples is that of a customer login form with multiple accounts. To use data-driven testing in this scenario, you might record a single automated test.
In our example, we are going to use an external CSV file which will be our external source of information and we will read data from it in our C# code. To create data driven support, we’ll create a new class named ‘General.cs’ and paste the following code:
using System.Collections.Generic; using System.IO; namespace Test { public class General { public List<string> loadCsvFile(string filePath) { var reader = new StreamReader(File.OpenRead(filePath)); List<string> searchList = new List<string>(); while(!reader.EndOfStream) { var line = reader.ReadLine(); searchList.Add(line); } return searchList; } } }
This function receives the path we will decide to work with and retrieves a list of the information from the file. In the example below we’ll be creating another class called ‘Servers’, a similar technology to the Page Object Model. Paste the following code:
namespace Test { public static class Servers { private static T getServers<T>() where T : new () { var servers = new T(); return servers; } public static General general { get { return getServers<General>(); } } } }
Congratulations, now our test automation framework is will be able to read data from CSV File in C#, meaning loading CSV files! In the following tutorials we’ll be learning how to incorporate these capabilities in our automated tests. Continue on with the tutorial and Create a Test Suite in Selenium Webdriver and C#.
-Please share your ideas and insights in the comment below!
Hey Asya,
Thanks for the tutorial. I’m really new to test automation and I am wondering how large of a data set do I need to be able to run this?
Hi Bernard, the data set can be of any size you like it to be 🙂
As a side note – Since you mentioned you are new to test automation – I would recommend checking out TestProject Platform (it’s free) – https://testproject.io/. A great team of test automation engineers is there to guide you through your test automation journey 🙂