First, I took the ultra-simple approach of making the controls public, and then accessing them from the unit test code. However, this implement breaks encapsulation and feels 'dirty'. John suggested Reflection. Reflection is good clean fun, so I started down that path, and then realized, that every class that derives from System.Windows.Forms.Control (Form, UserControl...) exposes a public controls collection! so................
[Test]
public void MyUITest()
{
MyForm form = new MyForm();
ComboBox combo1 = GetControl(form, "combo1") as ComboBox;
Assert.IsNotNull(combo1);
Assert.AreEqual(combo1.SelectedValue, "somevalue");
//TODO party on the combo1 control
}
private Control GetControl(Control parent, string name)
{
foreach(Control c in parent.Controls)
{
if(name == c.Name)
return c;
}
return null;
}
So, hopefully this helps and inspires someone else! Test first baby!
jk
1 comment:
SendKeys to the rescue! ;)
Post a Comment