|
| 1 | +package com.testsigma.addons.android; |
| 2 | + |
| 3 | +import com.testsigma.sdk.AndroidAction; |
| 4 | +import com.testsigma.sdk.ApplicationType; |
| 5 | +import com.testsigma.sdk.Element; |
| 6 | +import com.testsigma.sdk.Result; |
| 7 | +import com.testsigma.sdk.annotation.Action; |
| 8 | +import com.testsigma.sdk.annotation.TestData; |
| 9 | +import io.appium.java_client.AppiumDriver; |
| 10 | +import lombok.Data; |
| 11 | +import org.openqa.selenium.NoSuchElementException; |
| 12 | +import org.openqa.selenium.Rectangle; |
| 13 | +import org.openqa.selenium.interactions.PointerInput; |
| 14 | +import org.openqa.selenium.interactions.Sequence; |
| 15 | + |
| 16 | +import java.time.Duration; |
| 17 | +import java.util.Collections; |
| 18 | + |
| 19 | +import static org.openqa.selenium.interactions.PointerInput.Kind.TOUCH; |
| 20 | +import static org.openqa.selenium.interactions.PointerInput.MouseButton.LEFT; |
| 21 | +import static org.openqa.selenium.interactions.PointerInput.Origin.viewport; |
| 22 | + |
| 23 | +@Data |
| 24 | +@Action(actionText = "Swipe inside the element element-name in direction direction for duration seconds", |
| 25 | + description = "Scrolls inside the given element in the specified direction. The swipe gesture takes the" + |
| 26 | + " given number of seconds to complete. Use longer durations for slower, more deliberate scrolls.", |
| 27 | + applicationType = ApplicationType.ANDROID, |
| 28 | + displayName = "Scroll inside element with direction and duration", |
| 29 | + useCustomScreenshot = false) |
| 30 | +public class ScrollInsideElementWithDuration extends AndroidAction { |
| 31 | + |
| 32 | + @com.testsigma.sdk.annotation.Element(reference = "element-name") |
| 33 | + private Element elementName; |
| 34 | + |
| 35 | + @TestData(reference = "direction", |
| 36 | + allowedValues = { |
| 37 | + "LEFT TO RIGHT", |
| 38 | + "RIGHT TO LEFT", |
| 39 | + "MIDDLE TO LEFT", |
| 40 | + "MIDDLE TO RIGHT", |
| 41 | + "LEFT TO MIDDLE", |
| 42 | + "RIGHT TO MIDDLE", |
| 43 | + "TOP TO BOTTOM", |
| 44 | + "BOTTOM TO TOP", |
| 45 | + "TOP TO MIDDLE", |
| 46 | + "MIDDLE TO TOP", |
| 47 | + "BOTTOM TO MIDDLE", |
| 48 | + "MIDDLE TO BOTTOM" |
| 49 | + }) |
| 50 | + private com.testsigma.sdk.TestData direction; |
| 51 | + |
| 52 | + @TestData(reference = "duration") |
| 53 | + private com.testsigma.sdk.TestData duration; |
| 54 | + |
| 55 | + @Override |
| 56 | + protected Result execute() throws NoSuchElementException { |
| 57 | + logger.info("Starting scroll inside element with direction and duration"); |
| 58 | + |
| 59 | + int durationSeconds; |
| 60 | + try { |
| 61 | + durationSeconds = Integer.parseInt(duration.getValue().toString().trim()); |
| 62 | + } catch (NumberFormatException e) { |
| 63 | + setErrorMessage("Invalid duration: '" + duration.getValue() + "'. Please provide a whole number of seconds."); |
| 64 | + return Result.FAILED; |
| 65 | + } |
| 66 | + |
| 67 | + if (durationSeconds <= 0) { |
| 68 | + setErrorMessage("Duration must be greater than 0, but got: " + durationSeconds); |
| 69 | + return Result.FAILED; |
| 70 | + } |
| 71 | + |
| 72 | + String swipeDirection = direction.getValue().toString().trim().toUpperCase(); |
| 73 | + |
| 74 | + try { |
| 75 | + org.openqa.selenium.WebElement webElement = elementName.getElement(); |
| 76 | + Rectangle rect = webElement.getRect(); |
| 77 | + |
| 78 | + int centerX = rect.x + rect.width / 2; |
| 79 | + int centerY = rect.y + rect.height / 2; |
| 80 | + int left = rect.x; |
| 81 | + int right = rect.x + rect.width; |
| 82 | + int top = rect.y; |
| 83 | + int bottom = rect.y + rect.height; |
| 84 | + |
| 85 | + logger.info(String.format("Element rect: x=%d y=%d w=%d h=%d", rect.x, rect.y, rect.width, rect.height)); |
| 86 | + |
| 87 | + int[] coords = resolveCoordinates(swipeDirection, left, right, top, bottom, centerX, centerY); |
| 88 | + if (coords == null) { |
| 89 | + setErrorMessage("Unsupported direction: " + swipeDirection); |
| 90 | + return Result.FAILED; |
| 91 | + } |
| 92 | + |
| 93 | + int startX = coords[0]; |
| 94 | + int startY = coords[1]; |
| 95 | + int endX = coords[2]; |
| 96 | + int endY = coords[3]; |
| 97 | + |
| 98 | + logger.info(String.format("Swipe [%s]: (%d,%d) -> (%d,%d) over %ds", |
| 99 | + swipeDirection, startX, startY, endX, endY, durationSeconds)); |
| 100 | + |
| 101 | + AppiumDriver appiumDriver = (AppiumDriver) this.driver; |
| 102 | + PointerInput finger = new PointerInput(TOUCH, "finger"); |
| 103 | + Sequence swipe = new Sequence(finger, 1) |
| 104 | + .addAction(finger.createPointerMove(Duration.ofMillis(0), viewport(), startX, startY)) |
| 105 | + .addAction(finger.createPointerDown(LEFT.asArg())) |
| 106 | + .addAction(finger.createPointerMove(Duration.ofSeconds(durationSeconds), viewport(), endX, endY)) |
| 107 | + .addAction(finger.createPointerUp(LEFT.asArg())); |
| 108 | + |
| 109 | + appiumDriver.perform(Collections.singletonList(swipe)); |
| 110 | + |
| 111 | + setSuccessMessage(String.format( |
| 112 | + "Successfully scrolled inside the element [%s] over %d second(s)", swipeDirection, durationSeconds)); |
| 113 | + return Result.SUCCESS; |
| 114 | + |
| 115 | + } catch (NoSuchElementException e) { |
| 116 | + logger.debug("Element not found: " + e.getMessage()); |
| 117 | + setErrorMessage("Element not found: " + e.getMessage()); |
| 118 | + return Result.FAILED; |
| 119 | + } catch (Exception e) { |
| 120 | + logger.debug("Scroll failed: " + e.getMessage()); |
| 121 | + setErrorMessage("Scroll failed: " + e.getMessage()); |
| 122 | + return Result.FAILED; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private int[] resolveCoordinates(String dir, int left, int right, int top, int bottom, int cx, int cy) { |
| 127 | + switch (dir) { |
| 128 | + // ── Horizontal ────────────────────────────────────────────────────────── |
| 129 | + case "LEFT TO RIGHT": return new int[]{left, cy, right, cy}; |
| 130 | + case "RIGHT TO LEFT": return new int[]{right, cy, left, cy}; |
| 131 | + case "MIDDLE TO LEFT": return new int[]{cx, cy, left, cy}; |
| 132 | + case "MIDDLE TO RIGHT": return new int[]{cx, cy, right, cy}; |
| 133 | + case "LEFT TO MIDDLE": return new int[]{left, cy, cx, cy}; |
| 134 | + case "RIGHT TO MIDDLE": return new int[]{right, cy, cx, cy}; |
| 135 | + // ── Vertical ──────────────────────────────────────────────────────────── |
| 136 | + case "TOP TO BOTTOM": return new int[]{cx, top, cx, bottom}; |
| 137 | + case "BOTTOM TO TOP": return new int[]{cx, bottom, cx, top}; |
| 138 | + case "TOP TO MIDDLE": return new int[]{cx, top, cx, cy}; |
| 139 | + case "MIDDLE TO TOP": return new int[]{cx, cy, cx, top}; |
| 140 | + case "BOTTOM TO MIDDLE":return new int[]{cx, bottom, cx, cy}; |
| 141 | + case "MIDDLE TO BOTTOM":return new int[]{cx, cy, cx, bottom}; |
| 142 | + default: return null; |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments