【问题标题】:How to validate a messagebox popup in c#?如何在 C# 中验证消息框弹出窗口?
【发布时间】:2016-02-15 12:02:10
【问题描述】:

我正在编写一些测试并尝试验证某些系统消息框正在弹出。比如http://www.dotnetperls.com/messagebox-show。但是,MessageBox 类用于创建消息框。我应该如何捕获和验证系统生成的并对其进行操作?

例如:动作是:

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

有什么提示吗?

【问题讨论】:

标签: c# automated-tests messagebox


【解决方案1】:

一种选择是使用White自动化框架。

例如:

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

【讨论】:

    【解决方案2】:

    白框+1!!

    您可以查看我发布的断言消息框的答案,并使用 messageBox.Get() 方法单击“确定”按钮。

    参考:https://stackoverflow.com/a/35219222/2902212

    window.MessageBox() 是一个很好的解决方案

    但是如果没有出现消息框,这个方法会卡很久。有时我想检查消息框的“未出现”(警告、错误等)。所以我写了一个方法来通过线程设置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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 2015-05-14
      • 2013-05-03
      • 1970-01-01
      • 2014-06-23
      相关资源
      最近更新 更多