related to: How to use watin with WebBrowser control?
So, I read the appropriate section which discusses the STA model here:
http://watin.org/documentation/sta-apartmentstate/
Now the question is, how do you deal with this?
I created a method to navigate:
public void NavigateTo(object browser, string url)
{
var t = new Thread(() =>
{
Settings.AutoStartDialogWatcher = false;
browserInstance = new IE(browser);
browserInstance.GoTo(url);
} );
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
I call this by passing in my Browser ActiveX instance:
driver.NavigateTo(webBrowser1.ActiveXInstance, "http://www.google.com");
Now I try to login to the target site:
public void OpenSiteAndLogin(object browser)
{
if (_Log.IsDebugEnabled) _Log.DebugFormat("Open Site and Login");
var t = new Thread(() =>
{
Settings.AutoStartDialogWatcher = false;
//browserInstance = new IE(browser);
//browserInstance.GoTo(baseURL);
using (var browserInstance = new IE(browser))
{
try
{
if (_Log.IsDebugEnabled) _Log.DebugFormat("Looking for Account Element");
TextField account = browserInstance.TextField(Find.ByClass("account-link"));
if (_Log.IsDebugEnabled) _Log.DebugFormat("Logged In");
loggedIn = true;
}
catch (Exception)
{
if (_Log.IsDebugEnabled) _Log.DebugFormat("Not found, not logged in");
}
if (!loggedIn)
{
if (_Log.IsDebugEnabled) _Log.DebugFormat("not logged in, click login");
Element login = browserInstance.Element(Find.ByText("Login"));
login.Click();
if (_Log.IsDebugEnabled) _Log.DebugFormat("fill in login info");
browserInstance.TextField(Find.ById("id_auth-username")).TypeText("test");
browserInstance.TextField(Find.ById("id_auth-password")).TypeText("test");
browserInstance.Element(Find.BySelector("input[type=submit]")).Click();
if (_Log.IsDebugEnabled) _Log.DebugFormat("submit login info");
if (_Log.IsDebugEnabled) _Log.DebugFormat("wait for account element");
browserInstance.WaitForComplete();
if (_Log.IsDebugEnabled) _Log.DebugFormat("Logged In");
loggedIn = true;
}
}
}
);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
}
So, I try clicking a button to do this:
WebDriver driver = new WebDriver();
driver.NavigateTo webBrowser1.ActiveXInstance, "http://www.google.com");
driver.OpenSiteAndLogin(webBrowser1.ActiveXInstance);
I tried something like this:
WebDriver driver = new WebDriver();
driver.NavigateTo(webBrowser1.ActiveXInstance, "http://www.google.com");
Application.DoEvents();
Thread.Sleep(5000);
driver.OpenSiteAndLogin(webBrowser1.ActiveXInstance);
but this doesnt work either, the whole thing hangs. What is the best way to wait for this spawned thread without locking the UI thread and move onto the next operation when it's complete?