Device Posture - Automation Commands
Description
Foldable devices can change their screen size and layout while an application is running. The device posture commands let you fold and unfold a supported Android foldable device from your Appium test, so you can validate how your application responds to each posture during a test run.
| Command | Description |
|---|---|
mobile: setDevicePosture | Sets the device to the specified posture: folded or continuous (unfolded). |
mobile: clearDevicePosture | Clears the posture set by setDevicePosture and returns the device to its current physical (hardware) state. |
These commands are supported only for Android foldable devices in Appium automation.
Parameters
setDevicePosture
| Parameter Name | Value | Description |
|---|---|---|
posture | String | The posture to apply to the device. You can pass one of the following values:folded - Folds the device.continuous - Unfolds the device so that the display is flat and continuous. |
clearDevicePosture
This command does not take any parameters.
Code Example
The device posture commands can be executed using the executeScript function:
- Java
- Python
- C#
- JavaScript
- Ruby
// Fold the device
driver.executeScript("mobile: setDevicePosture", Map.of("posture", "folded"));
// Unfold the device
driver.executeScript("mobile: setDevicePosture", Map.of("posture", "continuous"));
// Return the device to its hardware state
driver.executeScript("mobile: clearDevicePosture");
# Fold the device
self.driver.execute_script("mobile: setDevicePosture", {"posture": "folded"})
# Unfold the device
self.driver.execute_script("mobile: setDevicePosture", {"posture": "continuous"})
# Return the device to its hardware state
self.driver.execute_script("mobile: clearDevicePosture")
// Fold the device
driver.ExecuteScript("mobile: setDevicePosture", new Dictionary<string, object> { { "posture", "folded" } });
// Unfold the device
driver.ExecuteScript("mobile: setDevicePosture", new Dictionary<string, object> { { "posture", "continuous" } });
// Return the device to its hardware state
driver.ExecuteScript("mobile: clearDevicePosture");
// Fold the device
await driver.executeScript("mobile: setDevicePosture", [{ posture: "folded" }]);
// Unfold the device
await driver.executeScript("mobile: setDevicePosture", [{ posture: "continuous" }]);
// Return the device to its hardware state
await driver.executeScript("mobile: clearDevicePosture", []);
# Fold the device
driver.execute_script("mobile: setDevicePosture", { posture: "folded" })
# Unfold the device
driver.execute_script("mobile: setDevicePosture", { posture: "continuous" })
# Return the device to its hardware state
driver.execute_script("mobile: clearDevicePosture")
Practical Example
In a typical scenario, you can verify that your application adapts its layout when the device is folded and unfolded, and then return the device to its hardware state at the end of the test:
@Test
public void validateFoldableLayout() {
// Unfold the device and validate the expanded layout
driver.executeScript("mobile: setDevicePosture", Map.of("posture", "continuous"));
// ... validate the unfolded layout
// Fold the device and validate the compact layout
driver.executeScript("mobile: setDevicePosture", Map.of("posture", "folded"));
// ... validate the folded layout
}
@AfterMethod
public void tearDown() {
// Return the device to its hardware state before ending the session
driver.executeScript("mobile: clearDevicePosture");
driver.quit();
}
Reports and Video Recording
Each posture change is added as a step in the test report, so you can see when the device was folded or unfolded during the test. The video recording of the test also reflects the change in screen size when the device posture changes.