1

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?

Community
  • 1
  • 1
Gavin Stevens
  • 673
  • 5
  • 14
  • did you try to click the 'pause' button in the debugger to know where it hangs in the thread ? what do your log messages say ? – Baptiste Pernet Aug 08 '11 at 23:17
  • What did you do to let the browserInstance = new IE (browser) not return null? If I try this method out in my form class, then it works. But if I try to send the WebBrowser instance to another class and let it instantiate there, it fails. – dylanmensaert Dec 30 '12 at 15:57

1 Answers1

0

Script error has not been disabled.

 webBrowser.ScriptErrorsSuppressed = true

further, try to start with stable code:

public void SearchForWatiNOnGoogle()
{
   using (var browser = new IE("http://www.google.com"))
   {
   browser.TextField(Find.ByName("q")).TypeText("WatiN");
   browser.Button(Find.ByName("btnG")).Click();
   Assert.IsTrue(browser.ContainsText("WatiN"));
   }
}
Harshal Doshi Jain
  • 2,567
  • 1
  • 20
  • 16