Skip to content

Commit 3aee8fc

Browse files
Merge pull request #338 from testsigmahq/feat/CUS-10727-Added-class-to-swipe-based-on-directions
feat/CUS-10727-Added class to swipe based on directions
2 parents e085ae4 + e812a89 commit 3aee8fc

3 files changed

Lines changed: 205 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
<groupId>com.testsigma.addons</groupId>
8+
<artifactId>swipe_inside_element_with_direction</artifactId>
9+
<version>1.0.0</version>
10+
<packaging>jar</packaging>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<maven.compiler.source>11</maven.compiler.source>
15+
<maven.compiler.target>11</maven.compiler.target>
16+
<testsigma.sdk.version>1.2.24_cloud</testsigma.sdk.version>
17+
<junit.jupiter.version>5.8.0-M1</junit.jupiter.version>
18+
<testsigma.addon.maven.plugin>1.0.0</testsigma.addon.maven.plugin>
19+
<maven.source.plugin.version>3.2.1</maven.source.plugin.version>
20+
<lombok.version>1.18.30</lombok.version>
21+
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>com.testsigma</groupId>
27+
<artifactId>testsigma-java-sdk</artifactId>
28+
<version>${testsigma.sdk.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.projectlombok</groupId>
32+
<artifactId>lombok</artifactId>
33+
<version>${lombok.version}</version>
34+
<optional>true</optional>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-api</artifactId>
39+
<version>${junit.jupiter.version}</version>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.testng</groupId>
44+
<artifactId>testng</artifactId>
45+
<version>6.14.3</version>
46+
</dependency>
47+
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
48+
<dependency>
49+
<groupId>org.seleniumhq.selenium</groupId>
50+
<artifactId>selenium-java</artifactId>
51+
<version>4.33.0</version>
52+
</dependency>
53+
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
54+
<dependency>
55+
<groupId>io.appium</groupId>
56+
<artifactId>java-client</artifactId>
57+
<version>9.4.0</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.fasterxml.jackson.core</groupId>
61+
<artifactId>jackson-annotations</artifactId>
62+
<version>2.13.0</version>
63+
</dependency>
64+
65+
</dependencies>
66+
<build>
67+
<finalName>swipe_inside_element_with_direction</finalName>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-shade-plugin</artifactId>
72+
<version>3.2.4</version>
73+
<executions>
74+
<execution>
75+
<phase>package</phase>
76+
<goals>
77+
<goal>shade</goal>
78+
</goals>
79+
</execution>
80+
</executions>
81+
</plugin>
82+
<plugin>
83+
<groupId>org.apache.maven.plugins</groupId>
84+
<artifactId>maven-source-plugin</artifactId>
85+
<version>${maven.source.plugin.version}</version>
86+
<executions>
87+
<execution>
88+
<id>attach-sources</id>
89+
<goals>
90+
<goal>jar</goal>
91+
</goals>
92+
</execution>
93+
</executions>
94+
</plugin>
95+
</plugins>
96+
</build>
97+
</project>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testsigma-sdk.api.key=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyMjMyMmM2Ni04NWYzLWIyN2UtN2FiOS0zM2U2M2Q4OWM1MGIiLCJ1bmlxdWVJZCI6IjU5NjUiLCJpZGVudGl0eUFjY291bnRVVUlkIjoiMzUifQ.aJfWmLh21f9jqvpmFI_tT_4w7CaSddCKCtvByJRhPGt9aUPqlEBTfkxwTEkqkEfe7rhQ2dOmsQz2FVSHQLPI1Q

0 commit comments

Comments
 (0)