0

I have a android application it opens a webview for login. I need to insert text for username and password but appium is not able to find element on webview. Appium inspector also not able to inspect element on the screen. Is this a issue with android webview ?

Xpath is just showing : /hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.webkit.WebView

2 Answers2

0

Android layout is made with ViewGroups and Views, and WebView is a View with own content, can't have childs like ViewGroups. text fields on rendered site aren't Androids EditTexts, they are just rendered and handled by Chromium engine. e.g. they have own default style and they may be styled by CSS, but for shure they won't be styled same as your EditTexts when you set some style for them "globally"

WebView is a bit like VideoView - yes, you can play a video, which contains just text printed on the middle of the screen, but this doesn't make VideoView behave like TextView. You can't e.g. change text color or select part of text for copying. whole content is rendered by another mechanism (which is huge and very complicated in both cases, working under the hood, probably running new threads or maybe even processes), just like Chromium renders web sites.

still you can try to get access to web content-text-fields with JS, using smth like below

webView.loadUrl("javascript:document.getElementByName('id_of_field').value = 'inserted text';");

don't forget to enable JS at first

webView.getSettings().setJavaScriptEnabled(true);

PS. Enable "Show layout bounds" in developer options and inspect how does you app looks like (or any other, or even system itself) and how looks web content (inside WebView or any browser app)

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • he is talking about Appium and Appium Inspector. You answer seems to be about Android framework. – Risinek Jan 11 '21 at 02:01
  • 1
    Appium by default inspects only native `View`s. I done some small search and looks like there is a [possibily for enabling `WebView` content support](https://stackoverflow.com/questions/12448663/how-to-set-text-size-using-the-selector) – snachmsm Jan 11 '21 at 09:01
0

You need to switch context first into WebView when trying to find elements inside it. See official documentation Automating hybrid apps.

Here are steps how enter WebView from the documentation mentioned above:

Entering the web view context

Here are the steps required to talk to a web view in your Appium test:

  1. Navigate to a portion of your app where a web view is active

  2. Retrieve the currently available contexts. This returns a list of contexts we can access, like 'NATIVE_APP' or 'WEBVIEW_1'

  3. Set the context with the id of the context you want to access This puts your Appium session into a mode where all commands are interpreted as being intended for automating the web view, rather than the native portion of the app. For example, if you run getElementByTagName, it will operate on the DOM of the web view, rather than return native elements. Of course, certain WebDriver methods only make sense in one context or another, so in the wrong context you will receive an error message.

  4. To stop automating in the web view context and go back to automating the native portion of the app, simply set the context again with the native context id (generally 'NATIVE_APP') to leave the web context and once again access the native commands.

Risinek
  • 390
  • 2
  • 16