appium / java-client

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

getShadowRoot() method is not working in Appium Webview - The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource #1657

Open shagupta92042 opened 2 years ago

shagupta92042 commented 2 years ago

The problem

We are trying to test a hybrid app and we are using webview to test the same . We recently upgraded the appium client from 7.1.0 to 8.0.0 and selenium to 4.1.2 version. While acccessing the shadow dom element the code is failing on Mobile where as the same code is working fine on Desktop Web automation . In selenium 4 , to access the shadow dom a new method was introduced which is getShadowRoot() . When we try to access the shadow element using getShadowRoot() method then we get below error

org.openqa.selenium.UnsupportedCommandException: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource Build info: version: '4.1.2', revision: '9a5a329c5a' System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_322' Driver info: io.appium.java_client.android.AndroidDriver Command: [1c227afb-151c-4642-a3aa-256dbf33c2c6, getElementShadowRoot {id=0.9766494918888731-11}] Capabilities {appium:app: D:\release.apk, appium:appActivity: com.iongroup.ionweb.activit..., appium:appPackage: com.iongroup.ionweb.agt, appium:autoGrantPermissions: true, appium:automationName: uiautomator2, appium:chromeOptions: {w3c: false}, appium:chromedriverExecutable: C:\Users\shashank.gupta\nod..., appium:databaseEnabled: false, appium:desired: {app: D:\release.apk, appActivity: com.iongroup.ionweb.activit..., appPackage: com.iongroup.ionweb.agt, autoGrantPermissions: true, automationName: uiautomator2, chromeOptions: {w3c: false}, chromedriverExecutable: C:\Users\shashank.gupta\nod..., deviceName: CVH7N16B14000292, newCommandTimeout: 60, platformName: android, platformVersion: 8.1.0}, appium:deviceApiLevel: 27, appium:deviceManufacturer: Huawei, appium:deviceModel: Nexus 6P, appium:deviceName: CVH7N16B14000292, appium:deviceScreenDensity: 560, appium:deviceScreenSize: 1440x2560, appium:deviceUDID: CVH7N16B14000292, appium:javascriptEnabled: true, appium:locationContextEnabled: false, appium:networkConnectionEnabled: true, appium:newCommandTimeout: 60, appium:pixelRatio: 3.5, appium:platformVersion: 8.1.0, appium:statBarHeight: 84, appium:takesScreenshot: true, appium:viewportRect: {height: 2308, left: 0, top: 84, width: 1440}, appium:warnings: {}, appium:webStorageEnabled: false, platformName: android}

When we use the belowold code which used to work with Android System webview version 95 then we get the below error

(code ) - WebElement shadowRootForTraderAcc = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowDomHostForTraderAcc);

(Error) - java.lang.ClassCastException: org.openqa.selenium.remote.ShadowRoot cannot be cast to org.openqa.selenium.WebElement

Environment

Code To Reproduce Issue [ Good To Have ]

WebElement shadowDomHostForTradeSettings = (wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//it-trade-tab//it-account-selector//ion-autocomplete")))); SearchContext shadowRootForTradeSettings = shadowDomHostForTradeSettings.getShadowRoot();

mykola-mokhnach commented 2 years ago

(code ) - WebElement shadowRootForTraderAcc = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowDomHostForTraderAcc);

(Error) - java.lang.ClassCastException: org.openqa.selenium.remote.ShadowRoot cannot be cast to org.openqa.selenium.WebElement

The error is expected. The returned object cannot be cast to WebElement, so the above statement should not work.

shagupta92042 commented 2 years ago

@mykola-mokhnach Yes therefore we are using below new code to access the shadow dom

WebElement shadowDomHostForTradeSettings = (wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//it-trade-tab//it-account-selector//ion-autocomplete")))); SearchContext shadowRootForTradeSettings = shadowDomHostForTradeSettings.getShadowRoot();

using above code we are getting below error

The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource

mykola-mokhnach commented 2 years ago

The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource

It is hard to say anything without having the full server log

shagupta92042 commented 2 years ago

https://gist.github.com/shagupta92042/fa4ebd4409afd10cdcfe08ca613edab8

Please check the Appium server logs for the same.

mykola-mokhnach commented 2 years ago

https://github.com/appium/appium/pull/16538 should resolve the "no route" issue after it is published, although it will only be available in Appium2

shagupta92042 commented 2 years ago

Thanks @mykola-mokhnach , Could you please suggest by when latest release will be available for use with this fix ?

mykola-mokhnach commented 2 years ago

Any following Appium2 version since beta.25

shagupta92042 commented 2 years ago

@mykola-mokhnach I tested this with Appium v2.0.0-beta.27 but still getting no route error for shadow dom elements . No route found for /wd/hub/session/ec9f6a47-d6aa-4f46-836b-8ecfe0768c78/shadow/0.9815731080829553-3/element

Please check the logs at below location https://gist.github.com/shagupta92042/06e6d55698dbddb180cb1ced4b6fb185

mykola-mokhnach commented 2 years ago

[35m[HTTP] --> GET /wd/hub/session/ec9f6a47-d6aa-4f46-836b-8ecfe0768c78/element/0.9815731080829553-2/shadow [HTTP] {} [AndroidUiautomator2Driver@c2a4 (ec9f6a47)] Driver proxy active, passing request on via HTTP proxy [debug] [WD Proxy] Matched '/wd/hub/session/ec9f6a47-d6aa-4f46-836b-8ecfe0768c78/element/0.9815731080829553-2/shadow' to command name 'elementShadowRoot' [debug] [WD Proxy] Proxying [GET /wd/hub/session/ec9f6a47-d6aa-4f46-836b-8ecfe0768c78/element/0.9815731080829553-2/shadow] to [GET http://127.0.0.1:8000/session/015b0d67d8100da7cf09fbf161dcbfa2/element/0.9815731080829553-2/shadow] with no body [debug] [WD Proxy] Got response with status 200: {"sessionId":"015b0d67d8100da7cf09fbf161dcbfa2","status":0,"value":{"shadow-6066-11e4-a52e-4f735466cecf":"0.9815731080829553-3"}} [WD Proxy] Replacing sessionId 015b0d67d8100da7cf09fbf161dcbfa2 with ec9f6a47-d6aa-4f46-836b-8ecfe0768c78 [HTTP] <-- GET /wd/hub/session/ec9f6a47-d6aa-4f46-836b-8ecfe0768c78/element/0.9815731080829553-2/shadow 200 31 ms - 122 [HTTP] 

Not sure where an error is visible. This particular request has passed as expected

shagupta92042 commented 2 years ago

@mykola-mokhnach Please check at 1013 line

[HTTP] --> POST /wd/hub/session/ec9f6a47-d6aa-4f46-836b-8ecfe0768c78/shadow/0.9815731080829553-3/element [HTTP] {"shadowId":"0.9815731080829553-3","using":"css selector","value":".selectize-input.items.full.has-options.has-items"} [debug] [HTTP] No route found for /wd/hub/session/ec9f6a47-d6aa-4f46-836b-8ecfe0768c78/shadow/0.9815731080829553-3/element [HTTP] <-- POST /wd/hub/session/ec9f6a47-d6aa-4f46-836b-8ecfe0768c78/shadow/0.9815731080829553-3/element 404 18 ms - 262 [HTTP] 

mykola-mokhnach commented 2 years ago

These are different routes. They should be added separately

shagupta92042 commented 2 years ago

@mykola-mokhnach So how can this be resolved ? Does it requires a fix from your end ?

mykola-mokhnach commented 2 years ago

@mykola-mokhnach So how can this be resolved ? Does it requires a fix from your end ?

I've added a PR. New routes will be available in the next server release

bawarim commented 5 months ago

@mykola-mokhnach Do we have any update this? I am also facing the same error using Appium with dotnet client.