Hybrid apps
Many apps show some screens as web pages in a WebView (Android) or WKWebView (iOS). A Shiplight test can work in both parts of the app: native steps on the app's own UI, and page steps on the DOM of a WebView page. The switch_context action moves the test between them.
Switch into a WebView and back
A test starts in the native context. switch_context with context: webview moves the following steps into the app's WebView page. context: native moves them back.
goal: Type into the WebView page, follow its link, and come back to the native list
platform: android
app:
path: ./build/ApiDemos-debug.apk
identifier: io.appium.android.apis
statements:
- intent: Open the Views category
action: click
locator: { kind: accessibility_id, value: Views }
- intent: Scroll to the WebView demo
action: scroll_to_element
locator: { kind: uiautomator, value: 'new UiSelector().text("WebView")' }
- intent: Open the WebView demo
action: click
locator: { kind: uiautomator, value: 'new UiSelector().text("WebView")' }
- intent: Switch into the demo's WebView page
action: switch_context
context: webview
timeout_seconds: 30
- intent: Type into the text box
action: input_text
locator: { kind: css, value: "#i_am_a_textbox" }
text: Hello from Shiplight
- intent: Check the text box holds the typed text
action: assert_text
locator: { kind: xpath, value: "//input[@id='i_am_a_textbox']" }
equals: Hello from Shiplight
- intent: Follow the link to the linked page
action: click
locator: { kind: css, value: 'a[href="linked.html"]' }
- intent: Check the linked page is shown
action: assert_text
locator: { kind: css, value: body }
contains: I am some other page content
- intent: Switch back to the app's native UI
action: switch_context
context: native
- intent: Go back to the Views list
action: back
- intent: Check the Views list shows the WebView demo again
action: assert_visible
locator: { kind: uiautomator, value: 'new UiSelector().text("WebView")' }The same steps work on iOS. This test fills in a form in a WKWebView that sits under a native header:
goal: Complete checkout in a mixed native and WebView screen
platform: ios
app:
path: ./build/MyApp.app
identifier: com.example.myapp
statements:
- intent: Open the checkout screen
action: click
locator: { kind: accessibility_id, value: nav.checkout }
- intent: Switch into the checkout page
action: switch_context
context: webview
title: Checkout
- intent: Type the email address
action: input_text
locator: { kind: css, value: '#email' }
text: buyer@example.com
- intent: Submit checkout
action: click
locator: { kind: css, value: '#submit' }
- intent: Check the receipt
action: assert_text
locator: { kind: css, value: '#receipt' }
contains: 'Confirmed: buyer@example.com'
- intent: Return to the native controls
action: switch_context
context: native
- intent: Check the native header is still shown
action: assert_visible
locator: { kind: accessibility_id, value: checkout.header }Choose the page
With only context: webview, the test switches to the app's own WebView page. When the app has more than one page open, select one with an exact match:
| Parameter | Description |
|---|---|
title | The page's exact title |
url | The page's exact URL |
name | The exact WebView context name, such as WEBVIEW_com.example.shop |
timeout_seconds | How long to wait for the WebView to appear and attach. Default: 30. 0 tries once |
title, url, and name apply only with context: webview. Shiplight only switches to pages that belong to the app under test.
What works in each context
- Locators. In the
webviewcontext, locators arecssandxpath, and they address the page's DOM. Native locator kinds such asaccessibility_idwork only in thenativecontext. - Actions. The native actions work in both contexts, so a test can tap, type, swipe, rotate the device, or press Back while in a WebView. Back is the device's Back key; to go back in the page's history, use
go_back. - Page actions. These work only in the
webviewcontext:select_dropdown_option,get_dropdown_options,scroll_to_text,scroll_on_element,wait_for_page_ready,go_back, andreload_page.select_dropdown_optionsets a<select>without opening the native picker. - AI steps.
intent:,VERIFY:,WAIT_UNTIL:, andai_extractwork in both contexts and need a Shiplight API token. - App lifecycle.
launch_app,terminate_app, andreset_appreturn the test to thenativecontext. Switch into the WebView again afterward. - Page dialogs. A JavaScript
alert,confirm, orpromptopened by the page can't be answered withaccept_alertordismiss_alert. Those actions answer native dialogs only.
Make your WebViews inspectable
Appium can attach only to WebViews that allow debugging. Enable it in the app builds you test:
- Android: call
WebView.setWebContentsDebuggingEnabled(true). Debug builds often enable it already. - iOS 16.4 and later: set
isInspectable = trueon eachWKWebView.
Android Chromedriver
On Android, Appium controls WebView pages through a Chromedriver that matches the version of the WebView on the device. Shiplight does not download Chromedriver. Download the matching version, put it in a folder, and pass that folder to Appium with the appium:chromedriverExecutableDir capability:
{
"devices": {
"emulator": {
"type": "local",
"appiumUrl": "http://127.0.0.1:4723",
"capabilities": {
"appium:chromedriverExecutableDir": { "$env": "CHROMEDRIVER_DIR" }
}
}
}
}To find the WebView version on a device, run adb shell dumpsys package com.google.android.webview | grep versionName (or com.android.webview on AOSP images). If no matching Chromedriver is available, the switch_context step fails and says so.
iOS needs no extra driver: the XCUITest driver attaches to WKWebView pages through Safari's Web Inspector.
Related
- Write mobile tests: native locators and actions
- Author with your agent: your agent can switch contexts while it explores