WatiN 可以处理 CuteWebUI Uploader 弹出对话框吗?

发布于 2024-10-18 01:14:49 字数 3283 浏览 2 评论 0原文

我的背景:
我是 WatiN 新手,但对于编写自动化 Web UI 测试并不陌生。在我的新工作中,我们尝试使用 WatiN 进行 Web UI 测试(由于一些 CUIT 失败)。

我过去使用 ArtOfTest.WebAii 解决了这个问题,通过使用 Win32 鼠标单击与包含元素的幻数偏移,但我似乎找不到有关如何在 WatiN 中执行此操作的文档,我可以'我自己解决不了:\

我的问题:
出现此对话框,我似乎找不到 WatiN 单击它的方法。

在此处输入图像描述

该对话框具有以下标记:

<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>  
    <param name="source" value="/CuteWebUI_Uploader_Resource.axd?type=file&file=silverlight.xap&_ver=634334311861475176"/>
    <param name="windowless" value="true" object="" <=""/>

我的测试代码:

[TestMethod]
public void SomeTest()
{
    Settings.MakeNewIeInstanceVisible = true;
    Settings.AutoStartDialogWatcher = true;
    Settings.AutoMoveMousePointerToTopLeft = false;
    using (IE ie2 = new IE())
    {
        ie2.GoTo(URL);
        ie2.Link(SomeButtonID).Click();
        ie2.Image(AnotherButtonID).FireEvent("onclick");


        // some debugging code wrapped around the next user action
        // which is clicking on the attach file button
        var helper = new DialogHandlerHelper();
        using (new UseDialogOnce(ie2.DialogWatcher, helper))
        {
            Thread.Sleep(1 * 1000); // wait for attach button to be "ready"

            // Click button that triggers the dialog that states:
            //     "file browsing dialog has been blocked"
            //     "please click here and try again"
            //
            ie2.Button(FileAttachButtonID).FireEvent("onclick"); 
        }
        foreach(string dialogHandler in helper.CandidateDialogHandlers)
        {
            // nothing prints out here :(
            Console.Out.WriteLine(dialogHandler);
        }


        // debug print out all elements with tagname = object
        foreach (Element objectElement in ie2.ElementsWithTag("object"))
        {
            StringBuilder elementInfo = new StringBuilder();
            elementInfo.AppendLine("--------------------------------------------");
            elementInfo.AppendLine("element.tagname = " + objectElement.TagName);
            elementInfo.AppendLine("element.style = " + objectElement.Style);
            elementInfo.AppendLine("element.type = " + objectElement.GetAttributeValue("type"));
            elementInfo.AppendLine("element.data = " + objectElement.GetAttributeValue("data"));
            elementInfo.AppendLine("--------------------------------------------");
            Console.Out.WriteLine(elementInfo.ToString());

            // none of these clicks make the dialog go away
            objectElement.ClickNoWait();
            objectElement.Click();
            objectElement.DoubleClick();
            objectElement.MouseEnter();
            objectElement.MouseDown();
            Thread.Sleep(500);
            objectElement.MouseUp();
        }

        // wait to see if dialog disappears after click
        Thread.Sleep(300 * 1000);
    }
}

非常感谢任何和所有帮助。

谢谢!

My Background:
I am new to WatiN, but not new to writing automated Web UI tests. At my new job, we are trying to use WatiN for our Web UI tests (thanks to a few CUIT fails).

I've solved this problem in the past using ArtOfTest.WebAii, by using a Win32 mouse click with a magic number offset from the containing element, but I can't seem to find documentation on how to do that in WatiN and I can't figure it out myself :\

My problem:
This dialog appears and I can't seem to find a way for WatiN to click it.

enter image description here

The dialog has the following markup:

<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>  
    <param name="source" value="/CuteWebUI_Uploader_Resource.axd?type=file&file=silverlight.xap&_ver=634334311861475176"/>
    <param name="windowless" value="true" object="" <=""/>

my test code:

[TestMethod]
public void SomeTest()
{
    Settings.MakeNewIeInstanceVisible = true;
    Settings.AutoStartDialogWatcher = true;
    Settings.AutoMoveMousePointerToTopLeft = false;
    using (IE ie2 = new IE())
    {
        ie2.GoTo(URL);
        ie2.Link(SomeButtonID).Click();
        ie2.Image(AnotherButtonID).FireEvent("onclick");


        // some debugging code wrapped around the next user action
        // which is clicking on the attach file button
        var helper = new DialogHandlerHelper();
        using (new UseDialogOnce(ie2.DialogWatcher, helper))
        {
            Thread.Sleep(1 * 1000); // wait for attach button to be "ready"

            // Click button that triggers the dialog that states:
            //     "file browsing dialog has been blocked"
            //     "please click here and try again"
            //
            ie2.Button(FileAttachButtonID).FireEvent("onclick"); 
        }
        foreach(string dialogHandler in helper.CandidateDialogHandlers)
        {
            // nothing prints out here :(
            Console.Out.WriteLine(dialogHandler);
        }


        // debug print out all elements with tagname = object
        foreach (Element objectElement in ie2.ElementsWithTag("object"))
        {
            StringBuilder elementInfo = new StringBuilder();
            elementInfo.AppendLine("--------------------------------------------");
            elementInfo.AppendLine("element.tagname = " + objectElement.TagName);
            elementInfo.AppendLine("element.style = " + objectElement.Style);
            elementInfo.AppendLine("element.type = " + objectElement.GetAttributeValue("type"));
            elementInfo.AppendLine("element.data = " + objectElement.GetAttributeValue("data"));
            elementInfo.AppendLine("--------------------------------------------");
            Console.Out.WriteLine(elementInfo.ToString());

            // none of these clicks make the dialog go away
            objectElement.ClickNoWait();
            objectElement.Click();
            objectElement.DoubleClick();
            objectElement.MouseEnter();
            objectElement.MouseDown();
            Thread.Sleep(500);
            objectElement.MouseUp();
        }

        // wait to see if dialog disappears after click
        Thread.Sleep(300 * 1000);
    }
}

Any and all help will be very much appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素罗衫 2024-10-25 01:14:49

您的控件是一个 silverlight 组件,无法使用 WatiN 实现自动化。幸运的是,您可以结合使用 WatiN 和 White 来完成工作。

以下代码由 Leo Bartnik 创建并发布,因此所有功劳都归他所有!请在此处查看他的博客文章。注释中的代码如下:

他使用了以下版本。

  • 2011-02-08 的 watin-2.0.50.1179.zip http://sourceforge.net/projects/watin/files/WatiN%202.x/2.0%20Final/

  • white 0.20 二进制文件 http://white. codeplex.com/releases/view/29694

    公共无效WatiN_and_White_join_forces()
    {
    // 使用 WatiN 导航到您的网页
    字符串 url = "http://localhost[端口#]/WatinWhiteTestLandingPage.aspx";
    WatiN.Core.IE watin = new WatiN.Core.IE(url);
    watin.Link(Find.ByText("点击此处)).Click();

    //将WatiN使用的IE实例附加到White上
    InternetExplorerFactory.Plugin();
    string title = "[这里是浏览器标题]"; // 类似于“WatinWhiteHybrid - Internet Explorer 由...提供”
    var ie = (InternetExplorerWindow)Application.Attach(watin.ProcessID).GetWindow(title);
    White.WebBrowser.Silverlight.SilverlightDocument sl = ie.SilverlightDocument;
    
    // 使用White点击silverlight控件中的按钮
    sl.Get(SearchCriteria.ByAutomationId("ClickMeId")).Click();
    

    }

顺便说一句,我不知道为什么这段代码的格式如此偏离......

Your control is a silverlight component which can't be automated with WatiN. Fortunately this you can combine WatiN and White to get the job done.

Following code is created and published by Leo Bartnik so ALL the credits go to him! Have a look at his blog post here. The following code in the comments:

He used the following versions.

  • watin-2.0.50.1179.zip from 2011-02-08 http://sourceforge.net/projects/watin/files/WatiN%202.x/2.0%20Final/

  • white 0.20 Binaries http://white.codeplex.com/releases/view/29694

    public void WatiN_and_White_join_forces()
    {
    // Navigate to your webpage with WatiN
    string url = "http://localhost[port#]/WatinWhiteTestLandingPage.aspx";
    WatiN.Core.IE watin = new WatiN.Core.IE(url);
    watin.Link(Find.ByText("click here)).Click();

    // Attach the IE instance used by WatiN to White
    InternetExplorerFactory.Plugin();
    string title = "[browser title here]"; // will be something like "WatinWhiteHybrid - Internet Explorer provided by ..."
    var ie = (InternetExplorerWindow)Application.Attach(watin.ProcessID).GetWindow(title);
    White.WebBrowser.Silverlight.SilverlightDocument sl = ie.SilverlightDocument;
    
    // Click the button in the silverlight control using White
    sl.Get(SearchCriteria.ByAutomationId("ClickMeId")).Click();
    

    }

btw don't know why the formating of this code is so way off....

海风掠过北极光 2024-10-25 01:14:49

所以这是我的黑客解决方案:
使用 Microsoft 的编码 UI 测试单击 Silverlight 对话框。然而,CUIT 不如 WatiN,所以我在 WatiN 中运行测试并加载 CUIT,神奇的是,单击一下。

此外,我无法使用 CUIT 轻松找到 Silverlight 对象,因此我找到了它后面的窗口,找到窗口的中心像素并强制执行 Microsoft.VisualStudio.TestTools.UITesting.Mouse.Click()。是的,黑客中的黑客,我是一个非常坏的人,但我没时间了,只需要一些工作。

如果有人有更优雅的解决方案,请分享。

我的解决方案代码:

FileUploadDialogHandler helper = new FileUploadDialogHandler(attachmentPath);
            using (new UseDialogOnce(ie.DialogWatcher, helper))
            {
                Thread.Sleep(1 * 1000); // wait for attach button to be "ready"
                ie.Button(browseButtonID).FireEvent("onclick");


                // When automating a file upload, there is a Silverlight popup in IE that forces an extra click
                // before opening the file open dialog.  WatiN does not support Silverlight automation
                // and the popup element was acting quirky in Microsoft's Coded UI Test, so we find the 
                // dialog box UNDERNEATH the Silverlight popup and force one, lovely, mouse click.
                //===== Entering Coded UI Test land, beware! =====================================

                // initialize Coded UI Test
                Playback.Initialize();
                BrowserWindow.CurrentBrowser = "IE";
                Process watinBrowserProcess = Process.GetProcessById(ie.ProcessID);
                BrowserWindow cuitBrowser = BrowserWindow.FromProcess(watinBrowserProcess); // attach Coded UI Test to the IE browser WatiN initialized

                // get the window underneath the Silverlight popup
                UITestControl modalUnderSilverlightPopup = new UITestControl(cuitBrowser.CurrentDocumentWindow);
                modalUnderSilverlightPopup.SearchProperties.Add("id", windowElementUnderPopupID);

                // get the X and Y pixel center of the window
                int centerX = modalUnderSilverlightPopup.BoundingRectangle.X + modalUnderSilverlightPopup.BoundingRectangle.Width / 2;
                int centerY = modalUnderSilverlightPopup.BoundingRectangle.Y + modalUnderSilverlightPopup.BoundingRectangle.Height / 2;

                // Click!
                Mouse.Click(new Point(centerX, centerY));

                // Shutdown Coded UI Test
                Playback.Cleanup();
                //===== End Coded UI Test land, you survived! yay! ============================
        }

So this was my hack solution:
Use Microsoft's Coded UI Test to click on the Silverlight dialog. However, CUIT is inferior to WatiN, so I run my test in WatiN and load CUIT for one, magical, click.

Additionally, I was not able to easily find the Silverlight object using CUIT, so I find the window behind it, find the window's center pixel and force a Microsoft.VisualStudio.TestTools.UITesting.Mouse.Click(). Yes, a hack inside of a hack, I am a very bad person, but I ran out of time and just needed something working.

If anyone has a more elegant solution, please share.

My solution code:

FileUploadDialogHandler helper = new FileUploadDialogHandler(attachmentPath);
            using (new UseDialogOnce(ie.DialogWatcher, helper))
            {
                Thread.Sleep(1 * 1000); // wait for attach button to be "ready"
                ie.Button(browseButtonID).FireEvent("onclick");


                // When automating a file upload, there is a Silverlight popup in IE that forces an extra click
                // before opening the file open dialog.  WatiN does not support Silverlight automation
                // and the popup element was acting quirky in Microsoft's Coded UI Test, so we find the 
                // dialog box UNDERNEATH the Silverlight popup and force one, lovely, mouse click.
                //===== Entering Coded UI Test land, beware! =====================================

                // initialize Coded UI Test
                Playback.Initialize();
                BrowserWindow.CurrentBrowser = "IE";
                Process watinBrowserProcess = Process.GetProcessById(ie.ProcessID);
                BrowserWindow cuitBrowser = BrowserWindow.FromProcess(watinBrowserProcess); // attach Coded UI Test to the IE browser WatiN initialized

                // get the window underneath the Silverlight popup
                UITestControl modalUnderSilverlightPopup = new UITestControl(cuitBrowser.CurrentDocumentWindow);
                modalUnderSilverlightPopup.SearchProperties.Add("id", windowElementUnderPopupID);

                // get the X and Y pixel center of the window
                int centerX = modalUnderSilverlightPopup.BoundingRectangle.X + modalUnderSilverlightPopup.BoundingRectangle.Width / 2;
                int centerY = modalUnderSilverlightPopup.BoundingRectangle.Y + modalUnderSilverlightPopup.BoundingRectangle.Height / 2;

                // Click!
                Mouse.Click(new Point(centerX, centerY));

                // Shutdown Coded UI Test
                Playback.Cleanup();
                //===== End Coded UI Test land, you survived! yay! ============================
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文