문제

I was writing some tests and try to validate that some system messagebox is popping up. Like in http://www.dotnetperls.com/messagebox-show. However, the class MessageBox is for creating the messagebox. How shall I capture and validate an system generated one and operate on it?

eg: The actions are:

    1.click on some execute file.
    2.validate a warning messagebox pop up
    3.click on yes/no on the messagebox

Any hint please?

도움이 되었습니까?

해결책

One choice is to use White automation framework.

For example:

Window messageBox = WindowFactory.Desktop
                                 .DesktopWindows()
                                 .Find(w => w.Title.Contains("MessageBoxTitle"));
Button ok = messageBox.Get<Button>(SearchCriteria.ByText("OK"));
ok.Click();

다른 팁

White Framework +1 !!

You can check my answer I posted for asserting a message box and use messageBox.Get() method to click Ok button.

Ref: https://stackoverflow.com/a/35219222/2902212

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);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top