将 WatiN 附加到 IE 时出现问题

发布于 2024-10-03 21:48:59 字数 1700 浏览 4 评论 0原文

我正在尝试使用 WatiN 进行 UI 测试,我可以让测试正常工作,但之后无法关闭 IE。

我正在尝试使用 WatiN 的示例 IEStaticInstanceHelper 技术

问题似乎是附加到 IE 线程,该线程超时:

_instance = IE.AttachTo<IE>(Find.By("hwnd", _ieHwnd));

(_ieHwnd 是 IE 首次启动时存储的 IE 句柄。)

这给出了错误:

类清理方法 Class1.MyClassCleanup 失败。错误 信息: WatiN.Core.Exceptions.BrowserNotFoundException: 找不到匹配的 IE 窗口 约束:属性“hwnd”等于 ‘1576084’。搜索在“30”后过期 秒..堆栈跟踪:在 WatiN.Core.Native.InternetExplorer.AttachToIeHelper.Find(约束 findBy、Int32 超时、布尔值 等待完成)

我确信我一定错过了一些明显的东西,有人对此有任何想法吗? 感谢

完整性,静态助手看起来像这样:

public class StaticBrowser
{
    private IE _instance;
    private int _ieThread;
    private string _ieHwnd;

    public IE Instance
    {
        get
        {
            var currentThreadId = GetCurrentThreadId();
            if (currentThreadId != _ieThread)
            {
                _instance = IE.AttachTo<IE>(Find.By("hwnd", _ieHwnd));
                _ieThread = currentThreadId;
            }
            return _instance;
        }
        set
        {
            _instance = value;
            _ieHwnd = _instance.hWnd.ToString();
            _ieThread = GetCurrentThreadId();
        }
    }

private int GetCurrentThreadId()
{
    return Thread.CurrentThread.GetHashCode();
}
    }

清理代码看起来像这样:

private static StaticBrowser _staticBrowser;

[ClassCleanup]
public static void MyClassCleanup()
{
    _staticBrowser.Instance.Close();
    _staticBrowser = null;
}

I am experimenting with WatiN for our UI testing, I can get tests to work, but I can't get IE to close afterwards.

I'm trying to close IE in my class clean up code, using WatiN's example IEStaticInstanceHelper technique.

The problem seems to be attaching to the IE thread, which times out:

_instance = IE.AttachTo<IE>(Find.By("hwnd", _ieHwnd));

(_ieHwnd is the handle to IE stored when IE is first launched.)

This gives the error:

Class Cleanup method
Class1.MyClassCleanup failed. Error
Message:
WatiN.Core.Exceptions.BrowserNotFoundException:
Could not find an IE window matching
constraint: Attribute 'hwnd' equals
'1576084'. Search expired after '30'
seconds.. Stack Trace: at
WatiN.Core.Native.InternetExplorer.AttachToIeHelper.Find(Constraint
findBy, Int32 timeout, Boolean
waitForComplete)

I'm sure I must be missing something obvious, has anyone got any ideas about this one?
Thanks

For completeness, the static helper looks like this:

public class StaticBrowser
{
    private IE _instance;
    private int _ieThread;
    private string _ieHwnd;

    public IE Instance
    {
        get
        {
            var currentThreadId = GetCurrentThreadId();
            if (currentThreadId != _ieThread)
            {
                _instance = IE.AttachTo<IE>(Find.By("hwnd", _ieHwnd));
                _ieThread = currentThreadId;
            }
            return _instance;
        }
        set
        {
            _instance = value;
            _ieHwnd = _instance.hWnd.ToString();
            _ieThread = GetCurrentThreadId();
        }
    }

private int GetCurrentThreadId()
{
    return Thread.CurrentThread.GetHashCode();
}
    }

And the clean up code looks like this:

private static StaticBrowser _staticBrowser;

[ClassCleanup]
public static void MyClassCleanup()
{
    _staticBrowser.Instance.Close();
    _staticBrowser = null;
}

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

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

发布评论

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

评论(3

分開簡單 2024-10-10 21:49:01

我自己通过转储 mstest 并使用 mbunit 解决了这个问题。我还发现我也不需要使用任何 IEStaticInstanceHelper 东西,它就可以工作。

Fixed this myself by dumping mstest and using mbunit instead. I also found that I didn't need to use any of the IEStaticInstanceHelper stuff either, it just worked.

|煩躁 2024-10-10 21:49:00

问题是,当 MSTEST 执行带有 [ClassCleanup] 属性的方法时,它将在不属于 STA

如果您运行以下代码,它应该可以工作:

[ClassCleanup]
public static void MyClassCleanup()
{
    var thread = new Thread(() =>
    {
        _staticBrowser.Instance.Close();
        _staticBrowser = null;
     });

    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}

WatiN 网站简要提到 WatiN 无法与不在 STA 中的线程一起工作 此处,但在 [ClassCleanup][AssemblyCleanupAttribute 等方法时,[TestMethod] 在 STA 中运行并不明显] 不。

The problem is that when MSTEST executes the method with the [ClassCleanup] attribute, it will be run on a thread that isn't part of the STA.

If you run the following code it should work:

[ClassCleanup]
public static void MyClassCleanup()
{
    var thread = new Thread(() =>
    {
        _staticBrowser.Instance.Close();
        _staticBrowser = null;
     });

    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}

The WatiN website briefly mentions that WatiN won't work with threads not in the STA here but it isn't obvious that [TestMethod]'s run in the STA while methods like [ClassCleanup] and [AssemblyCleanupAttribute] do not.

云胡 2024-10-10 21:49:00

默认情况下,当 IE 对象被销毁时,它们会自动关闭浏览器。

您的 CleanUp 代码可能会尝试查找已关闭的浏览器,这就是您遇到错误的原因。

By default when IE object are destroyed, they autoclose the browser.

Your CleanUp code may try to find a browser already close, which why you have an error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文