如何访问白色的消息框?

发布于 2024-07-05 13:11:40 字数 370 浏览 14 评论 0原文

我在 WPF 应用程序中有一个简单的消息框,启动如下:

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

我可以获得 white< /a> 单击我的按钮并启动消息框。

UISpy 将其显示为我的窗口的子窗口,我无法找出访问它的方法。

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

I have a simple message box in a WPF application that is launched as below:

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

I can get white to click my button and launch the message box.

UISpy shows it as a child of my window I couldn't work out the method to access it.

How do I get access to my MessageBox to verify its contents?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌上芳菲 2024-07-12 13:11:42

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

window.MessageBox() is a good solution!!

But this method would stuck for a long time if the messagebox doesn't appear. Sometimes I want to check "Not Appearance" of a messagebox (Warning, Error, etc.). So I write a method to set the timeOut by threading.

[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);
    }
}
深居我梦 2024-07-12 13:11:41

White 源代码中包含一些 UI 测试项目(用于测试 White 本身)。

其中一个测试包括 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();
    }
}

显然,用于显示文本消息的标签属于显示消息框的窗口,其主要标识是最大字值(65535)。

Contained in the White source code are some UI tests projects (to test White itself).

One of the test includes MessageBox tests, which includes a way to obtain the displayed message.

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

Evidently, the label used to display the text message is owned by the window displaying the messagebox, and its primary identification is the max word value (65535).

成熟稳重的好男人 2024-07-12 13:11:41

找到了! 窗口类有一个 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);

Found it! The window class has a MessageBox method that does the trick:

        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);
硪扪都還晓 2024-07-12 13:11:41

请尝试这个

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

Please try this

       Window messageBox = window.MessageBox("");
       var label = messageBox.Get<Label>(SearchCriteria.Indexed(0));
       Assert.AreEqual("Hello",label.Text);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文