单击“提交”按钮时如何验证是否关闭了WatiN新浏览器窗口

时间:2015-03-09 09:45:02

标签: popup watin

我有一个场景,我点击主浏览器窗口上的链接,然后打开一个新的弹出窗口(浏览器窗口没有提醒或确认)。

执行某些操作后,我单击“提交”按钮,期望新的弹出/浏览器窗口关闭。效果将显示在主窗口中。

现在,我想验证新的弹出窗口是否已关闭?怎么做?

var popup = br.AttachTo(Find.ByTitle("New Window");
popup.TextField("name").Value = "Part1*";
popup.Button("Search").Click();
popup.CheckBox("Part1").Checked = true;
popup.Button(Find.ByValue("Submit")).Click();
popup.WaitUntilClosed(30); // this is what I want to implement or
VerifyWindowClosed(popup, 30); // Wait for 30 secs with interval of 500 ms till popup is closed. Throw exception at timeout of 30 sec

感谢任何好的建议。

1 个答案:

答案 0 :(得分:0)

要回答这个问题并展示示例,我们需要2个html文件。 文件1 - 1.html

<html>
<head>
</head>
<body>
<!-- Text link tag - by www.rapidtables.com -->
<a href="2.html"  target="_blank">Print</a>
</body>
</html>

文件2 - 2.html

<head>
<title>Popup</title>
<script language="javascript" type="text/javascript"> 
function windowClose() { 
window.open('','_parent',''); 
window.close();
} 
</script>
</head>
<body>
<input type="button" value="Close this window" onclick="windowClose();">
</body>

这是WatIn解决方案:

 WatiN.Core.Settings.WaitUntilExistsTimeOut = 5;
        IE ieBrowser = new IE(@"c:\Page\1.html");
        var instances = new IECollection(true);
        Debug.WriteLine("Instances : " + instances.Count);
        var printElement = ieBrowser.Element(Find.By("target", "_blank"));
        if (printElement != null)
        {
            printElement.Click();
        }
        instances = new IECollection(true);
        Debug.WriteLine("Instances : " + instances.Count);
        IE poppedUpBrowser = IE.AttachTo<IE>(Find.ByUrl(url => url.Contains(@"C:\Page\2.html")));

        if (poppedUpBrowser != null)
        {
            Debug.WriteLine("Second window exists..");
        }

        poppedUpBrowser.WaitForComplete();
        Debug.WriteLine("Waiting...");
        var button = poppedUpBrowser.Element(Find.By("type", "button"));

        if (button != null)
        {
            Debug.WriteLine("Button not null");

            button.ClickNoWait();
        }
        instances = new IECollection(true);
        Debug.WriteLine("Instances : " + instances.Count);

在调试窗口中,您可以看到更改的实例数:

Instances : 1
Instances : 2
Second window exists..
Waiting...
Button not null
Instances : 1

因此,您可以确定窗口已关闭,因为实例数已更改。我希望这会有所帮助。

对于FireFox案例:

foreach (Process p in Process.GetProcesses("."))
        {
            try
            {
                if (p.MainWindowTitle.Length > 0)
                {
                    if (p.MainWindowTitle.Contains("fox"))
                    {
                        Debug.WriteLine("-------------------------");
                        Debug.WriteLine("\r\n Window Title:" + p.MainWindowTitle.ToString());
                        Debug.WriteLine("-------------------------");
                    }
                }
            }
            catch { }
}

您将在Debug中看到(然后您可以计算,比较,制作列表等)

-------------------------

Window Title:Mozilla Firefox
-------------------------

Window Title:Popup - Mozilla Firefox