Skip to content

Commit 97ce87f

Browse files
valfirstsaikrishna321
authored andcommitted
Expand touch options API to accept coordinates as Point (#997)
1 parent fd474c0 commit 97ce87f

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

src/main/java/io/appium/java_client/touch/offset/ElementOption.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ public class ElementOption extends PointOption<ElementOption> {
1515

1616
private String elementId;
1717

18+
/**
19+
* This method creates a build instance of the {@link ElementOption}.
20+
*
21+
* @param element is the element to calculate offset from.
22+
* @param offset is the offset from the upper left corner of the given element.
23+
* @return the built option
24+
*/
25+
public static ElementOption element(WebElement element, Point offset) {
26+
return new ElementOption().withElement(element).withCoordinates(offset);
27+
}
28+
29+
1830
/**
1931
* This method creates a build instance of the {@link ElementOption}.
2032
*
@@ -40,13 +52,25 @@ public static ElementOption element(WebElement element) {
4052
/**
4153
* It defines x and y offset from the upper left corner of an element.
4254
*
43-
* @param xOffset is x value.
44-
* @param yOffset is y value.
55+
* @param offset is the offset from the upper left corner of the given element.
56+
* @return self-reference
57+
*/
58+
@Override
59+
public ElementOption withCoordinates(Point offset) {
60+
super.withCoordinates(offset);
61+
return this;
62+
}
63+
64+
/**
65+
* It defines x and y offset from the upper left corner of an element.
66+
*
67+
* @param xOffset is the x-offset from the upper left corner of the given element.
68+
* @param yOffset is the y-offset from the upper left corner of the given element.
4569
* @return self-reference
4670
*/
4771
@Override
4872
public ElementOption withCoordinates(int xOffset, int yOffset) {
49-
coordinates = new Point(xOffset, yOffset);
73+
super.withCoordinates(xOffset, yOffset);
5074
return this;
5175
}
5276

src/main/java/io/appium/java_client/touch/offset/PointOption.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ public class PointOption<T extends PointOption<T>> extends ActionOptions<T> {
1111

1212
protected Point coordinates;
1313

14+
/**
15+
* It creates a built instance of {@link PointOption} which takes x and y coordinates.
16+
* This is offset from the upper left corner of the screen.
17+
*
18+
* @param offset is an offset value.
19+
* @return a built option
20+
*/
21+
public static PointOption point(Point offset) {
22+
return new PointOption().withCoordinates(offset);
23+
}
24+
1425
/**
1526
* It creates a built instance of {@link PointOption} which takes x and y coordinates.
1627
* This is offset from the upper left corner of the screen.
@@ -23,6 +34,17 @@ public static PointOption point(int xOffset, int yOffset) {
2334
return new PointOption().withCoordinates(xOffset, yOffset);
2435
}
2536

37+
/**
38+
* It defines x and y coordinates.
39+
* This is offset from the upper left corner of the screen.
40+
*
41+
* @param offset is an offset value.
42+
* @return self-reference
43+
*/
44+
public T withCoordinates(Point offset) {
45+
return withCoordinates(offset.x, offset.y);
46+
}
47+
2648
/**
2749
* It defines x and y coordinates.
2850
* This is offset from the upper left corner of the screen.

src/test/java/io/appium/java_client/touch/TouchOptionsTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.appium.java_client.touch.offset.ElementOption;
1717
import io.appium.java_client.touch.offset.PointOption;
1818
import org.junit.Test;
19+
import org.openqa.selenium.Point;
1920
import org.openqa.selenium.WebElement;
2021
import org.openqa.selenium.remote.RemoteWebElement;
2122

@@ -43,7 +44,7 @@ public void invalidEmptyElementOptionsShouldFailOnBuild() {
4344
public void invalidOptionsArgumentsShouldFailOnAltering() {
4445
final List<Runnable> invalidOptions = new ArrayList<>();
4546
invalidOptions.add(() -> waitOptions(ofMillis(-1)));
46-
invalidOptions.add(() -> new ElementOption().withCoordinates(0, 0).withElement(null));
47+
invalidOptions.add(() -> new ElementOption().withCoordinates(new Point(0, 0)).withElement(null));
4748
invalidOptions.add(() -> new WaitOptions().withDuration(null));
4849
invalidOptions.add(() -> tapOptions().withTapsCount(-1));
4950
invalidOptions.add(() -> longPressOptions().withDuration(null));
@@ -71,7 +72,7 @@ public void longPressOptionsShouldBuildProperly() {
7172
@Test
7273
public void tapOptionsShouldBuildProperly() {
7374
final Map<String, Object> actualOpts = tapOptions()
74-
.withPosition(point(0, 0))
75+
.withPosition(point(new Point(0, 0)))
7576
.withTapsCount(2)
7677
.build();
7778
final Map<String, Object> expectedOpts = new HashMap<>();

0 commit comments

Comments
 (0)