How do I access a MessageBox with white?

不问归期 提交于 2019-12-07 11:23:55

问题


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?


回答1:


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



回答2:


Please try this

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



回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/143736/how-do-i-access-a-messagebox-with-white

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