microsoft / WinAppDriver

Windows Application Driver
MIT License
3.68k stars 1.4k forks source link

Failed to locate opened application window with appId"xxx" and process "xxx" #516

Closed ewancombe closed 5 years ago

ewancombe commented 5 years ago

I have followed this issue which is closed but I have a question that was not quite answered by that issue. I am using Inspect.exe and I am finding that I can for example, launch notepad.exe and the session is OK. The problem is that I am testing a WPF application and when I launch that with the same code I get the error below:

"Failed to locate opened application window with appId"xxx" and process "xxx""

Using Inspect.exe I can see that the Process ID is correct but the AutomationId is blank for my WPF application under test. I am a bit confused because I can see that the filepath of the application can be used to launch the app but I cannot see where I am passing in the application ID.

My question is, can I launch an application using only it's filepath and then create a session for the application if the application does not have an AutomationId. If so how?

    private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
    //private const string AlarmClockAppId = @"C:\Windows\System32\notepad.exe";  //works and launches notepad
    private const string AlarmClockAppId = @"C:\Program Files\BookingSystem\Client\Booking.System.Client.Shell.exe";

    protected static OpenQA.Selenium.Appium.Windows.WindowsDriver<OpenQA.Selenium.Appium.Windows.WindowsElement> session;
    protected static RemoteTouchScreen touchScreen;

    public static void Setup(TestContext context)
    {
        // Launch application if it is not yet launched
        if (session == null || touchScreen == null)
        {
            TearDown();

            // Create a new session to bring up the Alarms & Clock application
            DesiredCapabilities appCapabilities = new DesiredCapabilities();
            appCapabilities.SetCapability("app", AlarmClockAppId);

            //This line fails with an error, does it need to be split into 2 lines to first launch the app and then create a session?
            session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
ewancombe commented 5 years ago

It is worth noting that the Process ID in the error message is correct as that's the Process Id I see when I use Inpect.exe. I am not sure why it needs to use the AuitomationId given that it knows the correct process Id, can I create a session using only the process ID and not the AutomationId?

hassanuz commented 5 years ago

Hi @ewancombe,

For WPF application, the filepath is indeed the applicationID and that should be enough for launching the application.

However, depending on how you create the WPF application, there is a possibility of it having multiple windows at launch including a splashscreen, login dialog, etc.

WinAppDriver by default tries to locate the first top-level window with the same processID. It looks like WinAppDriver was unable to determine the correct top level window for your application. If your application does happen to have a splash screen or dialogue, then we recommend taking a look here for steps towards working around this.

Thanks

ewancombe commented 5 years ago

Hi @hassanuz ,

Thank you, that has solved my problem. I have some experience in CodedUI testing but I'm learning WinAppDriver. I just wasn't sure what each part was doing or how it was meant to work so thanks for explaining that my filepath approach was indeed correct and that the wait was needed.

Ewan

ewancombe commented 5 years ago

In addition to the suggestion of launching my app with a delay, I have found that for my WPF app, I need to define the session as below because it is a windows application but seems to have Web elements,,,

protected static OpenQA.Selenium.Appium.Windows.WindowsDriver session;

hassanuz commented 5 years ago

Thanks for reporting back @ewancombe! Marking issue as closed.

aehven commented 4 years ago

Though this is closed, I would add for posterity:

None of the solutions above worked for me. What did work was removing the the application's package from

C:\Users\Me\AppData\Local\Packages\C4*

the exact package directory name being the same one you have in your application identifier, which you can get by looking at your solution's Package.appxmanifest and going tot he Packaging tab.

Then rebuild and re-run everything.

balaji-githubstore commented 3 years ago

Try to include below capability which solved my issue of Failed to locate opened application window with appId"xxx" and process "xxx"

My exe file present in C:\Program Files\MT

appCapabilities.SetCapability("appWorkingDir", "C:\Program Files\MT");

micahmo commented 3 years ago

@balaji-githubstore Thank you, that solved my issue as well! (Our application expects to be running out of the directory in which the executable is located.)

A good way to do this generically is... options.AddAdditionalCapability("appWorkingDir", Path.GetDirectoryName(appId));

Probably not a bad idea to do this as standard practice.

Ginjitzu commented 1 year ago

appCapabilities.SetCapability("appWorkingDir", MyApplicationDirectory);

This solution worked for me. Oddly, this capability is only required on one of 2 workstations I use to run my tests.