Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Wrapping WatiN dialog handling

By: a guest on Apr 13th, 2011  |  syntax: C#  |  size: 4.90 KB  |  views: 800  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public interface IAlertDialogHandler : IDialogHandler
  2. {
  3.     string Message { get; }
  4.     bool Exists();
  5.     void ClickOK();
  6.     void WaitUntilExists();
  7.     void WaitUntilExists(int waitInSeconds);
  8. }
  9.  
  10. public interface IConfirmDialogHandler : IAlertDialogHandler
  11. {
  12.     void ClickCancel();
  13. }
  14.  
  15. public class FirefoxAlertDialogHandler : IAlertDialogHandler
  16. {
  17.     private const string FindDialogWindowIdScript = @"for (i = 0; i < getWindows().length; i++) {
  18.  if (getWindows()[i].location.toString().indexOf('commonDialog.xul') >= 0) {
  19.    i;
  20.    break;
  21.  } else {
  22.    -1;
  23.  }
  24. }";
  25.  
  26.     private const string DialogHasCorrectButtonsScript = "typeof getWindows()[{0}].document.documentElement.getButton('accept') !== 'undefined' && typeof getWindows()[{0}].document.documentElement.getButton('cancel') !== 'undefined' && getWindows()[{0}].document.documentElement.getButton('cancel').hidden;";
  27.     private const string ClickOKButtonScript = "typeof getWindows()[{0}].document.documentElement.getButton('accept').click()";
  28.     private const string WindowClassName = "MozillaDialogClass";
  29.  
  30.     private string _message = string.Empty;
  31.  
  32.     FFDocument _nativeDocument;
  33.     int _windowIndex = -1;
  34.  
  35.     public FirefoxAlertDialogHandler(DomContainer hostBrowser)
  36.     {
  37.         FireFox hostFirefoxInstance = hostBrowser as FireFox;
  38.         if (hostFirefoxInstance != null)
  39.         {
  40.             _nativeDocument = hostFirefoxInstance.NativeDocument as FFDocument;
  41.         }
  42.     }
  43.  
  44.     protected FFDocument NativeDocument
  45.     {
  46.         get { return _nativeDocument; }
  47.     }
  48.  
  49.     protected virtual string HasCorrectButtonsScript
  50.     {
  51.         get { return DialogHasCorrectButtonsScript; }
  52.     }
  53.  
  54.     protected int WindowIndex
  55.     {
  56.         get
  57.         {
  58.             if (_nativeDocument != null && _windowIndex == -1)
  59.             {
  60.                 _windowIndex = _nativeDocument.ClientPort.WriteAndReadAsInt(FindDialogWindowIdScript);
  61.             }
  62.  
  63.             return _windowIndex;
  64.         }
  65.     }
  66.  
  67.     protected bool HasCorrectButtons
  68.     {
  69.         get
  70.         {
  71.             bool hasButtons = false;
  72.             if (_nativeDocument != null)
  73.             {
  74.                 hasButtons = _nativeDocument.ClientPort.WriteAndReadAsBool(HasCorrectButtonsScript, WindowIndex);
  75.             }
  76.  
  77.             return hasButtons;
  78.         }
  79.     }
  80.  
  81.     public string Message
  82.     {
  83.         get { return _message; }
  84.     }
  85.  
  86.     public bool Exists()
  87.     {
  88.         return WindowIndex >= 0 && HasCorrectButtons;
  89.     }
  90.  
  91.     public void ClickOK()
  92.     {
  93.         if (_nativeDocument != null)
  94.         {
  95.             _nativeDocument.ClientPort.WriteAndRead(ClickOKButtonScript, WindowIndex);
  96.         }
  97.     }
  98.  
  99.     public void WaitUntilExists()
  100.     {
  101.         WaitUntilExists(30);
  102.     }
  103.  
  104.     public void WaitUntilExists(int waitDurationInSeconds)
  105.     {
  106.         var tryActionUntilTimeOut = new TryFuncUntilTimeOut(TimeSpan.FromSeconds(waitDurationInSeconds));
  107.         tryActionUntilTimeOut.Try(() => Exists());
  108.  
  109.         if (!Exists())
  110.         {
  111.             throw new WatiNException(string.Format("Dialog not available within {0} seconds.", waitDurationInSeconds));
  112.         }
  113.     }
  114.  
  115.     #region IDialogHandler Members
  116.     public bool CanHandleDialog(WatiN.Core.Native.Windows.Window window, IntPtr mainWindowHwnd)
  117.     {
  118.         bool canHandle = false;
  119.         if (window.ClassName == WindowClassName)
  120.         {
  121.             canHandle = Exists();
  122.         }
  123.  
  124.         return canHandle;
  125.     }
  126.  
  127.     public bool HandleDialog(WatiN.Core.Native.Windows.Window window)
  128.     {
  129.         while (window.Exists())
  130.         {
  131.             Thread.Sleep(200);
  132.         }
  133.  
  134.         return true;
  135.     }
  136.  
  137.     #endregion
  138. }
  139.  
  140. public class FirefoxConfirmDialogHandler : FirefoxAlertDialogHandler, IConfirmDialogHandler
  141. {
  142.     private const string DialogHasCorrectButtonsScript = "typeof getWindows()[{0}].document.documentElement.getButton('accept') !== 'undefined' && typeof getWindows()[{0}].document.documentElement.getButton('cancel') !== 'undefined';";
  143.     private const string ClickCancelButtonScript = "getWindows()[{0}].document.documentElement.getButton('cancel').click()";
  144.  
  145.     public FirefoxConfirmDialogHandler(DomContainer hostBrowser)
  146.         : base(hostBrowser)
  147.     {
  148.     }
  149.  
  150.     protected override string HasCorrectButtonsScript
  151.     {
  152.         get { return DialogHasCorrectButtonsScript; }
  153.     }
  154.  
  155.     public void ClickCancel()
  156.     {
  157.         NativeDocument.ClientPort.WriteAndRead(ClickCancelButtonScript, WindowIndex);
  158.     }
  159. }
  160.  
  161. public class IEAlertDialogHandler : AlertDialogHandler, IAlertDialogHandler
  162. {
  163.     public void ClickOK()
  164.     {
  165.         this.OKButton.Click();
  166.     }
  167. }
  168.  
  169. public class IEConfirmDialogHandler : ConfirmDialogHandler, IConfirmDialogHandler
  170. {
  171.     public void ClickCancel()
  172.     {
  173.         this.CancelButton.Click();
  174.     }
  175.  
  176.     public void ClickOK()
  177.     {
  178.         this.OKButton.Click();
  179.     }
  180. }