Skip to content

[🐛 Bug]: Android context not selected right when switching context #14755

@jhdg

Description

@jhdg

Have you read the Contributing Guidelines on issues?

WebdriverIO Version

9.19.2

Node.js Version

22.14.0

Mode

Standalone Mode

Which capabilities are you using?

{"platformName":"android","appium:automationName":"Espresso","appium:udid":"emulator-5554","appium:deviceName":"Pixel_5_API_35","appium:appPackage":"com.ismobile.android.blaandroid","appium:appActivity":"com.ismobile.blaandroid.MainActivity","appium:systemPort":8300,"appium:chromedriverPort":8401,"appium:skipServerInstallation":true,"appium:fullReset":false,"appium:noReset":true,"appium:noSign":true,"appium:newCommandTimeout":240}

What happened?

When running with Appium and appium-espresso-driver we have code that does this:
await driver.switchContext({ url: webContext.url });

where url in this case is "http://127.0.0.1:14532/Micro?sub=showsummary"

This fails because the Android app in question (large and copyrighted and can not be shared) has two web views. This is the answer from the appium-espresso-driver:

[{"id":"NATIVE_APP"},{"androidWebviewData":{"attached":false,"empty":true,"height":0,"neverAttached":true,"screenX":0,"screenY":0,"visible":true,"width":0},"id":"WEBVIEW_com.ismobile.android.blaandroid","title":"about:blank","url":"about:blank","packageName":"com.ismobile.android.blaandroid","webviewPageId":"2D2D1AACA229F396B50C3240591FC7C4"},{"androidWebviewData":{"attached":true,"empty":false,"height":1984,"neverAttached":false,"screenX":0,"screenY":290,"visible":true,"width":1080},"id":"WEBVIEW_com.ismobile.android.blaandroid","title":"Översikt","url":"http://127.0.0.1:14532/Micro?sub=showsummary","packageName":"com.ismobile.android.blaandroid","webviewPageId":"69309624CC6CB0FDA3685FEE2BCEA695"}]

The code in WebdriverIO function getCurrentContexts does this:

const androidContext = parsedContexts.find((context) => context.packageName === packageName);

This returns the first web view context (url: about:blank) which is not the one we want.

In wbedriverio function getCurrentContexts this will be false for androidContext:
const isAndroidWebviewDataEmpty = androidContext && androidContext.androidWebviewData?.empty;

The function then throws an exception ("...no webview with pages was loaded in this response") and does not even consider checking the other webview.

The expected outcome is that getCurrentContexts loops over all returned contexts and do not fail if the first does not match.

What is your expected behavior?

getCurrentContexts should consider all contexts, not just the first.

How to reproduce the bug.

Sorry, not trivial.

Relevant log output

Error: The packageName 'com.ismobile.android.blaandroid' matches, but no webview with pages was loaded in this response: [{"proc":"@webview_devtools_remote_6858","webview":"WEBVIEW_6858","info":{"Android-Package":"com.ismobile.android.blaandroid","Browser":"Chrome/124.0.6367.219","Protocol-Version":"1.3","User-Agent":"Mozilla/5.0 (Linux; Android 15; sdk_gphone64_arm64 Build/AE3A.240806.043; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/124.0.6367.219 Mobile Safari/537.36","V8-Version":"12.4.254.16","WebKit-Version":"537.36 (@7741f71cbce3b7dfe3eb5890976d2596364b0733)","webSocketDebuggerUrl":"ws://127.0.0.1:10900/devtools/browser"},"pages":[{"description":"{\"attached\":false,\"empty\":true,\"never_attached\":true,\"screenX\":0,\"screenY\":0,\"visible\":true}","devtoolsFrontendUrl":"https://chrome-devtools-frontend.appspot.com/serve_internal_file/@7741f71cbce3b7dfe3eb5890976d2596364b0733/inspector.html?ws=127.0.0.1:10900/devtools/page/B4EF628032DA04BF5D2854F1B8CA7A37","id":"B4EF628032DA04BF5D2854F1B8CA7A37","title":"about:blank","type":"page","url":"about:blank","webSocketDebuggerUrl":"ws://127.0.0.1:10900/devtools/page/B4EF628032DA04BF5D2854F1B8CA7A37"},{"description":"{\"attached\":true,\"empty\":false,\"height\":1984,\"never_attached\":false,\"screenX\":0,\"screenY\":290,\"visible\":true,\"width\":1080}","devtoolsFrontendUrl":"https://chrome-devtools-frontend.appspot.com/serve_internal_file/@7741f71cbce3b7dfe3eb5890976d2596364b0733/inspector.html?ws=127.0.0.1:10900/devtools/page/B6745CF76C090CEC213E4A2A2B21D228","faviconUrl":"http://127.0.0.1:14532/images/icons/bla.ico","id":"B6745CF76C090CEC213E4A2A2B21D228","title":"Översikt","type":"page","url":"http://127.0.0.1:14532/Micro?sub=showsummary","webSocketDebuggerUrl":"ws://127.0.0.1:10900/devtools/page/B6745CF76C090CEC213E4A2A2B21D228"}],"webviewName":"WEBVIEW_com.ismobile.android.blaandroid"}]'

Code of Conduct

  • I agree to follow this project's Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛help wantedIssues that are free to take by anyone interested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions