1

我正在为我的应用程序进行 FlaUI 测试,但我遇到了应用程序启动问题。基类中的步骤看起来像应用程序启动,等待打开,最大化窗口,等待控件改变状态,然后它应该去测试执行。但取而代之的是,应用程序再启动 2 次,然后正确开始测试执行。我无法定位问题所在。您有任何想法或有类似的问题并知道如何解决吗?这是我的基类代码:

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using UI.Tests.Utilities;
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Conditions;
using FlaUI.Core.Definitions;
using FlaUI.Core.Tools;
using FlaUI.UIA3;
using Application = FlaUI.Core.Application;

namespace UI.Tests
{
    public class UiTestsFixture: IDisposable
    {
        public Application Application;
        public AutomationElement Desktop { get; }
        public ConditionFactory ConditionFactory { get; }
        public const string ApplicationName = "App.Shell";

        public UiTestsFixture()
        {
            ConditionFactory = new ConditionFactory(new UIA3PropertyLibrary());
            UIA3Automation automation = new UIA3Automation();

                var configuration = "Debug";
                var mainWindowTitle = "App (DEBUG 32-bit)";
                var loadingWindowTitle = "Opening - App";

                var applicationExecutablePath = GetApplicationExecutablePath(configuration);
                GetApplication(applicationExecutablePath);
                Desktop = automation.GetDesktop();
                GetApplicationWindow(loadingWindowTitle);
                WaitForMainPageToAppear(mainWindowTitle);
                WaitForOOServiceAndMaximizeWindow();
        }

        private string GetApplicationExecutablePath(string configuration)
        {
            var solutionDirectory = Directory.GetParent(Directory.GetCurrentDirectory())
                ?.Parent
                ?.Parent
                ?.Parent
                ?.FullName;

            var applicationDirectory = Path.Combine(solutionDirectory, $@"App.Shell\bin\x86\{configuration}");
            var applicationPath = Path.Combine(applicationDirectory, $"{ApplicationName}.exe");
            return applicationPath;
        }

        private void GetApplication(string applicationExecutablePath)
        {
            PreventOpenMoreThenOneWindowApplication();
            Application = Application.Launch(applicationExecutablePath);

            if (Application is null)
            {
                throw new ApplicationException("application can't be null!");
            }
        }

        private void PreventOpenMoreThenOneWindowApplication()
        {
            var processes = Process.GetProcessesByName(ApplicationName);

            foreach (var process in processes)
            {
                process.Kill();
            }
        }

        private void GetApplicationWindow(string loadingWindowTitle)
        {
            if (Desktop is null)
            {
                throw new ArgumentNullException(nameof(Desktop), "Window can't be null!");
            }
        }
        private void WaitForMainPageToAppear(string mainWindowTitle)
        {
            Retry.Find(() => Desktop.FindFirstDescendant(ConditionFactory.ByClassName("ServiceStatusTopMenuItemControl")),
                new RetrySettings
                {
                    Timeout = TimeSpan.FromSeconds(30),
                    Interval = TimeSpan.FromSeconds(5)
                });
        }

        private void WaitForOOServiceAndMaximizeWindow()
        {
            Desktop.FindFirstDescendant(ConditionFactory.ByAutomationId("PART_Max")).AsButton().Click();
            while (!Desktop.FindFirstDescendant(ConditionFactory.ByClassName("StateIndicatorControl")).HelpText.Contains("Service is running"))
            {
                Thread.Sleep(2000);
            }
            Desktop.FindFirstDescendant(ConditionFactory.ByAutomationId("NotificationCloseBtn")).AsButton().Click();
        }

        public void Dispose()
        {
            Application.Dispose();
        }
    }
}
4

1 回答 1

0

只是为了检查和丢弃选项:在第一行添加 UiTestsFixture() 构造函数:

System.Diagnostics.StackTrace t = new System.Diagnostics.StackTrace();
Console.WriteLine("Constructor" + t.ToString());

在最后 :

Console.WriteLine("Constructor end");

还要检查通过 cmd 打开它是否也会打开应用程序 3 次(win + r 和“cmd”,输入)放置 C:....\Debug\App.Shell.exe

操作:

感谢您的建议,我注意到我在测试中错误地使用了构造函数。谢谢你的帮助 :)

来自 Gitter 线程

于 2021-11-10T16:07:41.040 回答