声明:下列代码转自: http://stackoverflow.com/questions/3759569/watin-core-ie-component-giving-timeout-while-internet-explorer-busy-error-while
可以帮忙解决用Watin进行Web测试时IE对象可能出现的问题:
publicclassStaticIE:IDisposable
{
private IE _ie;
privateint _ieThread;
privatestring _ieHwnd;
privateint _processId;
publicStaticIE(IE aBrowser)
{
SetBrowser(aBrowser);
}
public IE Browser
{
get
{
var currentThreadId =GetCurrentThreadId();
_ie =Browser.AttachTo<IE>(Find.By("hwnd", _ieHwnd),1);
if(currentThreadId != _ieThread)
{
_ieThread = currentThreadId;
}
return _ie;
}
}
privatevoidSetBrowser(IE aBrowser)
{
_ie = aBrowser;
_ieHwnd = _ie.hWnd.ToString();
_ieThread =GetCurrentThreadId();
_processId = _ie.ProcessID;
}
privateintGetCurrentThreadId()
{
returnThread.CurrentThread.GetHashCode();
}
publicvoidDispose()
{
if(_ie !=null)
{
Process.GetProcessById(_processId).Kill();
_ie =null;
}
}
}