Changeset 1740

Show
Ignore:
Timestamp:
08/14/08 15:00:23 (4 months ago)
Author:
bhenderson
Message:

Modified for IE6 compatibility

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • honeyclient/branches/exp/bhenderson-browser_automation/BrowserAutomation/IE.cs

    r1733 r1740  
    11using System; 
     2using System.Collections.Generic; 
    23using System.Linq; 
    34using System.Text; 
    45using System.Threading; 
    5 using WatiN.Core; 
     6using System.Diagnostics; 
     7using SHDocVw; 
     8 
     9 
    610 
    711namespace BrowserAutomation 
    812{ 
     13    /// <summary> 
     14    /// The IE class controls an instance of Internet Explorer 
     15    /// </summary> 
    916    class IE 
    1017    { 
    11         private WatiN.Core.IE ie; 
    12         private Thread hardTimeoutThread; 
     18        private SHDocVw.InternetExplorer internetExplorer; 
    1319        private Thread ieThread; 
    14         private bool started; 
    1520        private String url; 
     21        private object o; 
     22        private int unfinishedDownloads; 
     23        private int navigateCounter; 
     24        private bool completed; 
    1625        private DateTime startTime; 
    1726        private TimeSpan duration; 
     27        private Process[] myOpenIEs; 
    1828 
    1929        /// <summary> 
    20         /// Constructor for an instance of Firefox 
     30        /// Constructor for an instance of IE 
    2131        /// </summary> 
    2232        /// <param name="url">URL to be navigated to</param> 
     
    2434        { 
    2535            this.url = url; 
    26             started = false; 
    27             hardTimeoutThread = new Thread(new ThreadStart(HardTimeoutCallback)); 
    2836            ieThread = new Thread(new ThreadStart(IECallback)); 
    29             ieThread.SetApartmentState(ApartmentState.STA); 
     37            completed = false; 
     38            navigateCounter = 0; 
     39            unfinishedDownloads = 0; 
     40            internetExplorer = new SHDocVw.InternetExplorerClass(); 
     41            internetExplorer.NavigateError += new DWebBrowserEvents2_NavigateErrorEventHandler(this.navigationError); 
     42            internetExplorer.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.documentComplete); 
     43            internetExplorer.DownloadBegin += new DWebBrowserEvents2_DownloadBeginEventHandler(this.downloadBegin); 
     44            internetExplorer.DownloadComplete += new DWebBrowserEvents2_DownloadCompleteEventHandler(this.downloadComplete); 
     45            internetExplorer.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(this.beforeNavigate); 
     46            internetExplorer.NavigateComplete2 += new DWebBrowserEvents2_NavigateComplete2EventHandler(this.navigateComplete); 
    3047            ieThread.Start(); 
    31             hardTimeoutThread.Start(); 
    3248        } 
    33  
     49         
     50        /// <summary> 
     51        /// This method will be called when the thread is started 
     52        /// </summary> 
    3453        private void IECallback() 
    35         { 
    36             ie = new WatiN.Core.IE(); 
    37                 
    38             ie.GoTo(url); 
    39             Thread.Sleep(1000); 
    40             ie.Close(); 
    41                  
    42             hardTimeoutThread.Abort(); 
    43             ieThread.Abort(); 
    44         } 
    45  
    46         /// <summary> 
    47         ///  
    48         /// </summary> 
    49         private void HardTimeoutCallback() 
    5054        { 
    5155            while (true) 
    5256            { 
    53                 if (!started
     57                if (url != null
    5458                { 
    55                     started = true; 
     59                    internetExplorer.Visible = true; 
     60                    internetExplorer.Navigate(url, ref o, ref o, ref o, ref o); 
     61                    url = null; 
    5662                    startTime = DateTime.Now; 
    5763                } 
     
    5965                duration = DateTime.Now - startTime; 
    6066 
    61                 if (duration.Seconds >= 30) 
     67                if(duration.Seconds >= 30) 
    6268                { 
    63                     ie.Close(); 
     69                    myOpenIEs = Process.GetProcessesByName("iexplore"); 
     70                    foreach (Process myOpenIE in myOpenIEs) 
     71                    { 
     72                        myOpenIE.Kill(); 
     73                    } 
    6474                    ieThread.Abort(); 
    65                     hardTimeoutThread.Abort(); 
     75                } 
     76                else 
     77                { 
     78                    if (completed && (unfinishedDownloads == 0) && !internetExplorer.Busy) 
     79                    { 
     80                        Thread.Sleep(2000); 
     81                        if (completed && (unfinishedDownloads == 0) && !internetExplorer.Busy) 
     82                        { 
     83                            Thread.Sleep(10000); 
     84                            internetExplorer.Quit(); 
     85                            ieThread.Abort(); 
     86                        } 
     87                    } 
    6688                } 
    6789            } 
    6890        } 
     91 
     92        /// <summary> 
     93        /// This method is called when a Navigation Error event occurs 
     94        /// </summary> 
     95        /// <param name="pDisp"></param> 
     96        /// <param name="URL">The URL that caused the Navigation Error</param> 
     97        /// <param name="Frame"></param> 
     98        /// <param name="StatusCode">Status code of the Navigation Error (ex. 404, 501)</param> 
     99        /// <param name="Cancel"></param> 
     100        private void navigationError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel) 
     101        { 
     102             
     103        } 
     104 
     105        /// <summary> 
     106        /// This method is called when a Document Complete event occurs 
     107        /// </summary> 
     108        /// <param name="pDisp"></param> 
     109        /// <param name="URL">URL of the completed document</param> 
     110        private void documentComplete(object pDisp, ref object URL) 
     111        { 
     112            completed = true;   
     113        } 
     114 
     115        /// <summary> 
     116        /// This method is called when a Download Begins event occurs 
     117        /// Download Begins occurs every time the browser makes a valid  
     118        /// request for content form a server. 
     119        /// </summary> 
     120        private void downloadBegin() 
     121        { 
     122            unfinishedDownloads++; 
     123        } 
     124 
     125        /// <summary> 
     126        /// This method is called when a Download Complete event occurs 
     127        /// Download Complete occurs everytime the content downloaded after  
     128        /// a Download Begins is completed. 
     129        /// </summary> 
     130        private void downloadComplete() 
     131        { 
     132            unfinishedDownloads--; 
     133        } 
     134 
     135        /// <summary> 
     136        /// This method is event that is triggered before a URL is navigated to 
     137        /// A web page will usually make multiple requests to Navigate URLs 
     138        /// </summary> 
     139        /// <param name="pDisp"></param> 
     140        /// <param name="URL">URL to be navigated to</param> 
     141        /// <param name="Flags"></param> 
     142        /// <param name="TargetFrameName"></param> 
     143        /// <param name="PostData"></param> 
     144        /// <param name="Headers"></param> 
     145        /// <param name="Cancel"></param> 
     146        private void beforeNavigate(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel) 
     147        { 
     148            if(!((String)URL).Equals("about:blank")) 
     149            { 
     150                navigateCounter++; 
     151            } 
     152            completed = false; 
     153        } 
     154 
     155        /// <summary> 
     156        /// A Navigation Complete event occurs when the browser has finished navigating to 
     157        /// a URL. It seems that this event is not triggered for each beforeNavigate event. 
     158        /// It also seems that this event can be triggered more than once for each beforeNavigate 
     159        /// event. 
     160        /// </summary> 
     161        /// <param name="pDisp"></param> 
     162        /// <param name="URL">URL of the completed navigation</param> 
     163        private void navigateComplete(object pDisp, ref object URL) 
     164        { 
     165            navigateCounter--; 
     166        } 
    69167    } 
    70168}