4

以下のように起動される WPF アプリケーションに単純なメッセージ ボックスがあります。

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

を取得してボタンをクリックし、メッセージ ボックスを起動できます。

UISpy はそれをウィンドウの子として表示します。アクセスする方法がわかりませんでした。

MessageBox にアクセスして内容を確認するにはどうすればよいですか?

4

4 に答える 4

3

これを試してください

       Window messageBox = window.MessageBox("");
       var label = messageBox.Get<Label>(SearchCriteria.Indexed(0));
       Assert.AreEqual("Hello",label.Text);
于 2012-12-02T15:36:55.547 に答える
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);
于 2008-09-27T15:04:37.833 に答える
1

window.MessageBox() は良い解決策です!!

しかし、メッセージボックスが表示されない場合、このメソッドは長時間スタックします。メッセージボックスの「 Not Appearance」(警告、エラーなど)を確認したい場合があります。そこで、スレッド化して 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);
    }
}
于 2016-02-05T08:14:32.367 に答える
1

White のソース コードには、(White 自体をテストするための) いくつかの UI テスト プロジェクトが含まれています。

テストの 1 つには、表示されたメッセージを取得する方法を含む 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) です。

于 2012-08-06T10:37:46.377 に答える