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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 84 additions & 61 deletions src/main/java/io/appium/java_client/MobileElement.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,84 @@
/*
+Copyright 2014 Appium contributors
+Copyright 2014 Software Freedom Conservancy
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+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 com.google.common.collect.ImmutableMap;
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 org.openqa.selenium.remote.RemoteWebElement;

import java.util.List;

public abstract class MobileElement extends RemoteWebElement implements FindsByAccessibilityId {

protected FileDetector fileDetector;

public List<WebElement> findElements(By by) {
return by.findElements(this);
}

public WebElement findElement(By by) {
return by.findElement(this);
}

public WebElement findElementByAccessibilityId(String using) {
return findElement("accessibility id", using);
}

public List<WebElement> findElementsByAccessibilityId(String using) {
return findElements("accessibility id", using);
}

public void setValue(String value) {
ImmutableMap.Builder builder = ImmutableMap.builder();
builder.put("id", id).put("value", value);
execute(MobileCommand.SET_VALUE, builder.build());
}

public Point getCenter() {
Point upperLeft = this.getLocation();
Dimension dimensions = this.getSize();
return new Point(upperLeft.getX() + dimensions.getWidth() / 2, upperLeft.getY() + dimensions.getHeight() / 2);
}
}
/*
+Copyright 2014 Appium contributors
+Copyright 2014 Software Freedom Conservancy
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+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 com.google.common.collect.ImmutableMap;

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 org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;

import java.util.List;

public abstract class MobileElement extends RemoteWebElement implements FindsByAccessibilityId, TouchableElement {

protected FileDetector fileDetector;

public List<WebElement> findElements(By by) {
return by.findElements(this);
}

public WebElement findElement(By by) {
return by.findElement(this);
}

public WebElement findElementByAccessibilityId(String using) {
return findElement("accessibility id", using);
}

public List<WebElement> findElementsByAccessibilityId(String using) {
return findElements("accessibility id", using);
}

public void setValue(String value) {
ImmutableMap.Builder builder = ImmutableMap.builder();
builder.put("id", id).put("value", value);
execute(MobileCommand.SET_VALUE, builder.build());
}

public Point getCenter() {
Point upperLeft = this.getLocation();
Dimension dimensions = this.getSize();
return new Point(upperLeft.getX() + dimensions.getWidth() / 2, upperLeft.getY() + dimensions.getHeight() / 2);
}

@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 zoom() {
((AppiumDriver) parent).zoom(this);
}


@Override
public void swipe(SwipeElementDirection direction, int duration) {
direction.swipe((AppiumDriver) parent, this, duration);
}
}
57 changes: 57 additions & 0 deletions src/main/java/io/appium/java_client/SwipeElementDirection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.appium.java_client;

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

public enum SwipeElementDirection {
/**
* Up from the center of the lower
*/
UP{
@Override
void swipe(AppiumDriver driver, MobileElement element, int duration){
Point p = element.getCenter();
Point location = element.getLocation();
Dimension size = element.getSize();
driver.swipe(p.getX(), location.getY() + size.getHeight(), p.getX(), location.getY(), duration);
}
},
/**
* Down from the center of the upper
*/
DOWN{
@Override
void swipe(AppiumDriver driver, MobileElement element, int duration){
Point p = element.getCenter();
Point location = element.getLocation();
Dimension size = element.getSize();
driver.swipe(p.getX(), location.getY(), p.getX(), location.getY() + size.getHeight(), duration);
}
},
/**
* To the left from the center of the rightmost
*/
LEFT{
@Override
void swipe(AppiumDriver driver, MobileElement element, int duration){
Point p = element.getCenter();
Point location = element.getLocation();
Dimension size = element.getSize();
driver.swipe(location.getX() + size.getWidth(), p.getY(), location.getX(), p.getY(), duration);
}
},
/**
* To the right from the center of the leftmost
*/
RIGHT{
@Override
void swipe(AppiumDriver driver, MobileElement element, int duration){
Point p = element.getCenter();
Point location = element.getLocation();
Dimension size = element.getSize();
driver.swipe(location.getX(), p.getY(), location.getX()+ size.getWidth(), p.getY(), duration);
}
};

void swipe(AppiumDriver driver, MobileElement element, int duration){}
}
52 changes: 52 additions & 0 deletions src/main/java/io/appium/java_client/TouchableElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.appium.java_client;

import org.openqa.selenium.WebElement;

/**
* It supposed that mobile elements could be tappable, swipeable, zoomable and so on.
* This interface extends {@link WebElement} and describes this behavior.
*/
public interface TouchableElement extends WebElement {

/**
* Convenience method for pinching the given element.
* "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.
*
*/
public void pinch();

/**
* Convenience method for tapping the center of the given element
*
* @param fingers
* number of fingers/appendages to tap with
* @param duration
* how long between pressing down, and lifting fingers/appendages
*/
public void tap(int fingers, int duration);

/**
* Convenience method for "zooming in" on the given element.
* "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.
*/
public void zoom();

/**
* Convenience method for swiping on the given element to the given direction
*
* @param direction UP, DOWN, LEFT, RIGHT
*
* @param duration amount of time in milliseconds for the entire swipe action to
* take
*/
public void swipe(SwipeElementDirection direction, int duration);

}
Loading