gpt4 book ai didi

testing - 将 WatiN 附加到 IE 时出现问题

转载 作者:行者123 更新时间:2023-11-28 20:12:45 27 4
gpt4 key购买 nike

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

我正在尝试在我的类清理代码中关闭 IE,使用 WatiN 的示例 IEStaticInstanceHelper technique .

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

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

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

这给出了错误:

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)

我确定我一定遗漏了一些明显的东西,有人对此有任何想法吗?谢谢

为了完整起见,静态助手看起来像这样:

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;
}

最佳答案

问题是,当 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 中的线程 here但是 [TestMethod] 在 STA 中运行并不明显,而 [ClassCleanup][AssemblyCleanupAttribute] 等方法则不会。

关于testing - 将 WatiN 附加到 IE 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4227461/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com