appium / appium-flutter-driver

Appium Flutter Driver is a test automation tool for Flutter apps on multiple platforms/OSes. Appium Flutter Driver is part of the Appium mobile test automation tool maintained by community
MIT License
440 stars 179 forks source link

Sometimes WebView Context Not found #645

Closed DhakshathAmin closed 6 months ago

DhakshathAmin commented 6 months ago

While Automating Hybrid App, (Which supports FLUTTER, NATIVE_APP, WEBVIEW_Chrome, WEBVIEW_Chrome.resi.com)

Sometimes, Driver is unable to get Webviews (both WEBVIEW_Chrome, WEBVIEW_Chrome.resi.com) Why is this?

I had to Close the Appium server, Uninstall Appium Settings and App from Phone and then retry. It will work..

Appium Server : 2.3.0 Appium Flutter Driver : 2.2.4 Mobile : OS 14 Android Pixel 6

DhakshathAmin commented 6 months ago

LOGS :

Calling AppiumDriver.getContexts() with args: ["f2b5ab93-6467-43f6-9071-d6d6e15b8980"] [FlutterDriver] Executing Flutter driver command 'getContexts' [AndroidDriver] Getting a list of available webviews [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC shell cat /proc/net/unix' [AndroidDriver] Parsed 2 active devtools sockets: ["@webview_devtools_remote_2923","@chrome_devtools_remote"] [AndroidDriver] Collecting CDP data of 2 webviews [AndroidDriver] Forwarding remote port webview_devtools_remote_2923 to a local port in range 10900..11000 [AndroidDriver] You could use the 'webviewDevtoolsPort' capability to customize the starting port number [AndroidDriver] Forwarding remote port chrome_devtools_remote to a local port in range 10900..11000 [AndroidDriver] You could use the 'webviewDevtoolsPort' capability to customize the starting port number [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC forward tcp:10900 localabstract:webview_devtools_remote_2923' [ADB] Removing forwarded port socket connection: 10900 [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC forward --remove tcp:10900' [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC forward tcp:10900 localabstract:chrome_devtools_remote' [ADB] Removing forwarded port socket connection: 10900 [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC forward --remove tcp:10900' [AndroidDriver] CDP data collection completed [AndroidDriver] WEBVIEW_2923 mapped to pid 2923 [AndroidDriver] Getting process name for webview 'WEBVIEW_2923' [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC shell ps --help' [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC shell ps -A' [AndroidDriver] Got process name: 'com.resideo.wick' [AndroidDriver] Skipping the webview 'WEBVIEW_2923' at '@webview_devtools_remote_2923' since it has reported having zero pages [AndroidDriver] Skipping the webview 'WEBVIEW_chrome' at '@chrome_devtools_remote' since it has reported having zero pages [AndroidDriver] Found 0 webviews: [] [AndroidUiautomator2Driver@87e0 (8548a16f)] Available contexts: ["NATIVE_APP"] [FlutterDriver@1045 (f2b5ab93)] Responding to client with driver.getContexts() result: ["NATIVE_APP","FLUTTER"] [HTTP] <-- GET /wd/hub/session/f2b5ab93-6467-43f6-9071-d6d6e15b8980/contexts 200 755 ms - 34 [HTTP]

KazuCocoa commented 6 months ago

appium:showChromedriverLog caps and debug log would show more hints.

[AndroidDriver] Skipping the webview 'WEBVIEW_2923' at '@webview_devtools_remote_2923' since it has reported having zero pages

As the message, basically, the device's WebView/Chrome process returned as no pages. Retrial a couple of times/kill chrome process might help in my past experience. (If so, this was out of Appium's behavior caused the issue) https://github.com/appium/appium-android-driver/blob/b2a370457382d6ca615f2ddbf18b39ba00ecf85a/lib/helpers/webview.ts#L399-L404

DhakshathAmin commented 6 months ago

@KazuCocoa Your right, Killing process and restrat everything works, but I see this behaviour too often. Let me come back if more logs and if its really appium issue, let me open ticket in https://github.com/appium/appium

DhakshathAmin commented 6 months ago

@KazuCocoa I dont think appium:showChromedriverLog would be helpful, as I am in flutter context and switching to WebView Context


[FlutterDriver@fb8d (5199b6e3)] Calling AppiumDriver.getCurrentContext() with args: ["5199b6e3-9e9a-4e9b-9bac-9399694a7041"] [FlutterDriver] Executing Flutter driver command 'getCurrentContext' [FlutterDriver@fb8d (5199b6e3)] Responding to client with driver.getCurrentContext() result: "FLUTTER" [HTTP] <-- GET /wd/hub/session/5199b6e3-9e9a-4e9b-9bac-9399694a7041/context 200 1 ms - 19 [HTTP] [HTTP] --> GET /wd/hub/session/5199b6e3-9e9a-4e9b-9bac-9399694a7041/contexts [HTTP] {} [FlutterDriver@fb8d (5199b6e3)] Calling AppiumDriver.getContexts() with args: ["5199b6e3-9e9a-4e9b-9bac-9399694a7041"] [FlutterDriver] Executing Flutter driver command 'getContexts' [AndroidDriver] Getting a list of available webviews [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC shell cat /proc/net/unix' [AndroidDriver] Parsed 2 active devtools sockets: ["@webview_devtools_remote_29065","@chrome_devtools_remote"] [AndroidDriver] Collecting CDP data of 2 webviews [AndroidDriver] Forwarding remote port webview_devtools_remote_29065 to a local port in range 10900..11000 [AndroidDriver] You could use the 'webviewDevtoolsPort' capability to customize the starting port number [AndroidDriver] Forwarding remote port chrome_devtools_remote to a local port in range 10900..11000 [AndroidDriver] You could use the 'webviewDevtoolsPort' capability to customize the starting port number [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC forward tcp:10900 localabstract:webview_devtools_remote_29065' [ADB] Removing forwarded port socket connection: 10900 [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC forward --remove tcp:10900' [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC forward tcp:10900 localabstract:chrome_devtools_remote' [ADB] Removing forwarded port socket connection: 10900 [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC forward --remove tcp:10900' [AndroidDriver] CDP data collection completed [AndroidDriver] WEBVIEW_29065 mapped to pid 29065 [AndroidDriver] Getting process name for webview 'WEBVIEW_29065' [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC shell ps -A' [AndroidDriver] Got process name: 'com.resideo.wick' [AndroidDriver] Skipping the webview 'WEBVIEW_29065' at '@webview_devtools_remote_29065' since it has reported having zero pages [AndroidDriver] Skipping the webview 'WEBVIEW_chrome' at '@chrome_devtools_remote' since it has reported having zero pages [AndroidDriver] Found 0 webviews: [] [AndroidUiautomator2Driver@cca2 (25c022ca)] Available contexts: ["NATIVE_APP"] [FlutterDriver@fb8d (5199b6e3)] Responding to client with driver.getContexts() result: ["NATIVE_APP","FLUTTER"] [HTTP] <-- GET /wd/hub/session/5199b6e3-9e9a-4e9b-9bac-9399694a7041/contexts 200 611 ms - 34 [HTTP] [HTTP] --> POST /wd/hub/session/5199b6e3-9e9a-4e9b-9bac-9399694a7041/context [HTTP] {"name":"WEBVIEW_chrome"} [FlutterDriver@fb8d (5199b6e3)] Calling AppiumDriver.setContext() with args: ["WEBVIEW_chrome","5199b6e3-9e9a-4e9b-9bac-9399694a7041"] [FlutterDriver] Executing Flutter driver command 'setContext' [AndroidDriver] Getting a list of available webviews [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC shell cat /proc/net/unix' [AndroidDriver] Parsed 2 active devtools sockets: ["@webview_devtools_remote_29065","@chrome_devtools_remote"] [AndroidDriver] Collecting CDP data of 2 webviews [AndroidDriver] Forwarding remote port webview_devtools_remote_29065 to a local port in range 10900..11000 [AndroidDriver] You could use the 'webviewDevtoolsPort' capability to customize the starting port number [AndroidDriver] Forwarding remote port chrome_devtools_remote to a local port in range 10900..11000 [AndroidDriver] You could use the 'webviewDevtoolsPort' capability to customize the starting port number [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC forward tcp:10900 localabstract:webview_devtools_remote_29065' [ADB] Removing forwarded port socket connection: 10900 [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC forward --remove tcp:10900' [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC forward tcp:10900 localabstract:chrome_devtools_remote' [ADB] Removing forwarded port socket connection: 10900 [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC forward --remove tcp:10900' [AndroidDriver] CDP data collection completed [AndroidDriver] WEBVIEW_29065 mapped to pid 29065 [AndroidDriver] Getting process name for webview 'WEBVIEW_29065' [ADB] Running '/Users/dhakshath/Library/Android/sdk/platform-tools/adb -P 5037 -s 24141FDF6001CC shell ps -A' [AndroidDriver] Got process name: 'com.resideo.wick' [AndroidDriver] Skipping the webview 'WEBVIEW_29065' at '@webview_devtools_remote_29065' since it has reported having zero pages [AndroidDriver] Skipping the webview 'WEBVIEW_chrome' at '@chrome_devtools_remote' since it has reported having zero pages [AndroidDriver] Found 0 webviews: [] [AndroidUiautomator2Driver@cca2 (25c022ca)] Available contexts: ["NATIVE_APP"] [FlutterDriver@fb8d (5199b6e3)] Encountered internal error running command: NoSuchContextError: No such context found. [FlutterDriver@fb8d (5199b6e3)] at AndroidUiautomator2Driver.setContext (/Users/dhakshath/.appium/node_modules/appium-flutter-driver/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/commands/context.js:77:13) [FlutterDriver@fb8d (5199b6e3)] at FlutterDriver.setContext (/Users/dhakshath/.appium/node_modules/appium-flutter-driver/lib/commands/context.ts:20:7) [HTTP] <-- POST /wd/hub/session/5199b6e3-9e9a-4e9b-9bac-9399694a7041/context 400 568 ms - 464 [HTTP]

ashwithpoojary98 commented 6 months ago

@DhakshathAmin The logs show only two contexts: "NATIVE_APP" and "FLUTTER." [FlutterDriver@fb8d (5199b6e3)] Responding to client with driver.getContexts() result: ["NATIVE_APP","FLUTTER"] First, try switching to the native context, then switch webview it might work.

DhakshathAmin commented 6 months ago

Solmelu @ashwithpoojary98 👍 . It reduced the probability, but its not a complete fix.

cc @KazuCocoa

KazuCocoa commented 6 months ago

https://github.com/appium/appium-flutter-driver/blob/f308ca80b151c0d2d673c6ed024fd85fb79f7936/driver/lib/commands/context.ts#L27-L30 It always calls both, so no diff between NATIVE_APP and FLUTTER to get contexts by NATIVE_APP in this driver's perspecitve.

If it changed the behavior, it implies we should call the endpoint a couple of times to get contexts as something was delayed in the Safari/Chrome side (the device side). For example, I observed newer chromedriver x chrome could have an issue to get chrome_devtools_remote related info. It needed to try a couple of times as chrome/chromedriver side's behavior to get available web view related info.

KazuCocoa commented 6 months ago

https://github.com/appium/appium-uiautomator2-driver/pull/699 is not for this driver, but it is the case. https://github.com/appium/appium/issues/19251 Thus, if you could get expected web view context after a couple of retrial, I'd recommend you to add a helper method to call multiple times to call get contexts. I hope Google will improve the chrome's behavior in the future.

Or not available yet in this driver, but you could call https://github.com/appium/appium-uiautomator2-driver?tab=readme-ov-file#mobile-getcontexts with waitForWebviewMs after a new driver version release in NATIVE_APP context. This is what Appium can provide as Appium layer's workaround.

KazuCocoa commented 6 months ago

appium flutter driver 2.3.0 includes the latest XCUITest/UIA2 driver update

KazuCocoa commented 6 months ago

Close as possible chrome side thing. https://github.com/appium/appium-flutter-driver/issues/645#issuecomment-1872571881 is current workaround as Appium (based on my past observation possibly similar issue)