WatiN 和 .net winforms WebBrowser 控件 - DialogWatcher 可能吗? [英] WatiN and .net winforms WebBrowser control - is DialogWatcher possible?

查看:20
本文介绍了WatiN 和 .net winforms WebBrowser 控件 - DialogWatcher 可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的目标是:嵌入在 .net winform 中的支持 Watin 的浏览器测试.

Our target is: Watin-enabled browser testing embedded in a .net winform.

目前,我们使用 .net WebBrowser 控件将浏览器行为嵌入到 winform 中.我们使用这样的代码将 WatiN 附加到表单上的 WebBroswer 控件(感谢 prostynick):

Currently, we are using a .net WebBrowser control to embed browser behavior in a winform. We are attaching WatiN to the WebBroswer control on the form with code like this ( thanks prostynick ):

var thread = new Thread(() =>
{
    Settings.AutoStartDialogWatcher = false;
    var ie = new IE(webBrowser1.ActiveXInstance);
    ie.GoTo("http://www.google.com");
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

问题在于——winform 浏览器"需要在测试/自动化过程中处理弹出窗口.

The problem is this - the "winform browser" needs to handle popups during testing/automation.

问题:当 Watin 附加到 winforms webBrowser 控件(而不使用它自己的 WatiN 生成的 IE 窗口)时,如何处理弹出窗口?
a) Watin 的 DialogWatcher 还可以使用吗?如果是这样......如何?
b) 如果没有,那么也许我们可以编写我们自己的 DialogWatcher —— 但我们需要一个 hWnd 或 processID 来添加它.在 Waitin 没有自己的窗口或进程的情况下,我们从哪里获得正确的 hWnd 或 processId?

Question: How can popups be handled when Watin is attached to a winforms webBrowser control ( and not using its own WatiN-generated IE window )?
a) Can Watin's DialogWatcher still be used? If so ...how?
b) If not, then perhaps we can write our own DialogWatcher -- but we would need an hWnd or processID to add it. Where would we get correct hWnd or processId in this scenario where Waitin does not have its own window or process?

提前感谢您提供任何想法......欢迎使用达到相同目标的替代方法!

Thanks in advance for any ideas ...alternate approaches that reach the same target are welcome!

推荐答案

我刚刚升级到最新版本的 WatiN(头部修订 - 1166 - 在主干中:https://watin.svn.sourceforge.net/svnroot/watin/trunk/src/).由于原始 DialogWatcher 类发生了变化,现在可以使用更少的代码使用现有的 DialogWatcher.

I have just upgraded to newest version of WatiN (head revision - 1166 - in trunk: https://watin.svn.sourceforge.net/svnroot/watin/trunk/src/). Since there was a change in original DialogWatcher class it is now possible to use existing DialogWatcher with fewer code.

创建类:

public class WebBrowserIE : IE
{
    private IntPtr hwnd;

    public WebBrowserIE(WebBrowser webBrowserControl)
        : base(webBrowserControl.ActiveXInstance, false)
    {
        hwnd = webBrowserControl.FindForm().Handle;
        StartDialogWatcher();
    }

    public override IntPtr hWnd
    {
        get
        {
            return hwnd;
        }
    }

    protected override void Dispose(bool disposing)
    {
        hwnd = IntPtr.Zero;
        base.Dispose(disposing);
    }
}

使用它代替原来的 IE 类并看到消失的 javascript 警报对话框:

Use it instead of original IE class and see disappearing javascript alert dialog:

var ie = new WebBrowserIE(webBrowser1);
var thread = new Thread(() =>
{
    ie.GoTo("http://www.plus2net.com/javascript_tutorial/window-alert.php");
    ie.Button(Find.ByValue("Click here to display alert message")).Click();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

警告:在线程外创建 WebBrowserIE 实例.否则在读取FormHandle属性时,你将不得不修改这个类以避免跨线程操作.

Warning: Create WebBrowserIE instance outside the thread. Otherwise you will have to modify this class to avoid cross-thread operation when reading Handle property of Form.

这篇关于WatiN 和 .net winforms WebBrowser 控件 - DialogWatcher 可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆