问题
Automation using WatiN is being carried out. Using couple of simultaneous threads to test the application. Few threads are failed and log report/stack trace says the following.
System.InvalidCastException: Specified cast is not valid.
at SHDocVw.IWebBrowser2.get_HWND()
at WatiN.Core.Native.InternetExplorer.IEBrowser.get_hWnd()
at WatiN.Core.Browser.get_hWnd()
at WatiN.Core.DomContainer.StartDialogWatcher()
at WatiN.Core.IE.CreateNewIEAndGoToUri(Uri uri, IDialogHandler logonDialogHandler, Boolean createInNewProcess)
at WatiN.Core.IE..ctor(Boolean createInNewProcess)
at Core.BaseWatinTestWrapper.Run()
Following this exception there is another exception being raised i.e.
System.Runtime.InteropServices.COMException Retrieving the COM class factory for component with CLSID {0002DF01-0000-0000-C000-000000000046} failed due to the following error: 80080005.
Please provide some help. Thanks in advance.
回答1:
I was also using couple of threads and was getting the same error. Make your main method [MTAThread]. This fixed the problem for me.
回答2:
If I'm not mistaken, WatiN explicitly is single-threaded [STApartment] - http://watin.org/documentation/sta-apartmentstate/ If you wish to have such test, I believe you need separate processes in OS-level. I can't tell for sure does you approach collide with this requirement, yet I'd suggest single instance for single browser (and it's descendent windows / processess).
Also, there might be another hickup - WatiN connects to IE using browser's internals, but if there's any Windows / User Session related activity along the way, this might bring additional confusion.
来源:https://stackoverflow.com/questions/5584247/system-invalidcastexception-specified-cast-is-not-valid