Skip to content

Commit

Permalink
Expand touch options API to accept coordinates as Point (#997)
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst authored and saikrishna321 committed Aug 24, 2018
1 parent fd474c0 commit 97ce87f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ public class ElementOption extends PointOption<ElementOption> {

private String elementId;

/**
* This method creates a build instance of the {@link ElementOption}.
*
* @param element is the element to calculate offset from.
* @param offset is the offset from the upper left corner of the given element.
* @return the built option
*/
public static ElementOption element(WebElement element, Point offset) {
return new ElementOption().withElement(element).withCoordinates(offset);
}


/**
* This method creates a build instance of the {@link ElementOption}.
*
Expand All @@ -40,13 +52,25 @@ public static ElementOption element(WebElement element) {
/**
* It defines x and y offset from the upper left corner of an element.
*
* @param xOffset is x value.
* @param yOffset is y value.
* @param offset is the offset from the upper left corner of the given element.
* @return self-reference
*/
@Override
public ElementOption withCoordinates(Point offset) {
super.withCoordinates(offset);
return this;
}

/**
* It defines x and y offset from the upper left corner of an element.
*
* @param xOffset is the x-offset from the upper left corner of the given element.
* @param yOffset is the y-offset from the upper left corner of the given element.
* @return self-reference
*/
@Override
public ElementOption withCoordinates(int xOffset, int yOffset) {
coordinates = new Point(xOffset, yOffset);
super.withCoordinates(xOffset, yOffset);
return this;
}

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/io/appium/java_client/touch/offset/PointOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ public class PointOption<T extends PointOption<T>> extends ActionOptions<T> {

protected Point coordinates;

/**
* It creates a built instance of {@link PointOption} which takes x and y coordinates.
* This is offset from the upper left corner of the screen.
*
* @param offset is an offset value.
* @return a built option
*/
public static PointOption point(Point offset) {
return new PointOption().withCoordinates(offset);
}

/**
* It creates a built instance of {@link PointOption} which takes x and y coordinates.
* This is offset from the upper left corner of the screen.
Expand All @@ -23,6 +34,17 @@ public static PointOption point(int xOffset, int yOffset) {
return new PointOption().withCoordinates(xOffset, yOffset);
}

/**
* It defines x and y coordinates.
* This is offset from the upper left corner of the screen.
*
* @param offset is an offset value.
* @return self-reference
*/
public T withCoordinates(Point offset) {
return withCoordinates(offset.x, offset.y);
}

/**
* It defines x and y coordinates.
* This is offset from the upper left corner of the screen.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.appium.java_client.touch.offset.ElementOption;
import io.appium.java_client.touch.offset.PointOption;
import org.junit.Test;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;

Expand Down Expand Up @@ -43,7 +44,7 @@ public void invalidEmptyElementOptionsShouldFailOnBuild() {
public void invalidOptionsArgumentsShouldFailOnAltering() {
final List<Runnable> invalidOptions = new ArrayList<>();
invalidOptions.add(() -> waitOptions(ofMillis(-1)));
invalidOptions.add(() -> new ElementOption().withCoordinates(0, 0).withElement(null));
invalidOptions.add(() -> new ElementOption().withCoordinates(new Point(0, 0)).withElement(null));
invalidOptions.add(() -> new WaitOptions().withDuration(null));
invalidOptions.add(() -> tapOptions().withTapsCount(-1));
invalidOptions.add(() -> longPressOptions().withDuration(null));
Expand Down Expand Up @@ -71,7 +72,7 @@ public void longPressOptionsShouldBuildProperly() {
@Test
public void tapOptionsShouldBuildProperly() {
final Map<String, Object> actualOpts = tapOptions()
.withPosition(point(0, 0))
.withPosition(point(new Point(0, 0)))
.withTapsCount(2)
.build();
final Map<String, Object> expectedOpts = new HashMap<>();
Expand Down

0 comments on commit 97ce87f

Please sign in to comment.