My goal is to automate test in .NET WebBrowser control. Below code works just fine if I put my WatiN test code in the same file.
Code:
namespace WindowsFormsApplication1
{
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
}
private void Form1_Load(object sender, EventArgs e)
{
//I have WebBrowser control in the form
//so the WatiN code below works
var t = new Thread(() =>
{
Settings.AutoStartDialogWatcher = false;
var ie = new IE(webBrowser1.ActiveXInstance);
ie.GoTo("http://www.google.com");
ie.TextField(Find.ByClass("lst")).TypeText("this is awesome!!");
ie.Button(Find.ByName("btnG")).Click();
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
}//end form load
}//end class
}//end namespace
But it's not good to mix test code and target code. So I would like to take WatiN test code part out and put it in separate C# file in separate project. But then if I do so, I lose reference to the Form of course.
I've searched for something called WndProc (http://dotnet.sys-con.com/node/39039) but seems like it is not really what I am looking for.
So my question is:
is it possible to separate the WatiN code from target code?
given the fact, it is even possible to get the Form object that's already running? (I mean getting reference of the Form from other C# console app for instance? )
if so, could someone show me the sample code?
I've tried below in separate project but nothing happens after the Form1 is opened.
Because the process stop at Application.Run(myform)
var t = new Thread(() =>
{
Form1 myform = new Form1();
Application.Run(myform);
myform.textBox1.Text = "really";
Settings.AutoStartDialogWatcher = false;
var ie = new IE(myform.webBrowser1.ActiveXInstance);
ie.GoTo("http://www.google.com");
ie.TextField(Find.ByClass("lst")).TypeText("this is awesome!!");
ie.Button(Find.ByName("btnG")).Click();
});
t.SetApartmentState(ApartmentState.STA);
t.Start();