javaselenium-webdriverappiumwinappdriverappium-desktop

"Bad capabilities. Specify either app or appTopLevelWindow to create a session" Error given by the WinApp driver


I am getting the below error screenshot when using the WinApp driver for the desktop automation.

enter image description here

The source code I am using to initiate the WinApp driver session as below.

public void setupDesktopAutomation(String appName, String platformName, String deviceName) {

try {

    Desktop desktop = Desktop.getDesktop();
    desktop.open(new File("D:\\Software\\Windows Application Driver\\WinApp\\WinAppDriver.exe"));
    
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("app", appName);
    caps.setCapability("platformName", platformName);
    caps.setCapability("deviceName", deviceName);
    windowsDriver = new WindowsDriver(new URL("http://127.0.0.1:4723/"), caps);
    windowsDriver.manage().wait(5000);
} catch (Exception e) {
    throw new RuntimeException(e);
}

}

I did many research on this and found that the Selenium version 4 (I am using the Selenium Version 4.7.2) is not compatible with the latest WinApp driver. There are many articles found in the web that we need to downgrade the Selenium version to 3.141.1 in order to WinApp driver to work

  1. https://github.com/microsoft/WinAppDriver/issues/1774.
  2. https://github.com/microsoft/WinAppDriver/issues/1839

Also I found that in few articles that we can use Selenium Version 4 and still connect to the WinApp driver using the Appium as a middle man. But those articles does not shown how we can do this. Please check the below article.

  1. https://github.com/nightwatchjs/nightwatch/issues/3579

So I want to know without downgrading the selenium version, how I can modify the above source code to initiate the WinApp driver successfully.


Solution

  • Finally I was able to solve the problem of my own. Here are the steps.

    1. Download the Appium. - https://appium.io/downloads.html
    2. Install the downloaded Appium.
    3. Start the Appium server and give a host and port number - Here I have given as shown in below screenshot.

    enter image description here

    1. You can use the below source code to connect to the Appium server and open the notepad application (In the URL section you have to give the correct host and port number as given in step 3, Also appname is the parameter where you can pass the application you want to open)

      DesiredCapabilities caps = new DesiredCapabilities();

      caps.setCapability("app", appName);

      windowsDriver = new WindowsDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);

    Pre-Requisite:

    java.lang.RuntimeException: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: An unknown server-side error occurred while processing the command. Original error: WinAppDriver.exe has not been found in any of these locations: C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe,C:\Program Files\Windows Application Driver\WinAppDriver.exe,C:\Program Files\Windows Application Driver\WinAppDriver.exe. Is it installed?

    I got the above error when initially execute the Appium driver, so I had manually downloaded WinApp driver to one of the above locations.