简体   繁体   中英

Wait for application launch without using Thread.Sleep() using FLAUI

I am new to using FLAUI and Automation Testing and would like to use it to test my system. At the moment I am using a Thread.Sleep() to wait till the application launches to then find the Login textbox. Is there a more efficient way to do this rather than using Thread.Sleep()?

At the moment i launch the application and use Thread.sleep(10000) to wait until the applicationis fully launched and that the logIn textbox is find-able before clicking on the control to input the password to enter the application. However I understand that Thread.Sleep is the worst way to tell the system to wait especially in automated tests. Could anyone offer any other things i could test out?

It is always the best to use Retry mechanism and wait until your main window loads and controls are visible. For example, after calling Application.Launch you can retry up to 30 seconds to find main window, and txtLogin in it:

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

The question already has good answers, but I found another way to wait for any element (including main window) using the Retry class in FlaUI.Core.Tools.Retry class

[TestFixture]
public class SmokeTests
{
    private Application _theApp;
    private UIA3Automation _automation;
    private Window _mainWindow;
    private const int BigWaitTimeout = 3000;
    private const int SmallWaitTimeout = 1000;

    [SetUp]
    public void Setup()
    {
        _theApp = FlaUI.Core.Application.Launch(new ProcessStartInfo("YOUR_APPLICATION.exe", "/quickStart"));
        _automation = new UIA3Automation();
        _mainWindow = _theApp.GetMainWindow(_automation);
    }

    [TearDown]
    public void Teardown()
    {
        _automation?.Dispose();
        _theApp?.Close();
    }

    [Test]
    public void Foo()
    {
        // This will wait until the element is available, or timeout passed
        var examplesWrapPanel = WaitForElement(() => _mainWindow.FindFirstDescendant(cf => cf.ByAutomationId("ExamplesWrapPanel")));

        // This will wait for the child element or timeout 
        var exampleButton = WaitForElement(() => examplesWrapPanel?.FindFirstDescendant(cf => cf.ByAutomationId("Another Automation Id")).AsButton());

        // Do something with your elements 
        exampleButton?.WaitUntilClickable();
        exampleButton?.Invoke();
    }

    private T WaitForElement<T>(Func<T> getter)
    {
        var retry = Retry.WhileNull<T>(
            () => getter(),
            TimeSpan.FromMilliseconds(BigWaitTimeout));

        if (!retry.Success)
        {
            Assert.Fail("Failed to get an element within a wait timeout");
        }

        return retry.Result;
    }
}

}

private void RunProc()
{
Process.Start("exeName");
}


public async Task StartProcessAsync()
{
var result= await Task.Run(()=>RunProc());
//optional
Task.Delay(new TimeSpan.FromSeconds(5));
}

Did you try this solution?

public static void LaunchApplication(string exePath, string arguments, bool waitForExit, bool waitForStart, int waitForStartTimeout)
    {
        ProcessStartInfo thisProcessInfo = new ProcessStartInfo();
        thisProcessInfo.CreateNoWindow = true;
        thisProcessInfo.UseShellExecute = false;
        thisProcessInfo.RedirectStandardOutput = false;
        thisProcessInfo.FileName = exePath;
        thisProcessInfo.Arguments = arguments;
        using(Process thisProcess = Process.Start(thisProcessInfo))
        {
            if(waitForStart)
                thisProcess.WaitForInputIdle(waitForStartTimeout);
            if(waitForExit)
                thisProcess.WaitForExit();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM