首页 > 解决方案 > 等待应用程序启动而不使用 Thread.Sleep() 使用 FLAUI

问题描述

我是使用 FLAUI 和自动化测试的新手,想用它来测试我的系统。目前我正在使用 Thread.Sleep() 等待应用程序启动,然后找到登录文本框。有没有比使用 Thread.Sleep() 更有效的方法来做到这一点?

目前我启动应用程序并使用 Thread.sleep(10000) 等到应用程序完全启动并且在单击控件输入密码进入应用程序之前可以找到登录文本框。但是我知道 Thread.Sleep 是告诉系统等待的最糟糕的方法,尤其是在自动化测试中。谁能提供我可以测试的任何其他东西?

标签: c#multithreadingtestingautomated-teststeststack

解决方案


最好使用重试机制并等到主窗口加载并且控件可见。例如,在调用 Application.Launch 后,您最多可以重试 30 秒以找到主窗口,并在其中输入 txtLogin:

        Retry.WhileException(() =>
        {
            using (var automation = new UIA3Automation())
            {
                Window mainWindow = Application.GetMainWindow(automation, TimeSpan.FromSeconds(60));

                Assert.IsNotNull(Mainwindow, "Main window is not found");

                TextBox loginTextBox = mainWindow.FindFirstDescendant(x => x.ByAutomationId("txtLogin")).AsTextBox();

                Assert.IsNotNull(loginTextBox, "txtLogin is not found");
            }

        }, TimeSpan.FromSeconds(30), null, true);

推荐阅读