Resolving the ‘URL did not map to a valid resource’ Error for Desktop Application Testing with WinAppDriver and Java

Resolving the 'URL did not map to a valid resource' Error for Desktop Application Testing with WinAppDriver and Java

5 April 2024 Stephan Petzl Leave a comment Tech-Help

When automating tests for desktop applications like the Windows Calculator using WinAppDriver, Appium, and Java, a common error that may arise is:

org.openqa.selenium.UnsupportedCommandException: The URL '/session' did not map to a valid resource

This error can be perplexing and halt your testing process. Fear not, as this article will guide you through the steps to resolve this issue and continue with your automation testing.

Understanding the Error

The error message indicates that the client is trying to access a URL endpoint that is not recognized by the server. In the context of Appium and WinAppDriver, this usually happens when the server URL is not specified correctly in your test code.

Correcting the Server URL

To resolve the error, you must ensure that the server URL is correctly specified depending on the service you’re using. Here are the solutions:

  • If you are using the Appium server: You need to append /wd/hub to the server URL when initializing your driver, like so:
    CalculatorSession = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  • If you are using WinAppDriver directly: You should only specify the IP and the port, as follows:
    CalculatorSession = new IOSDriver(new URL("http://127.0.0.1:4723"), capabilities);

Note that you should not run both the Appium server and WinAppDriver simultaneously. Choose one method and stick to it for your testing session.

Sample Code Snippet

Here’s a corrected version of the initial code snippet that should address the error:

import org.junit.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;
import java.net.URL;
import io.appium.java_client.ios.IOSDriver;

public class CalculatorTest {

    private static IOSDriver<WebElement> CalculatorSession = null;
    private static WebElement CalculatorResult = null;

    @BeforeClass
    public static void setup() {
        try {
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("app", "C:\\Windows\\System32\\calc.exe");
            // Corrected server URL with /wd/hub
            CalculatorSession = new IOSDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
            CalculatorSession.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

            CalculatorSession.findElementByName("Clear").click();
            CalculatorSession.findElementByName("Seven").click();
            CalculatorResult = CalculatorSession.findElementByName("Display is  7 ");
            Assert.assertNotNull(CalculatorResult);

        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            // Cleanup if necessary
        }
    }
}

Remember to replace IOSDriver with the appropriate driver for Windows applications, such as WindowsDriver, if you’re not testing an iOS app.

Conclusion

By adjusting the server URL in your test setup, you can overcome the ‘/session‘ mapping error and successfully run your automated tests using WinAppDriver and Java. Ensure that you’re using the correct URL based on whether you’re connecting to Appium or WinAppDriver directly.

Like this article? there’s more where that came from!