detect new browser window with WATIN

扶醉桌前 提交于 2019-12-11 06:17:58

问题


is there a way to test wether a link was opened in a new browser-window (or browser-tab)?

Update:

So far I used the following code:

var newBrowserWindow = Browser.AttachTo<IE>(Find.ByTitle(browserTitle));
Assert.That(newBrowserWindow.hWnd, Is.Not.EqualTo(existingBrowserWindow.hWnd));

Where I used the existingBrowserWindow to open a page and to click on a link. But when the link opens a new tab in the existing browser (default behaviour for IE with targer=_blank) it has the same window-handle, since it's the same browser window. So how can I detect a new tab?


回答1:


Some code of yours would help...,

Anyway, what I do when a link open a new browser window is

using (var newBrowser = WatiN.Core.Browser.AttachTo<IE>(Find.ByTitle("Analytics - Read conversation"))
{
}

Browser.AttachTo supports Find.ByUri(), Find.ByTitle() and Find.By("hwnd", windowHandle) according to documentation. I only tested Find.ByUri() and Find.ByTitle() methods.

if you want to detect if you action has opened a new window you could do

public bool TryGetNewBrowser(out IE browser, string title)
{
    try
    {
        browser = WatiN.Core.Browser.AttachTo<IE>(Find.ByTitle(title));
        return true;
    }
    catch(WatiN.Core.Exceptions.BrowserNotFoundException)
    {
        browser = null;
        return false;
    }
}

As far as I know, there is no support in WatiN for new tab. But the default behavior of internet explorer is to open new links in new window.



来源:https://stackoverflow.com/questions/4750113/detect-new-browser-window-with-watin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!