Appium + Sauce Labs Bootcamp: Chapter 3, Working with Hybrid Apps and Mobile Web

Sauce AI for Test Authoring: Move from intent to execution in minutes.|xBack to ResourcesBlogPosted

May 01, 2026 · 5 min read · Mobile Testing

Sauce AI for Test Authoring: Move from intent to execution in minutes.

|

x

Back to Resources

Blog

Posted July 6, 2015

Appium + Sauce Labs Bootcamp: Chapter 3, Working with Hybrid Apps and Mobile Web

quote

This is the 3rd in a serial of posts that discuss habituate Appium with Sauce Labs. Chapter 1 continueLanguage Bindings;Chapter 2 discussesTouch Actions; this chapter coversTesting Hybrid Apps & amp; Mobile Web; and Chapter 4 is aboutAdvanced Desired Capabilities.Mobile applications can be strictly aboriginal, or web applications running in mobile browser, or a loan-blend of the two, with a web application running in a particular perspective or set of views within a native application. Appium is open of automating all three types of application, by ply different & quot; contexts & quot; in which command will be interpreted.

Contexts

A contextspecifies how the server interprets commands, and which commands are available to the exploiter. Appium currently supports two contexts: native and webview. Both of these are treat by different constituent of the system, and may still proxy command to another framework (such as webviews on Android, which are actually served by a managed ChromeDriver instance). It is significant to know what context you are in, in order to cognise how you can automatize an covering.

Native context

Native contexts refer to aboriginal applications, and to those parts of hybrid apps that are running native aspect. Commands sent to Appium in the native context execute against the twist vendor ’ s mechanization API, afford access to views and elements through gens, accessibility id, etc. As well, in this context commands to interact directly with the gimmick, to do operations such as changing the wifi connect or setting the location, can be used. These very knock-down operations are not available within the context of a webview.

In addition to native and hybrid applications, the aboriginal context can be access in a mobile web app, in order to have some of the methods merely available there. In this case it is important to understand that the commands are not bunk against the web coating running in the browser, but rather are interact with the twist and the browser itself.

Webviews

There are two types of webviews. The first is the bulk of a nomadic web application. Indeed, all mechanisation of a mobile web coating is done within a webview context, though one can switch into the aboriginal circumstance in order to conduct advantage of some of Appium ’ s features for automating the device and manage the application living cycle. The second type of webview is that component of a hybrid covering that is inside aUIAWebView(for iOS) orandroid.webkit.WebView(for Android). In the webview context the bid that can be used are the standard WebDriver commands, giving admission to component through css selectors and other web-specific locators such as link textbook.

Mobile web is essentially a specialised variation of a hybrid application. What would be the native portion of a hybrid application is the browser itself! As you automatize your application you can tread out into the native setting in order to interact with the browser or with the device itself. But when you begin automate a mobile web covering Appium automatically lead you into the webview context. If you hold a hybrid coating that get in a webview, you can have the same functionality by automatically entering into the initial webview by using theautoWebviewdesired capableness set totrue. Otherwise the automation script will need to first enter into webview before interacting with any factor.

Navigation

To move between contexts there is a method that takes the string name of the circumstance to which you require to switch. The aboriginal circumstance will receive the gens “ NATIVE_APP ” while the available webview contexts will have a name likeWEBVIEW_1(for iOS) orWEBVIEW_io.appium.android.apis(for Android, whereio.appium.android.apisis the running action). The genericWEBVIEWwill choose the first available webview. This is not necessary when automatise a mobile web browser

[codification language= ” python ”]
# switch to first available webview
driver.switch_to.context (“ WEBVIEW_1″)
[/code]

Once in the webview context Selenium commands to interact with a web application can be use.

SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.

[code language= ” python ”]
driver.find_element_by_css_selector (‘ .some_class ’)
driver.find_element_by_partial_link_text (‘ Home Page ’)
[/code]

The source at this point will be the html of the page loaded into the webview view, or the mobile web browser.

To return to the aboriginal context (which is not necessary for automating mobile web applications), you use the same bidding as used to get into the webview, but asking to shift to the native setting.

[code language= ” python ”]
# switch back to native circumstance
driver.switch_to.context (“ NATIVE_APP ”)
[/code]

Now, in the native context, if you get the rootage you will get an xml document describing all the component in the view itself, not the html still if there is html being rendered in that view!

Querying context

It is potential to get a inclination of the available contexts, and prefer the one to which to shift from those. This has the added bonus of making your tests capable of handling changes in context naming, and being the like across platforms. There will always be one (and only one) aborigine context, nominateNATIVE_CONTEXT, and zero or more webview contexts, all of which will begin withWEBVIEW.

[codification language= ” python ”]
webview = driver.contexts.last
driver.switch_to.context (webview)
[/code]

Finally, you can retrieve the current circumstance in order to get sure you are in the correct place, and to programmatically switch contexts at the right time.

[code language= ” python ”]
current_context = driver.context

# or
current_context = driver.current_context
[/code]

Multi-tabbed web browsers

If your nomadic environment supports tabbed browse, as mobile Chrome does on Android, the tabs are approachable through thewindowcommands in a webview setting, just as in background browser automation!

[codification language= ” python ”]
# enter into the webview
webview = driver.contexts.last
driver.switch_to.context (webview)

# cycle through the tabs
for tab in driver.window_handles:
driver.switch_to.window (tab)

# return to native context
driver.switch_to.context (“ NATIVE_APP ”)
[/code]

Conclusion

The main thing about switching from a native context into a webview is that subsequent dictation get proxied to a Selenium WebDriver session automating the browser which backs the webview. This makes it potential to run any webdriver commands that you would like! For instance, in a native context you can not chance an element using a css chooser, but in a webview circumstance that is perfectly reasonable. The inherent seed for the app at that point is the html of the web page be displayed!

But Appium has a number of methods that are not usable to normal webdriver. In order to take advantage of these methods one must be in a aboriginal context so that Appium itself handle the request, sooner than a proxy.

Published:
Jul 6, 2015
Share this post
Copy Share Link
LinkedIn
© 2026 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks own by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free