Skip to content

Instantly share code, notes, and snippets.

@RhysC
Created March 16, 2013 06:59
Show Gist options
  • Save RhysC/5175327 to your computer and use it in GitHub Desktop.
Save RhysC/5175327 to your computer and use it in GitHub Desktop.
html forms validation based on the teststack.seleno page model. Faked out page and assert, need to adjust to fit. This is designed to work with DynamicCsv Gist.
//Will need some rework and some decent test runs. IDeally generate the start of the csv too.
public class PageFormTestInput<T> where T : new()
{
private const string isFormValidFieldName = "IsFormValid";
private readonly dynamic input;
public PageFormTestInput(dynamic input)
{
this.input = input;
}
public T CreateModel()
{
var type = typeof(T);
var instance = new T();
foreach (var property in type.GetProperties())
{
var propertyName = property.Name;
if (property.CanWrite)
{
if(!((IDictionary<string,object>)input).ContainsKey(propertyName))
continue;
property.SetValue(instance, ((IDictionary<string,object>)input)[propertyName], null);
}
}
return instance;
}
public void AssertExpectations(Page page)
{
var type = typeof(T);
foreach (var property in type.GetProperties())
{
var propertyName = property.Name;
var expectedPropValidityName = string.Format("Is{0}Valid",propertyName);
if(!((IDictionary<string,object>)input).ContainsKey(expectedPropValidityName))
{
string.Format("no {0} exists",expectedPropValidityName ).Dump();
continue;
}
var expected = ((IDictionary<string,object>)input)[expectedPropValidityName];
var actual = page.IsFieldValid(propertyName);
Assert.Equal(expected,actual,propertyName);
}
if(!((IDictionary<string,object>)input).ContainsKey(isFormValidFieldName))
{
string.Format("no {0} exists",isFormValidFieldName ).Dump();
return;
}
var expectedFormValidity = ((IDictionary<string,object>)input)[isFormValidFieldName];
var actualFormValidity = page.IsFormValid();
Assert.Equal(expectedFormValidity,actualFormValidity, "Form");
}
}
//Main and fakes
void Main()
{
dynamic csvLine = new ExpandoObject();
csvLine.FirstName = "Rhys";
csvLine.IsFirstNameValid = "TRUE";
csvLine.IsFormValid = "TRUE";
var pageInput = new PageFormTestInput<PersonalDetails>(csvLine);
var model = pageInput.CreateModel();
model.Dump();
pageInput.AssertExpectations(new Page());
}
public class PersonalDetails
{
public string FirstName { get; set; }
}
public class Page
{
public string GetInputValue(string DomId)
{
return "rhys";
}
public bool IsFieldValid(string DomId)
{
return false;
}
public bool IsFormValid()
{
return true;
}
}
public class Assert
{
public static void Equal(object expected, object actual, string message)
{
if( expected.Equals(actual))
String.Format("Equal {0} :{1}", expected, actual).Dump();
else
String.Format("{2}*** Not Equal {0} :{1} ***", expected, actual, message).Dump();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment