0

I am not able to run a test for a WPF desktop application. It fails most of times when I create the Session object for the test. This is a .Net 6.0 application and it starts without admin permissions. It takes a few seconds to start, but it starts propertly after crashing the constructor or running it in a command window

public class UISession : IDisposable
{
    private const string WindowsApplicationDriverurl = "http://127.0.0.1:4723";
    private const string UIId = @"C:\Projects\UI\src\bin\Release\UI.exe";
    public WindowsDriver<WindowsElement>? Session { get; private set; }

    public UISession()
    {
        AppiumOptions uiOptions = new();
        uiOptions.AddAdditionalCapability("app", UIId);
        uiOptions.AddAdditionalCapability("deviceName", "WindowsPC");

        //The next line fails
        Session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverurl), uiOptions);

        Assert.IsNotNull(Session);
    }
}

This is the output for WinAppDriver

    ==========================================
    POST /session HTTP/1.1
    Accept: application/json, image/png
    Content-Length: 136
    Content-Type: application/json;charset=utf-8
    Host: 127.0.0.1:4723
    
    {"desiredCapabilities":{"app":"C:\Projects\UI\src\bin\Release\UI.exe","platformName":"Windows"}}
    HTTP/1.1 500 Internal Error
    Content-Length: 214
    Content-Type: application/json
    
    {"status":13,"value":{"error":"unknown error","message":"Failed to locate opened application window with appId: C:\\Projects\\UI\\src\\bin\\Release\\UI.exe, and processId: 31312"}}

I can run other application like Ms Paint changing the id:

private const string PaintAppId = @"C:\Windows\System32\mspaint.exe";

so the problem might be the time to start my WPF app. Can I add a delay in some way?

Edit: I do can add a delay by adding this line:

uiOptions.AddAdditionalCapability("ms:waitForAppLaunch", "10");

I found a solution for this problem, but I have to start the WPF application by using Process.Start or something similar.

private const string WindowsApplicationDriverurl = "http://127.0.0.1:4723";
private const string ProcessName = "UI";
public WindowsDriver<WindowsElement>? Session { get; private set; }

public UIRunningSession()
{
    IntPtr myAppTopLevelWindowHandle = new IntPtr();
    try
    {
        if (Session == null)
        {
            foreach (Process clsProcess in Process.GetProcesses())
            {
                if (clsProcess.ProcessName.Contains(ProcessName))
                {
                    myAppTopLevelWindowHandle = clsProcess.MainWindowHandle;
                    break;
                }
            }

            var appTopLevelWindowHandleHex = myAppTopLevelWindowHandle.ToString("x");

            AppiumOptions uiOptions = new();
            uiOptions.AddAdditionalCapability("deviceName", "WindowsPC");
            uiOptions.AddAdditionalCapability("platformName", "Windows");
            uiOptions.AddAdditionalCapability("appTopLevelWindow", appTopLevelWindowHandleHex);
            Session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverurl), uiOptions);
            Assert.IsNotNull(Session);
        }
    }
    catch { }
}

0 Answers0