|
| 1 | +package com.testsigma.addons.web; |
| 2 | + |
| 3 | +import com.testsigma.sdk.ApplicationType; |
| 4 | +import com.testsigma.sdk.Result; |
| 5 | +import com.testsigma.sdk.WebAction; |
| 6 | +import com.testsigma.sdk.annotation.Action; |
| 7 | +import com.testsigma.sdk.annotation.Element; |
| 8 | +import com.testsigma.sdk.annotation.TestData; |
| 9 | +import org.openqa.selenium.Point; |
| 10 | +import org.openqa.selenium.WebElement; |
| 11 | +import org.openqa.selenium.interactions.Actions; |
| 12 | + |
| 13 | +import java.util.NoSuchElementException; |
| 14 | + |
| 15 | +@Action( |
| 16 | + actionText = "Swipe direction within the element elementLocator", |
| 17 | + description = "Swipes in the given direction within the bounds of the specified element (e.g. TOP TO BOTTOM, TOP TO MIDDLE).", |
| 18 | + applicationType = ApplicationType.WEB, |
| 19 | + useCustomScreenshot = false |
| 20 | +) |
| 21 | +public class SwipeWithInElement extends WebAction { |
| 22 | + |
| 23 | + @TestData(reference = "direction", |
| 24 | + allowedValues = { |
| 25 | + "TOP TO BOTTOM", "BOTTOM TO TOP", |
| 26 | + "TOP TO MIDDLE", "MIDDLE TO TOP", |
| 27 | + "MIDDLE TO BOTTOM", "BOTTOM TO MIDDLE", |
| 28 | + "LEFT TO RIGHT", "RIGHT TO LEFT", |
| 29 | + "LEFT TO MIDDLE", "MIDDLE TO LEFT", |
| 30 | + "MIDDLE TO RIGHT", "RIGHT TO MIDDLE" |
| 31 | + }) |
| 32 | + private com.testsigma.sdk.TestData directionData; |
| 33 | + |
| 34 | + @Element(reference = "elementLocator") |
| 35 | + private com.testsigma.sdk.Element element; |
| 36 | + |
| 37 | + @Override |
| 38 | + public Result execute() throws NoSuchElementException { |
| 39 | + |
| 40 | + Result result = Result.SUCCESS; |
| 41 | + |
| 42 | + try { |
| 43 | + logger.info("Execution started"); |
| 44 | + |
| 45 | + WebElement webElement = element.getElement(); |
| 46 | + logger.info("Located element: " + element.getBy()); |
| 47 | + |
| 48 | + String direction = directionData.getValue().toString().trim().toUpperCase(); |
| 49 | + String[] parts = direction.split(" TO "); |
| 50 | + if (parts.length != 2) { |
| 51 | + throw new IllegalArgumentException("Direction must be in format 'X TO Y', e.g. TOP TO BOTTOM. Got: " + direction); |
| 52 | + } |
| 53 | + String fromPosition = parts[0].trim(); |
| 54 | + String toPosition = parts[1].trim(); |
| 55 | + |
| 56 | + Point startPoint = getPointInElement(webElement, fromPosition); |
| 57 | + Point endPoint = getPointInElement(webElement, toPosition); |
| 58 | + |
| 59 | + Actions actions = new Actions(driver); |
| 60 | + actions.moveToLocation(startPoint.getX(), startPoint.getY()) |
| 61 | + .clickAndHold() |
| 62 | + .moveToLocation(endPoint.getX(), endPoint.getY()) |
| 63 | + .release() |
| 64 | + .perform(); |
| 65 | + |
| 66 | + setSuccessMessage("Successfully swiped " + direction + |
| 67 | + " within element located by: " + element.getBy()); |
| 68 | + |
| 69 | + } catch (NoSuchElementException e) { |
| 70 | + logger.info("Element not found: " + e.getMessage()); |
| 71 | + setErrorMessage("Element not found: " + e.getMessage()); |
| 72 | + result = Result.FAILED; |
| 73 | + |
| 74 | + } catch (Exception e) { |
| 75 | + logger.info("Error occurred: " + e.getMessage()); |
| 76 | + setErrorMessage("Error: " + e.getMessage()); |
| 77 | + result = Result.FAILED; |
| 78 | + } |
| 79 | + |
| 80 | + return result; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + private Point getPointInElement(WebElement element, String position) { |
| 85 | + Point location = element.getLocation(); |
| 86 | + int elementWidth = element.getSize().getWidth(); |
| 87 | + int elementHeight = element.getSize().getHeight(); |
| 88 | + int x = location.getX(); |
| 89 | + int y = location.getY(); |
| 90 | + int inset = 10; |
| 91 | + |
| 92 | + switch (position) { |
| 93 | + case "TOP": |
| 94 | + return new Point(x + elementWidth / 2, y + inset); |
| 95 | + case "MIDDLE": |
| 96 | + return new Point(x + elementWidth / 2, y + elementHeight / 2); |
| 97 | + case "BOTTOM": |
| 98 | + return new Point(x + elementWidth / 2, y + elementHeight - inset); |
| 99 | + case "LEFT": |
| 100 | + return new Point(x + inset, y + elementHeight / 2); |
| 101 | + case "RIGHT": |
| 102 | + return new Point(x + elementWidth - inset, y + elementHeight / 2); |
| 103 | + default: |
| 104 | + throw new IllegalArgumentException("Invalid position: " + position); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments