When automating hybrid or mobile web applications, switching between native app views and web views is essential. This cheat sheet covers the most important Appium commands for managing contexts, browser windows, navigation, cookies, storage, and frames. With these ready-to-use Java examples, you can seamlessly control web-based content inside your mobile automation scripts.
|
Command / Action |
Description |
Example / Notes |
|
Get Contexts |
Lists available contexts (NATIVE_APP, WEBVIEW) |
driver.getContextHandles() |
|
Switch Context |
Switch between native and web context |
driver.context("WEBVIEW_com.example") |
|
Get Current Context |
Returns the active context |
driver.getContext() |
|
Get Window Handles |
Returns all open browser windows/tabs handles. |
driver.getWindowHandles() |
|
Switch to Window |
Switch to a specific window/tab. |
driver.switchTo().window(handle) |
|
Get Current Window Handle |
Returns the active window handle. |
driver.getWindowHandle() |
|
Set Window Size |
Changes browser window size. |
driver.manage(). |
|
Maximize Window |
Maximizes the browser window. |
driver.manage(). |
|
Minimize Window |
Minimizes the browser window. |
driver.manage(). |
|
Navigate To |
Opens a given URL. |
driver.get("https://example.com") |
|
Back |
Navigates back in browser history. |
driver.navigate().back() |
|
Forward |
Navigates forward in browser history. |
driver.navigate().forward() |
|
Refresh |
Reloads the current page. |
driver.navigate().refresh() |
|
Get Title |
Gets the current page title. |
driver.getTitle() |
|
Get URL |
Gets the current page URL. |
driver.getCurrentUrl() |
|
Get Cookies |
Retrieves all cookies. |
driver.manage().getCookies() |
|
Add Cookie |
Adds a new cookie. |
driver.manage(). |
|
Delete Cookie |
Deletes a specific cookie. |
driver.manage(). |
|
Delete All Cookies |
Clears all browser cookies. |
driver.manage().deleteAllCookies() |
|
Local Storage (Get) |
Retrieves value from local storage. |
driver. |
|
Local Storage (Set) |
Sets value in local storage. |
driver. |
|
Session Storage (Get) |
Retrieves value from session storage. |
driver.executeScript("return window.sessionStorage.getItem('key')") |
|
Session Storage (Set) |
Sets value in session storage. |
driver. |
|
Switch to Frame (by index) |
Switches to frame by index. |
driver.switchTo().frame(0) |
|
Switch to Frame (by name/id) |
Switches to frame by name or id. |
driver.switchTo().frame("frameName") |
|
Switch to Frame (by element) |
Switches to frame using WebElement. |
driver.switchTo(). |
|
Switch to Parent Frame |
Switches to the parent frame. |
driver.switchTo(). |