appium / java-client

Java language binding for writing Appium Tests, conforms to W3C WebDriver Protocol
Apache License 2.0
1.2k stars 753 forks source link

For Windows app GUI automation, code snippet fails to start a new session. #2011

Closed akshattambe closed 11 months ago

akshattambe commented 11 months ago

Description

I'm trying a POC for Windows app GUI test automation using WinAppDriver.

My Code snippet fails to start a new session with the message - Response code 400. Message: Bad capabilities. Specify either app or appTopLevelWindow to create a session

Can you please point me in the right direction?

Environment

Code To Reproduce Issue [ Good To Have ]

import io.appium.java_client.windows.WindowsDriver;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;

public class TestClass {

    @Test
    public void test1() throws InterruptedException, MalformedURLException, URISyntaxException {
        DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.setCapability("app", "C:\\Windows\\System32\\calc.exe");
//        capabilities.setCapability("appium:appTopLevelWindow", "calc");
//        capabilities.setCapability("appium:automationName", "Windows");
//        capabilities.setCapability("platformName", "windows");
        WindowsDriver driver = new WindowsDriver(new URI("http://127.0.0.1:4723/").toURL(), capabilities);

        Thread.sleep(5000);
        driver.quit();
    }
}

Exception

Exception in Win App Driver session -

POST /session HTTP/1.1
Accept: */*
Content-Length: 213
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:4723
User-Agent: appium/8.5.1 (selenium/4.12.1 (java windows))
X-Idempotency-Key: b247ec94-4c43-4692-86e7-404129c59787

{
  "capabilities": {
    "firstMatch": [
      {
      }
    ],
    "alwaysMatch": {
      "appium:appTopLevelWindow": "calc",
      "appium:automationName": "Windows",
      "platformName": "windows"
    }
  }
}
HTTP/1.1 400 Bad Request
Content-Length: 141
Content-Type: application/json

{"status":100,"value":{"error":"invalid argument","message":"Bad capabilities. Specify either app or appTopLevelWindow to create a session"}}

Exception in IDE -

.selenium.SessionNotCreatedException: Could not start a new session. Response code 400. Message: Bad capabilities. Specify either app or appTopLevelWindow to create a session 
Host info: host: '3NLYFL3', ip: '10.69.211.5'
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '20.0.2'
Driver info: io.appium.java_client.windows.WindowsDriver
Command: [null, newSession {capabilities=[{appium:appTopLevelWindow=calc, appium:automationName=Windows, platformName=windows}], desiredCapabilities=Capabilities {appium:appTopLevelWindow: calc, appium:automationName: Windows, platformName: windows}}]
Capabilities {appium:appTopLevelWindow: calc, appium:automationName: Windows, platformName: windows}

    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:140)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at io.appium.java_client.remote.AppiumProtocolHandshake.createSession(AppiumProtocolHandshake.java:133)
    at io.appium.java_client.remote.AppiumProtocolHandshake.createSession(AppiumProtocolHandshake.java:102)
    at io.appium.java_client.remote.AppiumCommandExecutor.createSession(AppiumCommandExecutor.java:182)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:250)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:602)
    at io.appium.java_client.AppiumDriver.startSession(AppiumDriver.java:274)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:163)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:89)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:101)
    at io.appium.java_client.windows.WindowsDriver.<init>(WindowsDriver.java:50)
    at WindowsAppTest.test1(WindowsAppTest.java:27)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

Thanks for your help in advance

mykola-mokhnach commented 11 months ago

Use vanilla WebElement

akshattambe commented 11 months ago

i updated the issue, highlighting the foundational problem. I'm not sure what I am missing in a simple POC.

akshattambe commented 11 months ago

I have made the following change-

WindowsDriver driver = new WindowsDriver(new URI("http://127.0.0.1:4723/wd/hub").toURL(), capabilities);

and get the following exception message in my IDE console-

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Unable to parse remote response:  
Host info: host: '3NLYFL3', ip: '10.69.211.5'

    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:126)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at io.appium.java_client.remote.AppiumProtocolHandshake.createSession(AppiumProtocolHandshake.java:133)
    at io.appium.java_client.remote.AppiumProtocolHandshake.createSession(AppiumProtocolHandshake.java:102)
    at io.appium.java_client.remote.AppiumCommandExecutor.createSession(AppiumCommandExecutor.java:182)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:250)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:602)
    at io.appium.java_client.AppiumDriver.startSession(AppiumDriver.java:274)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:163)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:89)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:101)
    at io.appium.java_client.windows.WindowsDriver.<init>(WindowsDriver.java:50)
    at WindowsAppTest.test1(WindowsAppTest.java:19)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: org.openqa.selenium.json.JsonException: Unable to parse: 
    at org.openqa.selenium.json.Json.toType(Json.java:169)
    at org.openqa.selenium.json.Json.toType(Json.java:152)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:122)
    ... 16 more
Caused by: org.openqa.selenium.json.JsonException: Expected to read a START_MAP but instead have: END. Last 0 characters read: 
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '20.0.2'
Driver info: driver.version: WindowsDriver
    at org.openqa.selenium.json.JsonInput.expect(JsonInput.java:443)
    at org.openqa.selenium.json.JsonInput.beginObject(JsonInput.java:331)
    at org.openqa.selenium.json.MapCoercer.lambda$apply$1(MapCoercer.java:64)
    at org.openqa.selenium.json.JsonTypeCoercer.lambda$buildCoercer$6(JsonTypeCoercer.java:174)
    at org.openqa.selenium.json.JsonTypeCoercer.coerce(JsonTypeCoercer.java:149)
    at org.openqa.selenium.json.Json.toType(Json.java:206)
    at org.openqa.selenium.json.Json.toType(Json.java:167)
    ... 18 more

Exception in Win App Driver session -

POST /session HTTP/1.1
Accept: */*
Content-Length: 226
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:4723
User-Agent: appium/8.5.1 (selenium/4.12.1 (java windows))
X-Idempotency-Key: d9d9a3c1-6b9f-4872-bdb0-5b322fe2087f

{
  "capabilities": {
    "firstMatch": [
      {
      }
    ],
    "alwaysMatch": {
      "appium:app": "C:\\Windows\\System32\\calc.exe",
      "appium:automationName": "Windows",
      "platformName": "windows"
    }
  }
}
HTTP/1.1 400 Bad Request
Content-Length: 141
Content-Type: application/json
akshattambe commented 11 months ago

Refer this- https://github.com/appium/java-client/issues/2016