First of all, you're trying to automate Internet Explorer from the server-side ASP.NET code. This is generally a bad idea. This article describes the implications of doing this with Office, the same concerns apply to Internet Explorer.
That said, to succeed with what you're trying to do, you'd need to run an STA thread on the server side, and run your Watin code inside that thread. Placing [STAThread]
on your ASP.NET Page_Load
handler won't automagically make this happen.
Here is how it can be done, but again, doing so on the server is not recommended:
protected void Page_Load(object sender, EventArgs e)
{
RunOnStaThread<object>(() =>
{
IE ie = new IE("http://localhost:5243/Addition.aspx");
ie.TextField(Find.ByName("txtbox1")).TypeText("1");
ie.TextField(Find.ByName("txtbox2")).TypeText("2");
ie.Button(Find.ByValue("Calculate")).Click();
return null;
});
}
static T RunOnStaThread<T>(Func<T> func)
{
var tcs = new TaskCompletionSource<T>();
var thread = new Thread(() =>
{
try
{
tcs.SetResult(func());
}
catch (Exception ex)
{
tcs.SetException(ex);
}
});
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
try
{
return tcs.Task.Result;
}
finally
{
thread.Join();
}
}