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

Untitled

By: a guest on Jul 1st, 2012  |  syntax: None  |  size: 5.27 KB  |  views: 35  |  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. Can WatiN handle the CuteWebUI Uploader popup dialog?
  2. <OBJECT style="FILTER: alpha(opacity=1); WIDTH: 329px; HEIGHT: 100px; mozOpacity: 0.01; opacity: 0.01; mozopacity: 0.01" data="data:application/x-oleobject;base64, <a bunch of data>" width=329 height=100 type=application/x-silverlight-2></OBJECT>  
  3.     <param name="source" value="/CuteWebUI_Uploader_Resource.axd?type=file&file=silverlight.xap&_ver=634334311861475176"/>
  4.     <param name="windowless" value="true" object="" <=""/>
  5.        
  6. [TestMethod]
  7. public void SomeTest()
  8. {
  9.     Settings.MakeNewIeInstanceVisible = true;
  10.     Settings.AutoStartDialogWatcher = true;
  11.     Settings.AutoMoveMousePointerToTopLeft = false;
  12.     using (IE ie2 = new IE())
  13.     {
  14.         ie2.GoTo(URL);
  15.         ie2.Link(SomeButtonID).Click();
  16.         ie2.Image(AnotherButtonID).FireEvent("onclick");
  17.  
  18.  
  19.         // some debugging code wrapped around the next user action
  20.         // which is clicking on the attach file button
  21.         var helper = new DialogHandlerHelper();
  22.         using (new UseDialogOnce(ie2.DialogWatcher, helper))
  23.         {
  24.             Thread.Sleep(1 * 1000); // wait for attach button to be "ready"
  25.  
  26.             // Click button that triggers the dialog that states:
  27.             //     "file browsing dialog has been blocked"
  28.             //     "please click here and try again"
  29.             //
  30.             ie2.Button(FileAttachButtonID).FireEvent("onclick");
  31.         }
  32.         foreach(string dialogHandler in helper.CandidateDialogHandlers)
  33.         {
  34.             // nothing prints out here :(
  35.             Console.Out.WriteLine(dialogHandler);
  36.         }
  37.  
  38.  
  39.         // debug print out all elements with tagname = object
  40.         foreach (Element objectElement in ie2.ElementsWithTag("object"))
  41.         {
  42.             StringBuilder elementInfo = new StringBuilder();
  43.             elementInfo.AppendLine("--------------------------------------------");
  44.             elementInfo.AppendLine("element.tagname = " + objectElement.TagName);
  45.             elementInfo.AppendLine("element.style = " + objectElement.Style);
  46.             elementInfo.AppendLine("element.type = " + objectElement.GetAttributeValue("type"));
  47.             elementInfo.AppendLine("element.data = " + objectElement.GetAttributeValue("data"));
  48.             elementInfo.AppendLine("--------------------------------------------");
  49.             Console.Out.WriteLine(elementInfo.ToString());
  50.  
  51.             // none of these clicks make the dialog go away
  52.             objectElement.ClickNoWait();
  53.             objectElement.Click();
  54.             objectElement.DoubleClick();
  55.             objectElement.MouseEnter();
  56.             objectElement.MouseDown();
  57.             Thread.Sleep(500);
  58.             objectElement.MouseUp();
  59.         }
  60.  
  61.         // wait to see if dialog disappears after click
  62.         Thread.Sleep(300 * 1000);
  63.     }
  64. }
  65.        
  66. // Attach the IE instance used by WatiN to White
  67. InternetExplorerFactory.Plugin();
  68. string title = "[browser title here]"; // will be something like "WatinWhiteHybrid - Internet Explorer provided by ..."
  69. var ie = (InternetExplorerWindow)Application.Attach(watin.ProcessID).GetWindow(title);
  70. White.WebBrowser.Silverlight.SilverlightDocument sl = ie.SilverlightDocument;
  71.  
  72. // Click the button in the silverlight control using White
  73. sl.Get(SearchCriteria.ByAutomationId("ClickMeId")).Click();
  74.        
  75. FileUploadDialogHandler helper = new FileUploadDialogHandler(attachmentPath);
  76.             using (new UseDialogOnce(ie.DialogWatcher, helper))
  77.             {
  78.                 Thread.Sleep(1 * 1000); // wait for attach button to be "ready"
  79.                 ie.Button(browseButtonID).FireEvent("onclick");
  80.  
  81.  
  82.                 // When automating a file upload, there is a Silverlight popup in IE that forces an extra click
  83.                 // before opening the file open dialog.  WatiN does not support Silverlight automation
  84.                 // and the popup element was acting quirky in Microsoft's Coded UI Test, so we find the
  85.                 // dialog box UNDERNEATH the Silverlight popup and force one, lovely, mouse click.
  86.                 //===== Entering Coded UI Test land, beware! =====================================
  87.  
  88.                 // initialize Coded UI Test
  89.                 Playback.Initialize();
  90.                 BrowserWindow.CurrentBrowser = "IE";
  91.                 Process watinBrowserProcess = Process.GetProcessById(ie.ProcessID);
  92.                 BrowserWindow cuitBrowser = BrowserWindow.FromProcess(watinBrowserProcess); // attach Coded UI Test to the IE browser WatiN initialized
  93.  
  94.                 // get the window underneath the Silverlight popup
  95.                 UITestControl modalUnderSilverlightPopup = new UITestControl(cuitBrowser.CurrentDocumentWindow);
  96.                 modalUnderSilverlightPopup.SearchProperties.Add("id", windowElementUnderPopupID);
  97.  
  98.                 // get the X and Y pixel center of the window
  99.                 int centerX = modalUnderSilverlightPopup.BoundingRectangle.X + modalUnderSilverlightPopup.BoundingRectangle.Width / 2;
  100.                 int centerY = modalUnderSilverlightPopup.BoundingRectangle.Y + modalUnderSilverlightPopup.BoundingRectangle.Height / 2;
  101.  
  102.                 // Click!
  103.                 Mouse.Click(new Point(centerX, centerY));
  104.  
  105.                 // Shutdown Coded UI Test
  106.                 Playback.Cleanup();
  107.                 //===== End Coded UI Test land, you survived! yay! ============================
  108.         }