How do I access a MessageBox with white?

耗尽温柔 提交于 2019-12-05 15:29:05

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

Please try this

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

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).

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);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!