Reputation:
I used below code in a work, where I conducted 'Swipe(direction)' actions on both Android and iOS devices, to test a native mobile app.
Now the AUT is a website rendered in an Android mobile device on a cloud device farm. I need to swipe-up/down, and swipe-left/right for bringing Web Controls which are not in the view port due to longer display.
My ask is - are there an equivalent class(s) (preferably an example) in Selenium v4.12.1, which I am using to test the MobileWeb situation, I am dealing with right now ?
/**
* Swipe the mobile screen upwards so that hidden button comes into visible zone
* @param dir
*/
public void swipeScreen(Direction dir) {
System.out.println("swipeScreen(): dir: '" + dir + "'");
// Animation default time:
// - Android: 300 ms
// - iOS: 200 ms
// final value depends on your app and could be greater
final int ANIMATION_TIME = 200; // ms
final int PRESS_TIME = 200; // ms
int edgeBorder = 10; // better avoid edges
PointOption pointOptionStart, pointOptionEnd;
// init screen variables
Dimension dims = driver.manage().window().getSize();
// init start point = center of screen
pointOptionStart = PointOption.point(dims.width / 2, dims.height / 2);
switch (dir) {
case DOWN: // center of footer
pointOptionEnd = PointOption.point(dims.width / 2, dims.height - edgeBorder);
break;
case UP: // center of header
pointOptionEnd = PointOption.point(dims.width / 2, edgeBorder);
System.out.println("pointOptionEnd UP derived");
break;
case LEFT: // center of left side
pointOptionEnd = PointOption.point(edgeBorder, dims.height / 2);
break;
case RIGHT: // center of right side
pointOptionEnd = PointOption.point(dims.width - edgeBorder, dims.height / 2);
break;
default:
throw new IllegalArgumentException("swipeScreen(): dir: '" + dir + "' NOT supported");
}
// execute swipe using TouchAction
try {
// First change to 'NATIVE_APP' context
shiftContext("NATIVE_APP");
new TouchAction((PerformsTouchActions) driver)
.press(pointOptionStart)
// a bit more reliable when we add small wait
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(PRESS_TIME)))
.moveTo(pointOptionEnd)
.release().perform();
// Shift the context back to original
shiftContext("CHROMIUM");
} catch (Exception e) {
System.err.println("swipeScreen(): TouchAction FAILED\n" + e.getMessage());
return;
}
// always allow swipe action to complete
try {
Thread.sleep(ANIMATION_TIME);
} catch (InterruptedException e) {
// ignore
}
System.out.println("Swipe Action complete");
}
// public enumeration.
public enum Direction {
UP,
DOWN,
LEFT,
RIGHT;
}
Upvotes: 0
Views: 28