public void WhenIRegisterWithARegisteredEmailAddress() { ExcelData data = ExcelDataReaderUtil.FetchRowUsingKey("DuplicateEmail"); // this is lot of code duplication RegistrationPage = new HomePage(_driver).OpenRegistrationForm(); RegistrationPage.Register(data.FetchColumnValue("FirstName"), data.FetchColumnValue("LastName"), data.FetchColumnValue("Email"), data.FetchColumnValue("Password"), data.FetchColumnValue("ConfirmPassword")); }
// can only handle value types or types that implement IConvertible public static T CreateInstance <T>(ExcelData data) where T : class, new() { // Load Excel based on the type var type = typeof(T); var instance = Activator.CreateInstance <T>(); foreach (var propertyInfo in GetProperties()) { // if my custom attribute is found CustomJSONConverterAttribute attribute = (CustomJSONConverterAttribute)propertyInfo.GetCustomAttributes(false). FirstOrDefault(a => a.GetType() == typeof(CustomJSONConverterAttribute)); if (attribute != null) { // convert JSON formatted string to object var propertyType = propertyInfo.PropertyType; var convertedObj = JsonConvert.DeserializeObject(GetColumnValue(propertyInfo.Name), propertyType); propertyInfo.SetValue(instance, convertedObj, null); } else { var convertedValue = ConvertToPropertyValue(propertyInfo.PropertyType, GetColumnValue(propertyInfo.Name)) ?? default; propertyInfo.SetValue(instance, convertedValue, null); } } return(instance); IEnumerable <PropertyInfo> GetProperties() { foreach (var property in type.GetProperties().Where(p => p.CanWrite)) { yield return(property); } } string GetColumnValue(string columnName) { return(data.FetchColumnValue(columnName)); } }