Skip to content

Commit

Permalink
Added draft for swipe
Browse files Browse the repository at this point in the history
  • Loading branch information
martingrossmann committed Jun 12, 2024
1 parent 05e4b8c commit 539c777
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import eu.tsystems.mms.tic.testframework.pageobjects.internal.core.GuiElementCore;
import eu.tsystems.mms.tic.testframework.pageobjects.internal.core.GuiElementData;
import eu.tsystems.mms.tic.testframework.testing.WebDriverManagerProvider;
import eu.tsystems.mms.tic.testframework.utils.AppiumUtils;
import eu.tsystems.mms.tic.testframework.utils.ExecutionUtils;
import io.appium.java_client.AppiumDriver;
import org.apache.commons.lang3.NotImplementedException;
Expand Down Expand Up @@ -113,11 +114,9 @@ public void contextClick() {
// });
// }

// TODO: Migrate to W3C actions
@Override
public void swipe(int offsetX, int offsetY) {
throw new NotImplementedException("Implement me with W3C actions");
// this.findWebElement(webElement -> {
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));
Expand All @@ -127,7 +126,6 @@ public void swipe(int offsetX, int 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 @@ -25,15 +25,22 @@
import eu.tsystems.mms.tic.testframework.appium.AppiumContext;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.mobile.driver.MobileOsChecker;
import eu.tsystems.mms.tic.testframework.pageobjects.UiElement;
import eu.tsystems.mms.tic.testframework.testing.WebDriverManagerProvider;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.remote.SupportsContextSwitching;
import io.appium.java_client.remote.SupportsRotation;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Platform;
import org.openqa.selenium.Point;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -67,16 +74,34 @@ public void rotate(WebDriver driver, ScreenOrientation screenOrientation) {
});
}

public void swipe(UiElement uiElement, Point offset) {
AppiumDriver appiumDriver = this.getAppiumDriver(uiElement.getWebDriver());
uiElement.findWebElement(webElement -> {
Point sourceLocation = webElement.getLocation();
Dimension sourceSize = webElement.getSize();
int centerX = sourceLocation.getX() + sourceSize.getWidth() / 2;
int centerY = sourceLocation.getY() + sourceSize.getHeight() / 2;
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);

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));
});
}

/**
* Switch the Appium Context if available
*/
public void switchContext(WebDriver driver, AppiumContext desiredContext) {
Optional<AppiumDriver> appiumDriver = WEB_DRIVER_MANAGER.unwrapWebDriver(driver, AppiumDriver.class);
if (appiumDriver.isEmpty()) {
throw new RuntimeException("Current Webdriver is not an Appium driver.");
}
AppiumDriver appiumDriver = this.getAppiumDriver(driver);

SupportsContextSwitching contextSwitchingDriver = (SupportsContextSwitching) appiumDriver.get();
SupportsContextSwitching contextSwitchingDriver = (SupportsContextSwitching) appiumDriver;

String currentContext = contextSwitchingDriver.getContext();
AppiumContext parsedInitialContext = AppiumContext.parse(currentContext);
Expand All @@ -102,4 +127,9 @@ public void switchContext(WebDriver driver, AppiumContext desiredContext) {

}

public AppiumDriver getAppiumDriver(WebDriver webDriver) {
return WEB_DRIVER_MANAGER.unwrapWebDriver(webDriver, AppiumDriver.class)
.orElseThrow(() -> new RuntimeException("Current Webdriver is not an Appium driver."));
}

}

0 comments on commit 539c777

Please sign in to comment.