Changeset 1741

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

Testing IE6 vs IE7

Files:

Legend:

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

    r1740 r1741  
    11using System; 
    2 using System.Collections.Generic; 
    32using System.Linq; 
    43using System.Text; 
    54using System.Threading; 
    6 using System.Diagnostics; 
    7 using SHDocVw; 
    8  
    9  
     5using WatiN.Core; 
    106 
    117namespace BrowserAutomation 
    128{ 
    13     /// <summary> 
    14     /// The IE class controls an instance of Internet Explorer 
    15     /// </summary> 
    169    class IE 
    1710    { 
    18         private SHDocVw.InternetExplorer internetExplorer; 
     11        private WatiN.Core.IE ie; 
     12        private Thread hardTimeoutThread; 
    1913        private Thread ieThread; 
     14        private bool started; 
    2015        private String url; 
    21         private object o; 
    22         private int unfinishedDownloads; 
    23         private int navigateCounter; 
    24         private bool completed; 
    2516        private DateTime startTime; 
    2617        private TimeSpan duration; 
    27         private Process[] myOpenIEs; 
    2818 
    2919        /// <summary> 
    30         /// Constructor for an instance of IE 
     20        /// Constructor for an instance of Firefox 
    3121        /// </summary> 
    3222        /// <param name="url">URL to be navigated to</param> 
     
    3424        { 
    3525            this.url = url; 
     26            started = false; 
     27            hardTimeoutThread = new Thread(new ThreadStart(HardTimeoutCallback)); 
    3628            ieThread = new Thread(new ThreadStart(IECallback)); 
    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); 
     29            ieThread.SetApartmentState(ApartmentState.STA); 
    4730            ieThread.Start(); 
     31            hardTimeoutThread.Start(); 
    4832        } 
    49          
     33 
     34        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 
    5046        /// <summary> 
    51         /// This method will be called when the thread is started 
     47        ///  
    5248        /// </summary> 
    53         private void IECallback() 
     49        private void HardTimeoutCallback() 
    5450        { 
    5551            while (true) 
    5652            { 
    57                 if (url != null
     53                if (!started
    5854                { 
    59                     internetExplorer.Visible = true; 
    60                     internetExplorer.Navigate(url, ref o, ref o, ref o, ref o); 
    61                     url = null; 
     55                    started = true; 
    6256                    startTime = DateTime.Now; 
    6357                } 
     
    6559                duration = DateTime.Now - startTime; 
    6660 
    67                 if(duration.Seconds >= 30) 
     61                if (duration.Seconds >= 30) 
    6862                { 
    69                     myOpenIEs = Process.GetProcessesByName("iexplore"); 
    70                     foreach (Process myOpenIE in myOpenIEs) 
    71                     { 
    72                         myOpenIE.Kill(); 
    73                     } 
     63                    ie.Close(); 
    7464                    ieThread.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                     } 
     65                    hardTimeoutThread.Abort(); 
    8866                } 
    8967            } 
    9068        } 
    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         } 
    16769    } 
    16870}