Overview
Concrete value object for representing a three-dimensional device rotation provided by the Selenium WebDriver library.
Description
DeviceRotation is a value object that represents the three-dimensional orientation of a device using rotation angles around the x, y, and z axes. Each axis value must be a non-negative integer less than 360 degrees (0 <= deg < 360). The class validates these constraints in both constructors, throwing IllegalArgumentException for invalid values. It supports construction from three integers or from a Map<String, Number> with keys "x", "y", and "z". The class provides a parameters() method that returns the rotation as an immutable map, and correctly implements equals() and hashCode() for value semantics.
Usage
Use DeviceRotation when working with mobile device orientation in Selenium. It is used with the Rotatable interface to rotate a device to a specific orientation (e.g., portrait, landscape left, landscape right). This is primarily relevant for mobile testing scenarios with Appium or similar mobile WebDriver implementations.
Code Reference
Source Location
Signature
public class DeviceRotation {
private int x = 0;
private int y = 0;
private int z = 0;
public DeviceRotation(int x, int y, int z)
public DeviceRotation(Map<String, Number> map)
public int getX()
public int getY()
public int getZ()
public Map<String, Integer> parameters()
@Override public boolean equals(Object o)
@Override public int hashCode()
}
Import
import org.openqa.selenium.DeviceRotation;
I/O Contract
| Constructor |
Parameters |
Description
|
DeviceRotation(int, int, int) |
x, y, z (each 0-359) |
Creates a rotation from three axis angles in degrees
|
DeviceRotation(Map<String, Number>) |
Map with keys "x", "y", "z" |
Creates a rotation from a map of axis values
|
| Method |
Return Type |
Description
|
getX() |
int |
Returns rotation around the x axis in degrees
|
getY() |
int |
Returns rotation around the y axis in degrees
|
getZ() |
int |
Returns rotation around the z axis in degrees
|
parameters() |
Map<String, Integer> |
Returns all axis values as an immutable map with keys "x", "y", "z"
|
equals(Object) |
boolean |
Returns true if x, y, and z all match
|
hashCode() |
int |
Hash based on x, y, and z values
|
| Validation |
Constraint |
Exception
|
| Negative values |
All axes must be >= 0 |
IllegalArgumentException
|
| Values >= 360 |
All axes must be < 360 |
IllegalArgumentException
|
| Null or missing map keys |
Map must contain "x", "y", and "z" |
IllegalArgumentException
|
Usage Examples
// Rotate device to "Landscape Right" (90 degrees around z axis)
DeviceRotation landscapeRight = new DeviceRotation(0, 0, 90);
// Rotate device to "Landscape Left" (270 degrees around z axis)
DeviceRotation landscapeLeft = new DeviceRotation(0, 0, 270);
// Default portrait orientation
DeviceRotation portrait = new DeviceRotation(0, 0, 0);
// Create from a Map
Map<String, Number> rotationMap = Map.of("x", 0, "y", 0, "z", 90);
DeviceRotation fromMap = new DeviceRotation(rotationMap);
// Access individual axis values
int xRotation = landscapeRight.getX(); // 0
int yRotation = landscapeRight.getY(); // 0
int zRotation = landscapeRight.getZ(); // 90
// Get all parameters as a map (useful for serialization)
Map<String, Integer> params = landscapeRight.parameters();
// {x=0, y=0, z=90}
// Use with Rotatable interface (mobile testing)
((Rotatable) driver).rotate(new DeviceRotation(0, 0, 90));
// Compare two DeviceRotation objects
DeviceRotation a = new DeviceRotation(0, 0, 90);
DeviceRotation b = new DeviceRotation(0, 0, 90);
boolean same = a.equals(b); // true
Related Pages