白框+1!!
您可以查看我发布的断言消息框的答案,并使用 messageBox.Get() 方法单击“确定”按钮。
参考:https://stackoverflow.com/a/35219222/2902212
window.MessageBox() 是一个很好的解决方案
但是如果没有出现消息框,这个方法会卡很久。有时我想检查消息框的“未出现”(警告、错误等)。所以我写了一个方法来通过线程设置timeOut。
[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);
}
}