Question

J'ai une simple boîte de message dans une application WPF lancée comme suit:

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

Je peux demander à blanc de cliquer sur mon bouton et de lancer la boîte de message.

UISpy le montre comme un enfant de ma fenêtre, je ne pouvais pas trouver la méthode pour y accéder.

Comment puis-je accéder à ma MessageBox pour en vérifier le contenu?

Était-ce utile?

La solution

Je l'ai trouvé! La classe window a une méthode MessageBox qui fait le tour:

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

Autres conseils

Essayez ceci

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

Le code source White contient des projets de tests d’UI (pour tester White lui-même).

L'un des tests inclut des tests MessageBox, qui incluent un moyen d'obtenir le message affiché.

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

De toute évidence, l'étiquette utilisée pour afficher le message texte appartient à la fenêtre affichant la boîte de message et son identification principale est la valeur maximale du mot (65535).

window.MessageBox () est une bonne solution !!

Mais cette méthode resterait bloquée pendant longtemps si la boîte de message n'apparaissait pas . Parfois, je veux vérifier " Pas d'apparence ". d'une boîte de message ( Avertissement, Erreur, etc. ). Alors j’écris une méthode pour définir le timeOut en enfilant.

[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);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top