This is reposted from the appium-discuss google group at Jonah’s request, slightly modified to make more sense out of context, and with a simple code snippet. All examples are in java/junit terminology, but they should be usable in your language of choice with a bit of translating.
First, the AppiumDriver has a getOrientation() method that will return a ScreenOrientation object. The ScreenOrientation object has a value() method that returns either “portrait” or “landscape”. So you can pretty easily tell what your device’s current orientation is by calling driver.getOrientation().value().
You should also be able to call driver.rotate(“LANDSCAPE”) or driver.rotate(“PORTRAIT”) to switch orientations (although I haven’t actually used that functionality in any of my tests to date, so I have no idea if it actually works).
For element-relative swiping, you can locate whatever WebElement you’re trying to interact with, and then call element.getLocation().getX() and element.getLocation().getY() to get the upper-left coordinates of the element. element.getSize().getWidth() and element.getSize().getHeight() will allow you to find the width and height of the element, and from there it’s pretty easy to figure out the x and y for your relative swipes. You will probably want to do a little bit of “safety padding” on those values just to make sure that you’re not clicking on the absolute right or left edge of the element you’re trying to interact with.
So a simple “swipe left-to-right on some WebElement, or right-to-left if we’re in landscape layout” example:
public void swipeElementExample(WebElement el) {
String orientation = driver.getOrientation().value();
// get the X coordinate of the upper left corner of the element, then add the element's width to get the rightmost X value of the element
int leftX = el.getLocation().getX();
int rightX = leftX + el.getSize().getWidth();
// get the Y coordinate of the upper left corner of the element, then subtract the height to get the lowest Y value of the element
int upperY = el.getLocation().getY();
int lowerY = upperY - el.getSize().getHeight();
int middleY = (upperY - lowerY) / 2;
if (orientation.equals("portrait")) {
// Swipe from just inside the left-middle to just inside the right-middle of the element over 500ms
driver.swipe(leftX + 5, middleY, rightX - 5, middleY, 500);
}
else if (orientation.equals("landscape")) {
// Swipe from just inside the right-middle to just inside the left-middle of the element over 500ms
driver.swipe(rightX - 5, middleY, leftX + 5, middleY, 500);
}
}
Not crazy complex, and not particularly efficient, but it should be enough to get you started.
-Hank