Skip to content

Commit

Permalink
The addition to #513 (#514)
Browse files Browse the repository at this point in the history
* Splitting of TouchActions

* Codacy Fixes

* #456 FIX #454 FIX: API redesign.
- new interfaces were added
- deprecated API

* #456 FIX #454 FIX: MultiTouchAction refactoring
- new methods were added to MultiTouchAction
- AppiumDriver methods which perform multiple touch actions were marked as Deprecated
 - Constructors of TouchAction and MultiTouchAction were changed. Now it accepts any instance that can perform touch action and multiple touch actions.

* #456 FIX #454 FIX: New CreatesSwipeAction API

- the new interface CreatesSwipeAction was added.

- the reversion of last changes of TouchableElement.

- the `swipe` is deprecated method.

* #456 FIX #454 FIX:

Forgot to commit this change

* #456 FIX #454 FIX:  CreatesSwipeAction API was implemented

- CreatesSwipeAction API was implemented
- SwipeElementDirection was redesigned
- constructors of TouchAction and MultiTouchAction were improved.

* #456 FIX #454 FIX: AndroidTouchActions were covered with tests.

- also code issues were got fixed

* #456 FIX #454 FIX: Refactoring of MobileElement

* #456 FIX #454 FIX: IOSGesturesTest was redesigned.
- IOSSwipeGestureTest was added

* #456 FIX #454 FIX: Checkstyle issues were got fixed

* #456 FIX #454 FIX: The additional test on Android.
- the swiping combined with the tapping.

* Issues that found by codecy were got fixed

* The addition #513

Fixed Codacy errors

Fixed Codacy errors

* Fixed tests

Fixed tests

Fixed tests

Fixed tests
  • Loading branch information
SrinivasanTarget authored and TikhomirovSergey committed Nov 24, 2016
1 parent 28d10c8 commit 7042073
Show file tree
Hide file tree
Showing 35 changed files with 972 additions and 373 deletions.
133 changes: 37 additions & 96 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.DeviceRotation;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
Expand Down Expand Up @@ -67,7 +65,7 @@
* for each target mobile OS (still Android and iOS)
*/
@SuppressWarnings("unchecked")
public abstract class AppiumDriver<T extends WebElement>
public class AppiumDriver<T extends WebElement>
extends DefaultGenericMobileDriver<T> {

private static final ErrorHandler errorHandler = new ErrorHandler(new ErrorCodesMobile(), true);
Expand Down Expand Up @@ -214,9 +212,11 @@ public List<T> findElementsByXPath(String using) {
}

/**
* @see TouchShortcuts#tap(int, WebElement, int).
* This method is deprecated and it is going to be removed soon.
* Please use {@link MultiTouchAction#tap(int, WebElement, int)}.
*/
@Override public void tap(int fingers, WebElement element, int duration) {
@Deprecated
public void tap(int fingers, WebElement element, int duration) {
MultiTouchAction multiTouch = new MultiTouchAction(this);

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

/**
* @see TouchShortcuts#tap(int, int, int, int).
* This method is deprecated and it is going to be removed soon.
* Please use {@link MultiTouchAction#tap(int, int, int, int)}.
*/
@Override public void tap(int fingers, int x, int y, int duration) {
@Deprecated
public void tap(int fingers, int x, int y, int duration) {
MultiTouchAction multiTouch = new MultiTouchAction(this);

for (int i = 0; i < fingers; i++) {
multiTouch.add(createTap(x, y, duration));
}

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).
* This method is deprecated.
* It was moved to {@link CreatesSwipeAction#swipe(int, int, int, int, int)}.
*/
@Override public abstract void swipe(int startx, int starty, int endx, int endy, int duration);
@Deprecated
public void swipe(int startx, int starty, int endx, int endy, int duration) {
//does nothing
}

/**
* 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.
* This method is deprecated and it is going to be removed soon.
* Please use {@link MultiTouchAction#pinch(WebElement)}.
*/
@Deprecated
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.
* This method is deprecated and it is going to be removed soon.
* Please use {@link MultiTouchAction#pinch(int, int, int, int)} or
* {@link MultiTouchAction#pinch(int, int, int)}
*/
@Deprecated
public void pinch(int x, int y) {
MultiTouchAction multiTouch = new MultiTouchAction(this);

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

if (y - 100 < 0) {
Expand All @@ -313,57 +281,30 @@ public void pinch(int x, int 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.add(action0).add(action1).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.
* This method is deprecated and it is going to be removed soon.
* Please use {@link MultiTouchAction#zoom(WebElement)}.
*/
@Deprecated
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.
* This method is deprecated and it is going to be removed soon.
* Please use {@link MultiTouchAction#zoom(int, int, int, int)} or
* {@link MultiTouchAction#zoom(int, int, int)}.
*/
@Deprecated
public void zoom(int x, int y) {
MultiTouchAction multiTouch = new MultiTouchAction(this);

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

if (y - 100 < 0) {
Expand All @@ -375,9 +316,7 @@ public void zoom(int x, int 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.add(action0).add(action1).perform();
}

@Override public WebDriver context(String name) {
Expand Down Expand Up @@ -447,11 +386,13 @@ public void zoom(int x, int y) {
locationContext.setLocation(location);
}

@Deprecated
private TouchAction createTap(WebElement element, int duration) {
TouchAction tap = new TouchAction(this);
return tap.press(element).waitAction(duration).release();
}

@Deprecated
private TouchAction createTap(int x, int y, int duration) {
TouchAction tap = new TouchAction(this);
return tap.press(x, y).waitAction(duration).release();
Expand Down
101 changes: 101 additions & 0 deletions src/main/java/io/appium/java_client/CreatesSwipeAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client;

import org.openqa.selenium.WebElement;

public interface CreatesSwipeAction {

/**
* Creates combined touch action for the swiping by start/end coordinates.
*
* @param startX x-coordinate to swipe from
* @param startY y-coordinate to swipe from
* @param endX x-coordinate to swipe to
* @param endY y-coordinate to swipe to
* @param duration in milliseconds
* @return an instance of combined {@link TouchAction}
*/
TouchAction swipe(int startX, int startY, int endX, int endY, int duration);

/**
* Creates combined touch action for the swiping from given coordinates to the given element.
*
* @param startX x-coordinate to swipe from
* @param startY y-coordinate to swipe from
* @param element an element to swipe to
* @param duration in milliseconds
* @return an instance of combined {@link TouchAction}
*/
TouchAction swipe(int startX, int startY, WebElement element, int duration);

/**
* Creates combined touch action for the swiping from one element to another.
*
* @param element1 an element to swipe to
* @param element2 an element to swipe to
* @param duration in milliseconds
* @return an instance of combined {@link TouchAction}
*/
TouchAction swipe(WebElement element1, WebElement element2, int duration);


/**
* Creates combined touch action for the swiping inside an element.
*
* @param element an element where the swiping is performed
* @param direction is the direction to perform the swiping inside the element
* @param duration in milliseconds
* @return an instance of combined {@link TouchAction}
*/
default TouchAction swipe(MobileElement element, SwipeElementDirection direction, int duration) {
return direction.swipe(this, element, 0, 0, duration);
}

/**
* Creates combined touch action for the swiping inside an element using some offset from its
* borders.
*
* @param element an element where the swiping is performed
* @param direction is the direction to perform the swiping inside the element
* @param offsetFromStartBorder is the offset from the border of the element where the
* swiping should be started. If direction is UP then
* this is offset from the bottom of the element.
* If direction is DOWN then this is offset from the top of
* the element. If direction is RIGHT then this is offset from
* the left border of the element. If direction is LEFT then
* this is offset from the right border of the element.
* @param offsetFromEndBorder is the offset from the border of the element where
* the swiping should be finished. If direction is UP then
* this is offset from the top of the element.
* If direction is DOWN then this is offset from the bottom
* of the element. If direction is RIGHT then
* this is offset from the right border of the element.
* If direction is LEFT then this is offset from the
* left border of the element.
* @param duration in milliseconds
* @return an instance of combined {@link TouchAction}
* @throws IllegalCoordinatesException when resulted coordinates are out of the
* element borders or disagree with the given direction.
*/
default TouchAction swipe(MobileElement element, SwipeElementDirection direction, int offsetFromStartBorder,
int offsetFromEndBorder,
int duration) throws IllegalCoordinatesException {
return direction.swipe(this, element, offsetFromStartBorder, offsetFromEndBorder,
duration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

import org.openqa.selenium.remote.Response;

@Deprecated
/**
* This interface is deprecated and won't be supported anymore.
* Please use {@link HasDeviceTime} and {@link HidesKeyboard} API instead.
*/
public interface DeviceActionShortcuts extends ExecutesMethod {

/**
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/io/appium/java_client/HasDeviceTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client;

import static io.appium.java_client.MobileCommand.GET_DEVICE_TIME;

import org.openqa.selenium.remote.Response;

public interface HasDeviceTime extends ExecutesMethod {
/*
Gets device date and time for both iOS(Supports only real device) and Android devices
*/
default String getDeviceTime() {
Response response = execute(GET_DEVICE_TIME);
return response.getValue().toString();
}
}
Loading

0 comments on commit 7042073

Please sign in to comment.