问题描述
有没有办法测试链接是否在新的浏览器窗口(或浏览器标签)中打开?
is there a way to test wether a link was opened in a new browser-window (or browser-tab)?
更新:
到目前为止,我使用了以下代码:
So far I used the following code:
var newBrowserWindow = Browser.AttachTo<IE>(Find.ByTitle(browserTitle));
Assert.That(newBrowserWindow.hWnd, Is.Not.EqualTo(existingBrowserWindow.hWnd));
我使用 existingBrowserWindow 打开页面并单击链接的地方.但是当链接在现有浏览器中打开一个新选项卡时(带有 targer=_blank 的 IE 的默认行为)它具有相同的窗口句柄,因为它是相同的浏览器窗口.那么如何检测新标签页?
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?
推荐答案
你的一些代码会有所帮助...,
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
支持 Find.ByUri()
、Find.ByTitle()
和 Find.By("hwnd", windowHandle)
根据文档.我只测试了 Find.ByUri()
和 Find.ByTitle()
方法.
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;
}
}
据我所知,WatiN 不支持新标签.但是 Internet Explorer 的默认行为是在新窗口中打开新链接.
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.
这篇关于使用 WATIN 检测新的浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!