When working on native apps which contain a webview, one needs to move the entire test session into the webview context in order to automate the elements within. In the case where multiple webviews are present, how do we select the right one?
Check out: Test Automation - A Complete Guide
Getting the list of all webviews using the getContexts() command will return a list which looks something like:
Each of the webviews are named with a non-descriptive number, and the order is not guaranteed to be the same on every test run. If only there was a way to get more information about each webview when we call the getContexts() command.
Also check: Batching Appium Commands Using Execute Driver Script to Speed Up Tests
This is where the fullContextList desired capability comes in. When set to true, the contexts returned by the getContexts() command are returned as a JSON string. Once parsed, each context is an object which includes not only the ID of the webview, but also the URL the webview is currently displaying and the Title of the page. Here's an example response when fullContextList is set to true:
If multiple webviews are present in the current view, we can check the urls or titles of each so we can make sure to pick the right one.
Unfortunately, this capability is only supported when testing on iOS devices. There is a way Appium could implement this functionality on Android, but the work has not been done yet. Volunteers are welcome!
Selecting the right webview is a bit tricky in Java: you have to know that the contexts can be cast to Map<String,Object>, which is not obvious.
Here's a sample test written in Java, and as usual the full file can be found in our example code repository.