Skip to content

Commit

Permalink
Updated swipe actions
Browse files Browse the repository at this point in the history
  • Loading branch information
martingrossmann committed Aug 16, 2024
1 parent 59d4d93 commit 69a4cd3
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@ public enum Swipe {
// Relative start and end positions to the screen dimension of standard swipe actions
// for example scroll down:
// - 0.2 * screenHeight -> start y (SCREEN_SWIPE_START)
// - 0.5 * screenHeight -> end y (SCREEN_MULTIPLIER)
// - 0.8 * screenHeight -> end y (SCREEN_SWIPE_END)
// - x is center of screen
// for example scroll left:
// - 0.8 * screenWidth -> start x (SCREEN_SWIPE_END)
// - 0.5 * screenWidth -> end x (SCREEN_MULTIPLIER)
// - 0.2 * screenWidth -> end x (SCREEN_SWIPE_START)
// - y is center of screen
private static final double SCREEN_SWIPE_START = 0.2;
private static final double SCREEN_SWIPE_END = 0.8;
private static final double SCREEN_MULTIPLIER = 0.5;

/**
* Run a shell command, especially at Android devices
Expand Down Expand Up @@ -156,31 +155,33 @@ public void swipe(WebDriver driver, Swipe direction) {
private void swipeAction(WebDriver driver, Swipe direction, Point startPoint) {
AppiumDriver appiumDriver = this.getAppiumDriver(driver);
final Duration intensity = Duration.ofMillis(500);
// Length of swipe action according screen resolution


Dimension screenDim = appiumDriver.manage().window().getSize();
final int diffX = (int) (screenDim.getWidth() * SCREEN_MULTIPLIER);
final int diffY = (int) (screenDim.getHeight() * SCREEN_MULTIPLIER);

int startX = startPoint.getX();
int startY = startPoint.getY();
int endX = startX;
int endY = startY;
switch (direction) {
case LEFT:
// move finger from left to right
startX = (int) (screenDim.getWidth() * SCREEN_SWIPE_END);
endX = (int) (screenDim.getWidth() * SCREEN_SWIPE_START);
// endX = Math.max(startX - diffX, 1);
break;
case RIGHT:
endX = Math.max(startX + diffX, screenDim.getWidth() - 1);
// move finger from right to left
startX = (int) (screenDim.getWidth() * SCREEN_SWIPE_START);
endX = (int) (screenDim.getWidth() * SCREEN_SWIPE_END);
break;
case UP:
endY = Math.max(startY - diffY, 1);
// move finger from top to bottom
startY = (int) (screenDim.getHeight() * SCREEN_SWIPE_START);
endY = (int) (screenDim.getHeight() * SCREEN_SWIPE_END);
break;
case DOWN:
endY = Math.max(startY + diffY, screenDim.getHeight() - 1);
// move finger from bottom to top
startY = (int) (screenDim.getHeight() * SCREEN_SWIPE_END);
endY = (int) (screenDim.getHeight() * SCREEN_SWIPE_START);
}

this.swipeAction(appiumDriver, intensity, startX, startY, endX, endY);
Expand Down

0 comments on commit 69a4cd3

Please sign in to comment.