2008-09-27 26 views
4

我在推出如下一个WPF应用程序的简单消息框:如何使用白色访问MessageBox?

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    MessageBox.Show("Howdy", "Howdy"); 
} 

我能得到white点击我的按钮并启动消息框。

UISpy显示它作为我窗口的孩子我无法制定出访问它的方法。

如何访问我的MessageBox以验证其内容?

回答

3

找到了!窗口类有一个MessageBox方法,它可以实现:

 var app = Application.Launch(@"c:\ApplicationPath.exe"); 
     var window = app.GetWindow("Window1"); 
     var helloButton = window.Get<Button>("Hello"); 
     Assert.IsNotNull(helloButton); 
     helloButton.Click(); 
     var messageBox = window.MessageBox("Howdy"); 
     Assert.IsNotNull(messageBox); 
1

如何在消息框中获取消息?无论如何,我似乎无法在白色中找到这个。我认为这个消息是正确的,这是非常重要的。

1

包含在白色源代码中的是一些UI测试项目(测试白色本身)。

其中一项测试包括MessageBox测试,其中包括一种获取显示消息的方法。

[TestFixture, WinFormCategory, WPFCategory] 
public class MessageBoxTest : ControlsActionTest 
{ 
    [Test] 
    public void CloseMessageBoxTest() 
    { 
     window.Get<Button>("buttonLaunchesMessageBox").Click(); 
     Window messageBox = window.MessageBox("Close Me"); 
     var label = window.Get<Label>("65535"); 
     Assert.AreEqual("Close Me", label.Text); 
     messageBox.Close(); 
    } 

    [Test] 
    public void ClickButtonOnMessageBox() 
    { 
     window.Get<Button>("buttonLaunchesMessageBox").Click(); 
     Window messageBox = window.MessageBox("Close Me"); 
     messageBox.Get<Button>(SearchCriteria.ByText("OK")).Click(); 
    } 
} 

显然,用于显示所述文本消息的标签通过显示在MessageBox窗口所拥有,并且其主要识别是在最大字值(65535)。

3

请试试这个

 Window messageBox = window.MessageBox(""); 
     var label = messageBox.Get<Label>(SearchCriteria.Indexed(0)); 
     Assert.AreEqual("Hello",label.Text); 
1

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); 
    } 
}