UiAutomator2Options options = new UiAutomator2Options();
options.setPlatformName("Android");
options.setDeviceName("Pixel_4_Emulator");
options.setAutomationName("UiAutomator2");
options.setApp("/path/to/your/app.apk");
URL appiumServerUrl = new URL("http://localhost:4723");
AndroidDriver driver = new AndroidDriver(appiumServerUrl, options);
// 1. Get all available contexts
Set<String> contexts = ((AndroidDriver) driver).getContextHandles();
// 2. Loop through contexts to find the webview
for(String context : contexts){
if(context.contains("WEBVIEW")){
// 3. Switch to the Webview context
driver.context(context);
break;
}
}
// 4. Perform web automation here...
Assert.assertEquals(driver.findElement(By.tagName("h1")).getText(),"Hello, TAS!");
// 5. Switch back to Native context when done
driver.context("NATIVE_APP");
driver.quit();
AppiumOptions options = new AppiumOptions();
options.PlatformName = "Android";
options.DeviceName = "Pixel_4_Emulator";
options.AutomationName = "UiAutomator2";
options.App = "/path/to/your/app.apk";
var serverUri = new Uri("http://localhost:4723");
AndroidDriver driver = new AndroidDriver(serverUri, options);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(90);
// 1. Get all available contexts
var allContexts = driver.Contexts;
// 2. Loop through contexts to find the webview
foreach (string context in allContexts)
{
if (context.Contains("WEBVIEW"))
{
// 3. Switch to the Webview context
driver.Context = context;
break;
}
}
// 4. Perform web automation here...
Assert.That(driver.FindElement(By.TagName("h1")).Text, Is.EqualTo("Hello, TAS!"));
// 5. Switch back to Native context when done
driver.Context = "NATIVE_APP";
driver.Quit();
options = UiAutomator2Options()
options.platform_name = "Android"
options.platformVersion = '15'
options.device_name = "YourDeviceName"
options.app_activity = ".MainActivity"
options.App = "/path/to/your/app.apk"
driver = webdriver.Remote('http://localhost:4723', options=options)
driver.implicitly_wait(30)
# 1. Get all available contexts
contexts = driver.contexts
# 2. Loop through contexts to find the webview
for context in contexts:
if "WEBVIEW" in context:
# 3. Switch to the Webview context
driver.switch_to.context(context)
break
# 4. Perform web automation here...
assert driver.find_element(By.TAG_NAME, "h1").text, "Hello, TAS!"
# 5. Switch back to Native context when done
driver.switch_to.context("NATIVE_APP")
driver.quit()
// Define Desired Capabilities
const capabilities =
{
platformName: 'Android',
'appium:automationName': 'UiAutomator2',
'appium:deviceName': 'Pixel 7 API 34',
'appium:platformVersion':'14',
'appium:app': '/path/to/my.app',
'appium:appActivity': '.MainActivity',
};
// Define Server Configuration
const wdOpts = {
hostname: 'localhost',
port: 4723,
logLevel: 'info',
capabilities,
};
const driver = await remote({capabilities: wdOpts});
// 1. Get all available contexts
let contexts = await driver.getContexts();
// 2. Loop through contexts to find the webview
for(let context of contexts)
{
const webViewContext = contexts.find(context => context.includes('WEBVIEW'));
if(webViewContext)
{
// 3. Switch to the webview context
await driver.switchContext(webViewContext);
console.log("Switched to context: " + webViewContext);
break;
}
}
// 4. Perform web automation here...
let headerText = await driver.$("h1").getText();
// Assert text content
expect(headerText).toBe('"Hello, TAS!"');
// 5. Switch back to Native context when done
await driver.switchContext('NATIVE_APP');
await driver.deleteSession();
val options = UiAutomator2Options()
options.setPlatformName("Android")
options.setDeviceName("Pixel_4_Emulator") // Replace with the name of your device/emulator
options.setAutomationName("UiAutomator2")
options.setApp("/path/to/your/app.apk") // Path to your APK file
// Initialize the AndroidDriver
val serverUrl = URI.create("http://localhost:4723").toURL()
val driver: RemoteWebDriver = AndroidDriver(serverUrl, options)
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
// 1. Get all available contexts
val contexts = (driver as AndroidDriver).getContextHandles()
// 2. Loop through contexts to find the webview
for (context in contexts) {
if (context.contains("WEBVIEW")) {
// 3. Switch to the Webview context
driver.context(context)
break
}
}
// 4. Perform web automation here...
assertEquals(driver.findElement(By.tagName("h1")).getText(), "Hello, TAS!")
// 5. Switch back to Native context when done
driver.context("NATIVE_APP")
driver.quit()