WinAppDriver, connecting to existing instance of an application

In a previous post I showed how we can use specflow.actions.json to configure the WinAppDriver with the application name etc. but what if you want to simply connect to an existing instance of an application?

If we’re stick with Specflow then we would probably create a file in the Drivers folder of our test project (or ofcourse add one if it doesn’t exist)

public class WinAppDriver : IDisposable
{
   provate WindowsDriver<WindowsElement> _driver;

   public WindowsDriver<WindowsElement> Current
   {
      get 
      {
         if(!_driver != null) 
         {
            return _driver;
         }

         var appWindowHandle = new IntPtr();
         foreach(var clsProcess in Process.GetProcesses())
         {
            if(clsProcess.ProcessName.Contains("MyApp"))
            {
               appWindowHandle = clsProcess.MainWindowHandle;
               break;
            }
         }

         var appWindowHandleHex = appWindowHandle.ToString("x");

         var options = new AppiumOptions
         {
            PlatFormName = "Windows"
         };

         options.AddAdditionalCapability("deviceName", "WindowsPC");
         options.AddAdditionalCapability("appTopLevelWindows", appWindowHandleHex);

         return _driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723", options);
      }
   }
}

Testing Windows Package Application

With a packaged application, i.e. from the Window Store we cannot just supply the path of the .exe, instead we need the Package.appxmanifest “Package family name” which can be used as the “app” value