如何查找已使用WatiN打开的JavaScript警报
我结合使用SpecFlow、MSTests进行单元测试,并使用WatiN驱动浏览器来测试我们的web应用程序如何查找已使用WatiN打开的JavaScript警报,javascript,c#,internet-explorer-8,watin,specflow,Javascript,C#,Internet Explorer 8,Watin,Specflow,我结合使用SpecFlow、MSTests进行单元测试,并使用WatiN驱动浏览器来测试我们的web应用程序 VisualStudio2010 类库样式项目 SpecFlow MS测试 瓦廷 如果用户提交表单时未填写所有必填字段,则会弹出JavaScript警报。我正在用WatiN检测这个弹出窗口。触发警报的SpecFlow步骤与断言弹出窗口存在的SpecFlow步骤不同,因此等待WatiN对话框处理程序不起作用,因为警报已打开 SpecFlow场景示例: Scenario: Form Fi
- VisualStudio2010
- 类库样式项目
- SpecFlow
- MS测试
- 瓦廷
警报。我正在用WatiN检测这个弹出窗口。触发警报的SpecFlow步骤与断言弹出窗口存在的SpecFlow步骤不同,因此等待WatiN对话框处理程序不起作用,因为警报已打开
SpecFlow场景示例:
Scenario: Form Fields are required
# This step spawns the alert dialog
When I click the "Save and Continue" button
# Test fails here because the alert is already open
Then I should see the validation error alert
单击“保存并继续”按钮时的步骤定义
[When(@"I click the ""(.*)"" button")]
public void WhenIClickTheButton(string buttonText)
{
Button button = BitWeb.Browser.Button(Find.ByValue(buttonText).Or(Find.ByText(buttonText)));
Assert.IsTrue(button.Exists, "No button with text '{0}' was found", buttonText);
button.Click();
browser.WaitForComplete();
}
的步骤定义,然后我应该看到。。。警报
:
[Then(@"I should see the validation error alert")]
public void ThenIShouldSeeTheValidationErrorAlert()
{
var alert = new WatiN.Core.DialogHandlers.AlertDialogHandler();
alert.WaitUntilExists();
StringAssert.Contains(alert.Message, "An error has occurred. Check entire", "The validation error alert was not visible.");
}
调用alert.WaitUntilExists()代码>引发异常:
WatiN.Core.Exceptions.WatiNException:对话框在30秒内不可用
当我断言警报对话框可见时,DialogHandler找不到警报,因为它已经打开
如何找到已使用WatiN打开的警报对话框
更新#1:我倾向于使用场景上下文的解决方案。当前的
对象,我只是不知道如何将内容连接在一起,这样浏览器就不会每次单击按钮都等待30秒,只是为了查看是否弹出警报框
更新#2:经过一些调查后,一步点击按钮会导致整个测试框架暂停,直到警报对话框被取消。单击“OK”按钮取消对话框,这允许测试运行者进入下一步,我断言对话框是可见的——是鸡还是蛋场景。调用按钮。单击nowait()
修复了该问题。因为它是一个Win32对话框,所以您可以获得窗口弹出窗口的句柄(您知道窗口的样式和标题,如果您不知道WinSpy++可以找到的样式,只需打开该工具并将靶心指向警报,它将为您提供该窗口的所有详细信息)
从现在起,您将能够将WatiN视为常规对话框(您应该使用传递alertDialog的HandleDialog作为参数,等等。如果不清楚,请告诉我,我将增强响应)。让我为您编写一个更完整的示例:
public partial class Form1 : Form
{
//
// Your class properites/variables
//
AlertDialogHandler dialogHandler;
[DllImport("User32.dll")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
//
// Some methods/functions declarations
//
public void SomeInitMethod()
{
dialogHandler = new AlertDialogHandler()
browse.AddDialogHandler(dialogHandler);
}
public void SampleMethod()
{
IntPtr hwndTmp = (IntPtr)FindWindow("#32770", "Dialog Title");
WatiN.Core.Native.Windows.Window popUpDialog = new Window(hwndTmp);
dialogHandler.HandleDialog(popUpDialog);
//
// The line above will find the OK button for you and click on it,
// from here you continue with the rest of your code.
}
}
希望这一点更清楚。我将@ProgrammerV5的答案标记为真实答案,因为它引导我找到了这段代码。我之所以发布这个答案,是因为它直接与SpecFlow和WatiN有关
我有三个不同的SpecFlow步骤,单击一个按钮,产生了警报
,断言警报框中有我期望的文本,最后一个步骤是单击“确定”
特定流场景示例
Scenario: Doing something really important
When I click the "Save and Continue" button
Then I should see the validation error alert
When I click OK in the validation error alert
Then I continue on with the rest of this Scenario...
首先,我创建了一个快速助手方法来查找alert
对话框:
TestProject/Helpers/StepHelper.cs
public static class StepHelper
{
[DllImport("User32.dll")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
public static WatiN.Core.Native.Windows.Window GetAlertBox()
{
IntPtr hwndTmp = (IntPtr)FindWindow("#32770", "Message from webpage");
return new WatiN.Core.Native.Windows.Window(hwndTmp);
}
}
这允许您在Internet Explorer 8中抓取一个警报
框
单击“保存并继续”按钮时的步骤定义:
[When(@"I click the ""(.*)"" button")]
public void WhenIClickTheButton(string buttonText)
{
Button button = browser.Button(Find.ByValue(buttonText).Or(Find.ByText(buttonText)));
Assert.IsTrue(button.Exists, "No button with text '{0}' was found", buttonText);
button.ClickNoWait();
}
[Then(@"I should see the validation error alert")]
public void ThenIShouldSeeTheValidationErrorAlert()
{
var alert = StepHelper.GetAlertBox();
Assert.IsTrue(alert.Message.Contains("An error has occurred"),
"An alert was found, but contained the wrong message ('{0}')", alert.Message);
}
[When(@"I click OK in the validation error alert")]
public void WhenIClickOKInTheValidationErrorAlert()
{
var alert = StepHelper.GetAlertBox();
Assert.IsTrue(alert.Message.Contains("An error has occurred"),
"An alert was found, but contained the wrong message ('{0}')", alert.Message);
alert.ForceClose();
}
调用按钮。单击()
或按钮。单击nowait()
会生成警报框。只需调用单击()
方法导致WatiN等待该事件完全处理,然后再从方法调用返回。由于警报框已打开并阻止了页面,因此单击事件没有完成,从而导致对click()
的调用完全停止。我通过调用ClickNoWait()
解决了这个问题,这是一个“触发并忘记”的方法单击页面上某个元素的方法。现在,警报框弹出,但测试运行程序可以前进到下一个SpecFlow步骤
为定义步骤,然后我会看到验证错误警报:
[When(@"I click the ""(.*)"" button")]
public void WhenIClickTheButton(string buttonText)
{
Button button = browser.Button(Find.ByValue(buttonText).Or(Find.ByText(buttonText)));
Assert.IsTrue(button.Exists, "No button with text '{0}' was found", buttonText);
button.ClickNoWait();
}
[Then(@"I should see the validation error alert")]
public void ThenIShouldSeeTheValidationErrorAlert()
{
var alert = StepHelper.GetAlertBox();
Assert.IsTrue(alert.Message.Contains("An error has occurred"),
"An alert was found, but contained the wrong message ('{0}')", alert.Message);
}
[When(@"I click OK in the validation error alert")]
public void WhenIClickOKInTheValidationErrorAlert()
{
var alert = StepHelper.GetAlertBox();
Assert.IsTrue(alert.Message.Contains("An error has occurred"),
"An alert was found, but contained the wrong message ('{0}')", alert.Message);
alert.ForceClose();
}
在这里,我使用前面创建的StepHelper.GetAlertBox()
方法获取表示警报框的WatiN.Core.Native.Windows.Window
对象的句柄。然后它是一个简单的Assert.IsTrue
确保警报中显示的消息实际上是我们的“验证失败”消息(本页上还有其他警报)
最后,我们需要关闭警报框
在验证错误警报中单击“确定”时的步骤定义:
[When(@"I click the ""(.*)"" button")]
public void WhenIClickTheButton(string buttonText)
{
Button button = browser.Button(Find.ByValue(buttonText).Or(Find.ByText(buttonText)));
Assert.IsTrue(button.Exists, "No button with text '{0}' was found", buttonText);
button.ClickNoWait();
}
[Then(@"I should see the validation error alert")]
public void ThenIShouldSeeTheValidationErrorAlert()
{
var alert = StepHelper.GetAlertBox();
Assert.IsTrue(alert.Message.Contains("An error has occurred"),
"An alert was found, but contained the wrong message ('{0}')", alert.Message);
}
[When(@"I click OK in the validation error alert")]
public void WhenIClickOKInTheValidationErrorAlert()
{
var alert = StepHelper.GetAlertBox();
Assert.IsTrue(alert.Message.Contains("An error has occurred"),
"An alert was found, but contained the wrong message ('{0}')", alert.Message);
alert.ForceClose();
}
在这里,我们只需从帮助程序中获取警报框对象,并调用ForceClose
来关闭警报框,因为单击右上角的“OK”或“X”按钮基本上做了相同的事情。该对话框是html对话框(jquery类型)还是实际的弹出对话框(win32)?它是警报(“消息”)
对话框。什么是FindWindow
?该方法声明在哪里?还有,窗口
类存在于哪个命名空间/程序集中?[dlliport(“User32.dll”)]公共静态外部程序Int32 FindWindow(字符串lpClassName,字符串lpWindowName);Window是WatiN.Core.Native.windows的一部分。我没有一个类可以使用FindWindow
方法。这是一个类上的静态方法还是一个实例方法,如果是的话,它是哪个类的成员?好的。这更有意义。我在谷歌上搜索了一点,也找到了这个方法。我还在四处玩,想得到帮助警报框上的le。这主要是因为我的头撞到了WatiN和SpecFlow的组合上。感谢您的帮助,@ProgrammerV5.+1并将此标记为答案。我将添加一个我自己的答案(未标记为答案),给出关于SpecFlow的实际C#代码,以供将来参考。