Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 14 additions & 114 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ public List<T> findElementsByXPath(String using) {
}

/**
* @see TouchShortcuts#tap(int, WebElement, int).
* @see TouchableElement#tap(int, WebElement, int).
*/
@Override public void tap(int fingers, WebElement element, int duration) {
public void tap(int fingers, WebElement element, int duration) {
MultiTouchAction multiTouch = new MultiTouchAction(this);

for (int i = 0; i < fingers; i++) {
Expand All @@ -231,9 +231,9 @@ public List<T> findElementsByXPath(String using) {
}

/**
* @see TouchShortcuts#tap(int, int, int, int).
* @see TouchableElement#tap(int, int, int, int).
*/
@Override public void tap(int fingers, int x, int y, int duration) {
public void tap(int fingers, int x, int y, int duration) {
MultiTouchAction multiTouch = new MultiTouchAction(this);

for (int i = 0; i < fingers; i++) {
Expand All @@ -243,145 +243,45 @@ public List<T> findElementsByXPath(String using) {
multiTouch.perform();
}

protected void doSwipe(int startx, int starty, int endx, int endy, int duration) {
TouchAction touchAction = new TouchAction(this);

// appium converts press-wait-moveto-release to a swipe action
touchAction.press(startx, starty).waitAction(duration).moveTo(endx, endy).release();

touchAction.perform();
}

/**
* @see TouchShortcuts#swipe(int, int, int, int, int).
* @see TouchableElement#swipe(int, int, int, int, int).
*/
@Override public abstract void swipe(int startx, int starty, int endx, int endy, int duration);
public abstract void swipe(int startx, int starty, int endx, int endy, int duration);

/**
* Convenience method for pinching an element on the screen.
* "pinching" refers to the action of two appendages pressing the
* screen and sliding towards each other.
* NOTE:
* This convenience method places the initial touches around the element, if this would
* happen to place one of them off the screen, appium with return an outOfBounds error.
* In this case, revert to using the MultiTouchAction api instead of this method.
*
* @param el The element to pinch.
* @see TouchableElement#pinch(WebElement).
*/
public void pinch(WebElement el) {
MultiTouchAction multiTouch = new MultiTouchAction(this);

Dimension dimensions = el.getSize();
Point upperLeft = el.getLocation();
Point center = new Point(upperLeft.getX() + dimensions.getWidth() / 2,
upperLeft.getY() + dimensions.getHeight() / 2);
int yOffset = center.getY() - upperLeft.getY();

TouchAction action0 =
new TouchAction(this).press(el, center.getX(), center.getY() - yOffset).moveTo(el)
.release();
TouchAction action1 =
new TouchAction(this).press(el, center.getX(), center.getY() + yOffset).moveTo(el)
.release();

multiTouch.add(action0).add(action1);

multiTouch.perform();
multiTouch.pinch(el).perform();
}

/**
* Convenience method for pinching an element on the screen.
* "pinching" refers to the action of two appendages pressing the screen and
* sliding towards each other.
* NOTE:
* This convenience method places the initial touches around the element at a distance,
* if this would happen to place one of them off the screen, appium will return an
* outOfBounds error. In this case, revert to using the MultiTouchAction api instead of this
* method.
*
* @param x x coordinate to terminate the pinch on.
* @param y y coordinate to terminate the pinch on.
* @see TouchableElement#pinch(int, int).
*/
public void pinch(int x, int y) {
MultiTouchAction multiTouch = new MultiTouchAction(this);

int scrHeight = manage().window().getSize().getHeight();
int yOffset = 100;

if (y - 100 < 0) {
yOffset = y;
} else if (y + 100 > scrHeight) {
yOffset = scrHeight - y;
}

TouchAction action0 = new TouchAction(this).press(x, y - yOffset).moveTo(x, y).release();
TouchAction action1 = new TouchAction(this).press(x, y + yOffset).moveTo(x, y).release();

multiTouch.add(action0).add(action1);

multiTouch.perform();
multiTouch.pinch(x, y).perform();
}

/**
* Convenience method for "zooming in" on an element on the screen.
* "zooming in" refers to the action of two appendages pressing the screen and sliding
* away from each other.
* NOTE:
* This convenience method slides touches away from the element, if this would happen
* to place one of them off the screen, appium will return an outOfBounds error.
* In this case, revert to using the MultiTouchAction api instead of this method.
*
* @param el The element to pinch.
* @see TouchableElement#zoom(WebElement).
*/
public void zoom(WebElement el) {
MultiTouchAction multiTouch = new MultiTouchAction(this);

Dimension dimensions = el.getSize();
Point upperLeft = el.getLocation();
Point center = new Point(upperLeft.getX() + dimensions.getWidth() / 2,
upperLeft.getY() + dimensions.getHeight() / 2);
int yOffset = center.getY() - upperLeft.getY();

TouchAction action0 = new TouchAction(this).press(center.getX(), center.getY())
.moveTo(el, center.getX(), center.getY() - yOffset).release();
TouchAction action1 = new TouchAction(this).press(center.getX(), center.getY())
.moveTo(el, center.getX(), center.getY() + yOffset).release();

multiTouch.add(action0).add(action1);

multiTouch.perform();
multiTouch.zoom(el).perform();
}

/**
* Convenience method for "zooming in" on an element on the screen.
* "zooming in" refers to the action of two appendages pressing the screen
* and sliding away from each other.
* NOTE:
* This convenience method slides touches away from the element, if this would happen to
* place one of them off the screen, appium will return an outOfBounds error. In this case,
* revert to using the MultiTouchAction api instead of this method.
*
* @param x x coordinate to start zoom on.
* @param y y coordinate to start zoom on.
* @see TouchableElement#zoom(int, int).
*/
public void zoom(int x, int y) {
MultiTouchAction multiTouch = new MultiTouchAction(this);

int scrHeight = manage().window().getSize().getHeight();
int yOffset = 100;

if (y - 100 < 0) {
yOffset = y;
} else if (y + 100 > scrHeight) {
yOffset = scrHeight - y;
}

TouchAction action0 = new TouchAction(this).press(x, y).moveTo(0, -yOffset).release();
TouchAction action1 = new TouchAction(this).press(x, y).moveTo(0, yOffset).release();

multiTouch.add(action0).add(action1);

multiTouch.perform();
multiTouch.zoom(x, y).perform();
}

@Override public WebDriver context(String name) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/appium/java_client/MobileDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.Map;

public interface MobileDriver<T extends WebElement> extends WebDriver, PerformsTouchActions, ContextAware, Rotatable,
FindsByAccessibilityId<T>, LocationContext, DeviceActionShortcuts, TouchShortcuts,
FindsByAccessibilityId<T>, LocationContext, DeviceActionShortcuts,
InteractsWithFiles, InteractsWithApps, HasAppStrings, FindsByClassName, FindsByCssSelector, FindsById,
FindsByLinkText, FindsByName, FindsByTagName, FindsByXPath, FindsByFluentSelector<T>, ExecutesMethod {

Expand Down
32 changes: 30 additions & 2 deletions src/main/java/io/appium/java_client/MobileElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.FileDetector;

import java.util.List;
Expand All @@ -42,18 +43,45 @@ public Point getCenter() {
upperLeft.getY() + dimensions.getHeight() / 2);
}

@Override public void tap(int fingers, int duration) {
((AppiumDriver<?>) parent).tap(fingers, this, duration);
}

@Override public void tap(int fingers, int x, int y, int duration) {
((AppiumDriver<?>) parent).tap(fingers, x, y, duration);
}

@Override public void tap(int fingers, WebElement element, int duration) {
((AppiumDriver<?>) parent).tap(fingers, element, duration);
}

@Override public void pinch() {
((AppiumDriver<?>) parent).pinch(this);
}

@Override public void tap(int fingers, int duration) {
((AppiumDriver<?>) parent).tap(fingers, this, duration);
@Override public void pinch(int x, int y) {
((AppiumDriver<?>) parent).pinch(x, y);
}

@Override public void pinch(WebElement el) {
((AppiumDriver<?>) parent).pinch(el);
}

@Override public void zoom() {
((AppiumDriver<?>) parent).zoom(this);
}

@Override public void zoom(int x, int y) {
((AppiumDriver<?>) parent).zoom(x, y);
}

@Override public void zoom(WebElement el) {
((AppiumDriver<?>) parent).zoom(el);
}

@Override public void swipe(int startx, int starty, int endx, int endy, int duration) {
((AppiumDriver<?>) parent).swipe(startx, startx, endx, endy ,duration);
}

@Override public void swipe(SwipeElementDirection direction, int duration) {
direction.swipe((AppiumDriver<?>) parent, this, 0, 0, duration);
Expand Down
88 changes: 88 additions & 0 deletions src/main/java/io/appium/java_client/MultiTouchAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;

/**
* Used for Webdriver 3 multi-touch gestures
* See the Webriver 3 spec https://dvcs.w3.org/hg/webdriver/raw-file/default/webdriver-spec.html
Expand Down Expand Up @@ -86,4 +90,88 @@ protected ImmutableMap getParameters() {
}
return ImmutableMap.of("actions", listOfActionChains.build());
}

/**
* @see TouchableElement#pinch(WebElement).
*/
public MultiTouchAction pinch(WebElement el) {
MultiTouchAction multiTouch = new MultiTouchAction(driver);

Dimension dimensions = el.getSize();
Point upperLeft = el.getLocation();
Point center = new Point(upperLeft.getX() + dimensions.getWidth() / 2,
upperLeft.getY() + dimensions.getHeight() / 2);
int yOffset = center.getY() - upperLeft.getY();

TouchAction action0 =
new TouchAction(driver).press(el, center.getX(), center.getY() - yOffset).moveTo(el)
.release();
TouchAction action1 =
new TouchAction(driver).press(el, center.getX(), center.getY() + yOffset).moveTo(el)
.release();

return multiTouch.add(action0).add(action1);
}

/**
* @see TouchableElement#pinch(int, int).
*/
public MultiTouchAction pinch(int x, int y) {
MultiTouchAction multiTouch = new MultiTouchAction(driver);

int scrHeight = driver.manage().window().getSize().getHeight();
int yOffset = 100;

if (y - 100 < 0) {
yOffset = y;
} else if (y + 100 > scrHeight) {
yOffset = scrHeight - y;
}

TouchAction action0 = new TouchAction(driver).press(x, y - yOffset).moveTo(x, y).release();
TouchAction action1 = new TouchAction(driver).press(x, y + yOffset).moveTo(x, y).release();

return multiTouch.add(action0).add(action1);
}

/**
* @see TouchableElement#zoom(WebElement).
*/
public MultiTouchAction zoom(WebElement el) {
MultiTouchAction multiTouch = new MultiTouchAction(driver);

Dimension dimensions = el.getSize();
Point upperLeft = el.getLocation();
Point center = new Point(upperLeft.getX() + dimensions.getWidth() / 2,
upperLeft.getY() + dimensions.getHeight() / 2);
int yOffset = center.getY() - upperLeft.getY();

TouchAction action0 = new TouchAction(driver).press(center.getX(), center.getY())
.moveTo(el, center.getX(), center.getY() - yOffset).release();
TouchAction action1 = new TouchAction(driver).press(center.getX(), center.getY())
.moveTo(el, center.getX(), center.getY() + yOffset).release();

return multiTouch.add(action0).add(action1);
}

/**
* @see TouchableElement#zoom(int, int).
*/
public MultiTouchAction zoom(int x, int y) {
MultiTouchAction multiTouch = new MultiTouchAction(driver);

int scrHeight = driver.manage().window().getSize().getHeight();
int yOffset = 100;

if (y - 100 < 0) {
yOffset = y;
} else if (y + 100 > scrHeight) {
yOffset = scrHeight - y;
}

TouchAction action0 = new TouchAction(driver).press(x, y).moveTo(0, -yOffset).release();
TouchAction action1 = new TouchAction(driver).press(x, y).moveTo(0, yOffset).release();

return multiTouch.add(action0).add(action1);
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/appium/java_client/TouchAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
@SuppressWarnings({"rawtypes", "unchecked"}) public class TouchAction {

ImmutableList.Builder parameterBuilder;
protected ImmutableList.Builder parameterBuilder;
private MobileDriver driver;

public TouchAction(MobileDriver driver) {
Expand Down Expand Up @@ -346,7 +346,7 @@ protected void clearParameters() {
/**
* Just holds values to eventually return the parameters required for the mjsonwp.
*/
private class ActionParameter {
protected class ActionParameter {
private String actionName;
private ImmutableMap.Builder optionsBuilder;

Expand Down
Loading