如何在c#中验证messagebox弹出窗口?
我正在编写一些测试,并尝试验证是否出现了一些系统messagebox。比如说。但是,MessageBox类用于创建MessageBox。我应该如何捕获和验证系统生成的系统并对其进行操作 这些行动是:如何在c#中验证messagebox弹出窗口?,c#,automated-tests,messagebox,C#,Automated Tests,Messagebox,我正在编写一些测试,并尝试验证是否出现了一些系统messagebox。比如说。但是,MessageBox类用于创建MessageBox。我应该如何捕获和验证系统生成的系统并对其进行操作 这些行动是: 1.click on some execute file. 2.validate a warning messagebox pop up 3.click on yes/no on the messagebox 有什么提示吗?一个选择是使用自动化框架 例如: windowm
1.click on some execute file.
2.validate a warning messagebox pop up
3.click on yes/no on the messagebox
有什么提示吗?一个选择是使用自动化框架
例如:
windowmessagebox=WindowFactory.Desktop
.DesktopWindows()
.Find(w=>w.Title.Contains(“MessageBoxTitle”);
按钮ok=messageBox.Get(SearchCriteria.ByText(“ok”);
好的,点击();
白色框架+1
您可以检查我发布的用于断言消息框的答案,并使用messageBox.Get()方法单击“确定”按钮
参考:
MessageBox()是一个很好的解决方案
但是,如果messagebox不出现,这种方法将停留很长时间。有时我想勾选messagebox的“非外观”(警告、错误等)。因此,我编写了一个通过线程设置超时的方法
[TestMethod]
public void TestMethod()
{
// arrange
var app = Application.Launch(@"c:\ApplicationPath.exe");
var targetWindow = app.GetWindow("Window1");
Button button = targetWindow.Get<Button>("Button");
// act
button.Click();
var actual = GetMessageBox(targetWindow, "Application Error", 1000L);
// assert
Assert.IsNotNull(actual); // I want to see the messagebox appears.
// Assert.IsNull(actual); // I don't want to see the messagebox apears.
}
private void GetMessageBox(Window targetWindow, string title, long timeOutInMillisecond)
{
Window window = null ;
Thread t = new Thread(delegate()
{
window = targetWindow.MessageBox(title);
});
t.Start();
long l = CurrentTimeMillis();
while (CurrentTimeMillis() - l <= timeOutInMillsecond) { }
if (window == null)
t.Abort();
return window;
}
public static class DateTimeUtil
{
private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long currentTimeMillis()
{
return (long)((DateTime.UtcNow - Jan1st1970).TotalMilliseconds);
}
}
[TestMethod]
公共void TestMethod()
{
//安排
var app=Application.Launch(@“c:\ApplicationPath.exe”);
var targetWindow=app.GetWindow(“Window1”);
Button Button=targetWindow.Get(“按钮”);
//表演
按钮。单击();
var实际值=GetMessageBox(targetWindow,“应用程序错误”,1000L);
//断言
Assert.IsNotNull(实际);//我想看到messagebox出现。
//Assert.IsNull(实际);//我不想看到messagebox。
}
私有void GetMessageBox(窗口targetWindow,字符串标题,长时间输出毫秒)
{
Window=null;
线程t=新线程(委托()
{
窗口=targetWindow.MessageBox(标题);
});
t、 Start();
长l=CurrentTimeMillis();
虽然(CurrentTimeMillis()-l也许您可以使用AutoIt,但请看一看,这会有点昂贵。但是谢谢您的建议
[TestMethod]
public void TestMethod()
{
// arrange
var app = Application.Launch(@"c:\ApplicationPath.exe");
var targetWindow = app.GetWindow("Window1");
Button button = targetWindow.Get<Button>("Button");
// act
button.Click();
var actual = GetMessageBox(targetWindow, "Application Error", 1000L);
// assert
Assert.IsNotNull(actual); // I want to see the messagebox appears.
// Assert.IsNull(actual); // I don't want to see the messagebox apears.
}
private void GetMessageBox(Window targetWindow, string title, long timeOutInMillisecond)
{
Window window = null ;
Thread t = new Thread(delegate()
{
window = targetWindow.MessageBox(title);
});
t.Start();
long l = CurrentTimeMillis();
while (CurrentTimeMillis() - l <= timeOutInMillsecond) { }
if (window == null)
t.Abort();
return window;
}
public static class DateTimeUtil
{
private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long currentTimeMillis()
{
return (long)((DateTime.UtcNow - Jan1st1970).TotalMilliseconds);
}
}