Skip to content

Failed to locate opened application window with appId: C:\\Program Files (x86)\\Microsoft\\xxxxx\\xxxxxxxx.exe, and processId: 4352 #784

Open
@sparida1

Description

@sparida1

Winapp driver throwing exception when trying to start the application from session as below.
1)Tried open the app via processId , and could not get the session from windowhandle.
2)In catch toplevelwindow also not working.

Any solution for this issue?
Please suggest.

public class AppLauncherHelpers
{
private readonly string _windowsApplicationDriverUrl;
protected static WindowsDriver _session;

    public AppLauncherHelpers()
    {
        _windowsApplicationDriverUrl = "http://127.0.0.1:4723";

    }

    public WindowsDriver<WindowsElement> GetAppSessionAfterLaunch()
    {
        try
        {
            
            int processId= StartWinAppDriver();
            if (processId!=0 &&_session == null)
            {
                TearDown();
              

                DesiredCapabilities appCapabilities = new DesiredCapabilities();
                appCapabilities.SetCapability("app", @"C:\Program Files (x86)\Microsoft\xxxxxxx\xxxxxx.exe");
                
                appCapabilities.SetCapability("appWorkingDir", @"C:\Program Files (x86)\Microsoft\xxxxxxx.exe");
                _session = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appCapabilities,TimeSpan.FromHours(1));

                //Process process = Process.GetProcessesByName("xxxxxx.exe");
                //_session = (WindowsDriver<WindowsElement>)process.

            }
            return _session;
        }
        catch(Exception Ex)
        {
            if (Ex.Message.Contains("Failed to locate opened application window with appId: xxxxxxxxx.exe"))
            {
                Thread.Sleep(10000);
                DesiredCapabilities desktopCapabilities = new DesiredCapabilities();
                desktopCapabilities.SetCapability("app", "Root");
                _session = new WindowsDriver<WindowsElement>(new Uri(_windowsApplicationDriverUrl), desktopCapabilities);
                Assert.IsNotNull(_session);

                WindowsElement XXXShellWindow = _session.FindElementByName("xxxxx.exe");
                string XXXShellTopLevelWindowHandle = (int.Parse(XXXShellWindow.GetAttribute("NativeWindowHandle"))).ToString("x");

                DesiredCapabilities appCapabilities = new DesiredCapabilities();
                appCapabilities.SetCapability("appToplevelWindow", XXXShellTopLevelWindowHandle);
                _session = new WindowsDriver<WindowsElement>(new Uri(_windowsApplicationDriverUrl), appCapabilities, TimeSpan.FromSeconds(20));
                return _session;
            }
            else
            {
                Assert.Fail("Unexpected error occured: {0}", Ex.Message);
                return null;
            }
        }
       
    }

    private int StartWinAppDriver()
    {
        int? id;
        string WinAppDriverExePath = ConfigurationManager.AppSettings["WinAppDriverPath"];
        Process process = Process.Start(WinAppDriverExePath);
        Thread.Sleep(5000);
        id = process.Id;
        return id ?? 0;
    }
    public static void TearDown()
    {
        // Close the application and delete the session
        if (_session != null)
        {
            _session.Quit();
            _session = null;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions