Skip to content

Commit

Permalink
Added final implementation of swipe
Browse files Browse the repository at this point in the history
  • Loading branch information
martingrossmann committed Jun 20, 2024
1 parent 539c777 commit bf906dd
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,6 @@ public void contextClick() {
@Override
public void swipe(int offsetX, int offsetY) {
new AppiumUtils().swipe(this.guiElementData.getGuiElement(), new Point(offsetX, offsetY));
// TouchAction touchAction = new TouchAction(appiumDriver);
//
// final TapOptions tapOption = new TapOptions().withElement(new ElementOption().withElement(webElement));
// touchAction.tap(tapOption);
// touchAction.waitAction(new WaitOptions().withDuration(Duration.ofMillis(1500)));
// touchAction.moveTo(new PointOption().withCoordinates(offsetX, offsetY));
// touchAction.waitAction(new WaitOptions().withDuration(Duration.ofMillis(1500)));
// touchAction.release();
// touchAction.perform();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
Expand All @@ -51,11 +50,32 @@
*/
public class AppiumUtils implements WebDriverManagerProvider, Loggable {

public enum Swipe {
LEFT,
RIGHT,
UP,
DOWN;
}

/**
* Run a shell command, especially at Android devices
*
* @param driver
* @param command
* @param args
* @return
*/
public String runCommand(WebDriver driver, String command, String... args) {
log().info("Shell command (native): {}, {}", command, Arrays.toString(args)); // ImmutableList does NOT work here...
return String.valueOf(JSUtils.executeScript(driver, "mobile: shell", ImmutableMap.of("command", command, "args", Lists.newArrayList(args))));
}

/**
* Helper method to start a native app from iOS system. To start installed custom apps use the default way with AppiumDriverRequest
*
* @param driver Current webdriver instance
* @param bundleId Bundle ID of native app
*/
public void launchIOSApp(WebDriver driver, String bundleId) {
log().info("Start application '{}'", bundleId);
MobileOsChecker checker = new MobileOsChecker();
Expand All @@ -74,8 +94,12 @@ public void rotate(WebDriver driver, ScreenOrientation screenOrientation) {
});
}

/**
* Perform a swipe action, start point is an element and end point is calculated based on the offset.
*/
public void swipe(UiElement uiElement, Point offset) {
AppiumDriver appiumDriver = this.getAppiumDriver(uiElement.getWebDriver());

uiElement.findWebElement(webElement -> {
Point sourceLocation = webElement.getLocation();
Dimension sourceSize = webElement.getSize();
Expand All @@ -84,17 +108,75 @@ public void swipe(UiElement uiElement, Point offset) {
int endX = centerX + offset.getX();
int endY = centerY + offset.getY();

PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
org.openqa.selenium.interactions.Sequence swipe = new Sequence(finger, 0);
this.swipeAction(appiumDriver, Duration.ofMillis(500), centerX, centerY, endX, endY);
});
}

swipe.addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), centerX, centerY));
swipe.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
swipe.addAction(finger.createPointerMove(Duration.ofMillis(600), PointerInput.Origin.viewport(), endX, endY));
swipe.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
appiumDriver.perform(List.of(swipe));
/**
* Perform a swipe action, start point is the center of an element. Possible directions are
* Swipe.LEFT, Swipe.RIGHT, Swipe.UP and Swipe.DOWN.
*/
public void swipe(UiElement startElement, Swipe direction) {
startElement.findWebElement(webElement -> {
Point sourceLocation = webElement.getLocation();
Dimension sourceSize = webElement.getSize();
int startX = sourceLocation.getX() + sourceSize.getWidth() / 2;
int startY = sourceLocation.getY() + sourceSize.getHeight() / 2;

this.swipeAction(startElement.getWebDriver(), startX, startY, direction);
});
}

/**
* Perform a swipe action. Start point is the center of the screen. Possible directions are
* Swipe.LEFT, Swipe.RIGHT, Swipe.UP and Swipe.DOWN.
*/
public void swipe(WebDriver driver, Swipe direction) {
Dimension screenDim = driver.manage().window().getSize();
int startX = screenDim.getWidth() / 2;
int startY = screenDim.getHeight() / 2;
this.swipeAction(driver, startX, startY, direction);
}

private void swipeAction(WebDriver driver, int startX, int startY, Swipe direction) {
AppiumDriver appiumDriver = this.getAppiumDriver(driver);
final Duration intensity = Duration.ofMillis(500);
// Length of swipe action according screen resolution
final double screenMultiplier = 0.4;

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

int endX = startX;
int endY = startY;
switch (direction) {
case LEFT:
endX = Math.max(startX - diffX, 1);
break;
case RIGHT:
endX = Math.max(startX + diffX, screenDim.getWidth() - 1);
break;
case UP:
endY = Math.max(startY - diffY, 1);
break;
case DOWN:
endY = Math.max(startY + diffY, screenDim.getHeight() - 1);
}

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

private void swipeAction(AppiumDriver driver, Duration intensity, int startX, int startY, int endX, int endY) {
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence swipe = new Sequence(finger, 1);
swipe.addAction(finger.createPointerMove(intensity, PointerInput.Origin.viewport(), startX, startY));
swipe.addAction(finger.createPointerDown(0));
swipe.addAction(finger.createPointerMove(intensity, PointerInput.Origin.viewport(), endX, endY));
swipe.addAction(finger.createPointerUp(0));
driver.perform(List.of(swipe));
}

/**
* Switch the Appium Context if available
*/
Expand Down Expand Up @@ -127,6 +209,9 @@ public void switchContext(WebDriver driver, AppiumContext desiredContext) {

}

/**
* Unwrap the current WebDriver instance and returns a native AppiumDriver instance.
*/
public AppiumDriver getAppiumDriver(WebDriver webDriver) {
return WEB_DRIVER_MANAGER.unwrapWebDriver(webDriver, AppiumDriver.class)
.orElseThrow(() -> new RuntimeException("Current Webdriver is not an Appium driver."));
Expand Down

0 comments on commit bf906dd

Please sign in to comment.