Internet Explorer の起動と接続を処理する WatiN の次のコードを調べていました。
private static IEBrowser CreateIEPartiallyInitializedInNewProcess(Uri uri)
{
var m_Proc = CreateIExploreInNewProcess(uri);
var helper = new AttachToIeHelper();
var action = new TryFuncUntilTimeOut(TimeSpan.FromSeconds(Settings.AttachToBrowserTimeOut))
{
SleepTime = TimeSpan.FromMilliseconds(500)
};
var ie = action.Try(() =>
{
m_Proc.Refresh();
var mainWindowHandle = m_Proc.MainWindowHandle;
// return mainWindowHandle != IntPtr.Zero ? GetIWebBrowser2Directly(mainWindowHandle) : null;
return mainWindowHandle != IntPtr.Zero
? helper.FindIEPartiallyInitialized(new AttributeConstraint("hwnd", mainWindowHandle.ToString()))
: null;
});
if (ie != null) return ie._ieBrowser;
// if (ie != null) return new IEBrowser(ie);
throw new BrowserNotFoundException("IE", "Timeout while waiting to attach to newly created instance of IE.", Settings.AttachToBrowserTimeOut);
}
WatiN が行うことは、Internet Explorer を起動し、それが .MainWindowHandle (Internet Explorer 内のコンテンツを表示する「ウィンドウ」のハンドル) になるまで待機することです。このウィンドウ ハンドルを取得すると、ユーザーのデスクトップで実行中のすべての IWebBrowser2 ウィンドウのリストを取得し、プロセスの .MainWindowHandle を、. IWebBrowser2 コレクション。
このアプローチの最も重大な問題は、IWebBrowser2.HWND プロパティ (.MainWindowHandle と比較する必要がある) が、アクセスしようとすると (少なくともテストを実行しているマシン)。また、そのような操作のオーバーヘッドがあります。
これは、私が Windows プログラミングに精通している可能性がある人への私の質問です。HWND はとにかく一致するので、必要な IWebBrowser2 をすぐに取得するために .MainWindowHandle 値を使用しないのはなぜですか (上記のコメントアウトされたコードを参照)。 ) 次のメソッドを使用します (WatiN 自体が ShellWindow2.cs 内で使用するコードに触発されています)。
private static IWebBrowser2 GetIWebBrowser2Directly(IntPtr embeddedWebBrowserWindowHandle)
{
IHTMLDocument2 document2 = UtilityClass.TryFuncIgnoreException(() => IEUtils.IEDOMFromhWnd(embeddedWebBrowserWindowHandle));
if (document2 == null) return null;
IHTMLWindow2 parentWindow = UtilityClass.TryFuncIgnoreException(() => document2.parentWindow);
if (parentWindow == null) return null;
return UtilityClass.TryFuncIgnoreException(() => ShellWindows2.RetrieveIWebBrowser2FromIHtmlWindw2Instance(parentWindow));
}
(補足として、私の別の投稿で説明されているプロキシ オブジェクトを作成して、ウィンドウ ハンドルをキャッシュし、IWebBrowser2.HWND への要求を回避することもできます)。
これは私にとってはうまくいきます。HWND 間の競合や不一致は見られません。これについて WatiN フォーラムで質問したくなりましたが、明らかな何かが不足している場合に備えて、まず Programmers' Central で質問することにしました。
よろしくお願いします。どんなヒントでも大歓迎です。
乾杯、ドミニク